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

22
Rachana George .NET Security, Summer 2010

Upload: juniper-gilmore

Post on 19-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

Rachana George.NET Security, Summer 2010

Page 2: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

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

Overview

Page 3: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

• 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

Page 4: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

Sample Unmanaged C++ Library

Page 5: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 6: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 7: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

• 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

Page 8: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 9: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

Mapping mangled names to class members

Page 10: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

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

Perform Platform Invoke

Page 11: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 12: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 13: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 14: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL 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.

Page 15: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

Wrap all the Imports in Managed Classes

Page 16: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 17: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

• 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.

Page 18: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke
Page 19: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

Final Output

Page 20: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

• 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

Page 21: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke

• 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

Page 22: Rachana George.NET Security, Summer 2010. Introduction Sample Unmanaged C++ Library Retrieve Exported Information from the DLL Perform Platform Invoke