Saturday, March 15, 2008

The PInvoke tool you've been looking for all this time... the "PInvoke Interop Assistant"

jaredpar's WebLog - Making PInvoke Easy

"I very excited to announce we recently released a tool I've been working on to MSDN that will greatly help with using PInvoke in managed code.  The tool is called the "PInvoke Interop Assistant" and is included as part of a MSDN article on marshalling data for PInvoke and Reverse PInvoke scenarios. 

Here is a link to the article and tool

The motivation behind this tool is writing PInvoke is a hard and often tedious task. There are many rules you must obey and many exceptions that must be taken into account.  Anything beyond simple data structures gets very involved and subtle semantics of C can greatly change the needed signature.  Incorrect translations often result in obscure exceptions or crashes.

In short, it's not any fun.

The tool works in several different ways to make PInvoke generation an easier process.  The goal is to make generating managed code for structs, unions, enums, constants, functions, typedefs , etc ... as easy as possible. The resulting code can be generated in both VB and C#.

The GUI version of the tool operates in 3 modes.  

  1. SigImp Search: Search for a commonly used function and translate it into managed code.
  2. SigImp Translate Snippet: Directly translate C code into managed PInvoke signatures.
  3. SigExp: Convert managed binaries into C++ Reverse PInvoke scenarios

..."

Coming from a long boot strapped VB background (i.e. self taught, propelled and motivated, many years of blood, sweat and tears, etc), I've never been comfortable converting C/C++ signatures into PInvoke calls. And while "the Net" provides preconverted signatures for many libraries/API's/etc, when I come across something internal or purchased that doesn't already have PInvokes available the sweat begins the bead on my forehead and that "I hate being a lamer and not knowing this stuff" feeling starts to kick in.

Well now it seems Microsoft has heard my cries and has released a utility to help.

The PInvoke Assistant will help export signatures for Native consumption, find PInvoke stuff for existing Windows native signatures and convert (via GUI or command line)  native signatures into managed/PInvoked ones.

Good because I didn't really want to "...know this stuff" anyway. I just want to write code and solve problems... :)

 

Here's the Assistant with CopyFileExW selected (used when you want to handle long, greater than MAX_PATH, folder paths which .Net doesn't support yet) and VB code generated.

image

The generated code pasted into VS. Compiled and ran the first time...

image

1 comment:

Unknown said...

Hi, The PInvoke Interop Assistant is only capable of dealing with C Style DLL, not C++ dll with classes. The .NET PInvoke Interop SDK for C++ DLL can generate C# wrapper for C++ native DLL automatically just based on the C++ DLL itself and the C++ header files. There is lots of features it offers. Please take a look if you are interested.