Thursday, March 26, 2009


Marshaling between Managed and Unmanaged Code: PInvoke Interop Assistant

I recently saw this useful tool mentioned on StackOverflow by JaredPar (it seems I need to catch up on the MSDN magazines that are starting to pile up!): the PInvoke Interop Assistant hosted on CodePlex and first described in this MSDN article, Marshaling between Managed and Unmanaged Code:

.NET Framework interop opens a channel between managed and unmanaged code, and marshaling plays a very important role in that connection in that it allows for data exchange between the two. There are many factors that affect the way the CLR marshals data between the unmanaged and managed worlds, including attributes such as [MarshalAs], [StructLayout], [InAttribute], and [OutAttribute] as well as language keywords such as out and ref in C#.

Understanding and remembering all the attributes and rules (described in the MSDN article) may seem a bit daunting. After all, most developers of managed code just need to be able to quickly figure out the P/Invoke signature for a Win32® API function, paste it in their code, and be done with it. That's where the P/Invoke Interop Assistant can help. This tool can efficiently assist with conversions from C++ to managed P/Invoke signatures as well as those in the opposite direction. It even comes with a database of Win32 functions, data types, and constants, so the common task of adding a Win32 P/Invoke to your C# or Visual Basic source file is made very easy. The tool package includes two command-line tools, SigImp and SigExp, which can be used for batch file processing. A GUI tool is also found in the package, which includes the functionalities of both tools.


Powered by Blogger