rachana george.net security, summer 2010. introduction sample unmanaged c++ library retrieve...

Post on 19-Jan-2016

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Rachana George.NET Security, Summer 2010

• Introduction• Sample Unmanaged C++ Library• Retrieve Exported Information from the DLL• Perform Platform Invoke• Wrap all Imports in Managed Classes• Conclusion• References

Overview

• Why re-use unmanaged C/C++ libraries?

• Approach to reusing the unmanaged libraries IJW : It Just Works –Just recompile old code!

COM: Component Object Model – Works on both managed and unmanaged environments!

P/Invoke: Platform Invoke – Allows you to import a class as functions at the attribute level!

Introduction

Sample Unmanaged C++ Library

• First, import class methods from the DLL.• Using the Microsoft dumping tool “dumpbin.exe” to

retrieve the decorated name for each function from the DLL.

• After executing "dumpbin /exports CppWin32Dll.dll", we get:

Retrieve Exported Information from the DLL

Mapping mangled names to class members

• Four methods have been imported : the constructor, the destructor, GetId, and Move, and put them in another unmanaged class called "VehicleUnman"

Perform Platform Invoke

• Import the exported public methods/members only.

• Don't import compiler-added members. They are mostly internals, and not all of them are accessible.

• Every imported function takes the current pointer as an input parameter, in addition to the original input parameter(s).

• The DLL uses this pointer to call the function properly via the decorated name "@Vehicle" or "@Car", which is how the C++ compiler handles classes internally.

• Virtual Table or VTB has been added manually to handle virtual methods, and it contains function pointers for all virtual methods.

Wrap all the Imports in Managed Classes

• Don't derive the managed "VehicleWrap" from the unmanaged "VehicleUnman".

• Unmanaged wrappers merely provide the storage for the original class members, including data and methods, whereas managed ones handle the class relationship. More importantly, you pass the unmanaged object to the DLL, not the managed one.

• Derive "CarWrap" from "VehicleWrap" to recover the original inheritance between the two unmanaged classes. This way, we don't have to handle the inheritance manually in the managed classes.

Final Output

• This is an approach to reusing unmanaged C++ libraries, particularly when direct importing from unmanaged DLLs becomes necessary.

• The three steps to wrap unmanaged C++ DLLs for use in .NET applications:

Retrieve class member data from the DLL. Import required class methods. Wrap up all the imports in a managed class.

Conclusion

• http://www.codeproject.com/KB/mcpp/usingcppdll.aspx#Top

• http://www.microsoft.com/com/default.mspx• http://social.msdn.microsoft.com/Forums/en-US/

vbgeneral/thread/4624961a-bbd7-4b19-811c-bda211d2d87c

• http://msdn.microsoft.com/en-us/library/ms235636(VS.80).aspx

References

top related