Saturday, March 24, 2007

* Overview of Interoperability with unmanaged world

The interop between managed and unmanaged code generally involves following aspects :

  • Wrapping and accessing unmanaged code from managed code
  • Wrapping and accessing managed code from unmanaged code.

This post covers technologies available for wrapping & accessing unmanaged code from managed code.

Some of the forms in which unmanaged code currently exists are :

  • Win32 APIs
  • Static DLL exports
  • COM components
  • C++ Class Libraries

There are mainly three technologies/patterns available to interop with above kinds of unmanaged code :

  • Platform Invoke : Most straightforward and easy to use. Also referred as P/Invoke. Its recommended for calling flat APIs, e.g. Win32 APIs and static dll exports. CLR takes care of loading the DLLs and marshaling parameters.
  • COM Interop : This is the most suited way of interacting with COM components. Interop assemblies can be generated using tools like Tlbimp and Tlbexp which can be directly referenced in managed code. CLR will take care of component activation and parameter marshaling.
  • Managed VC++ wrapper : This is the most crude way of interop with unmanaged code and can work with any of the above unmanaged components. It involves writing a VC++ wrapper over unmanaged code. This wrapper will have both unmanaged code (to consume unmanaged component) and managed code to expose the unmanaged functionality to managed world. This is the only way to interop with C++ class libraries.

In my next posts in this series i will try to post the samples of above.

~tata & take care~

Other Posts

No comments:

Post a Comment