P/Invoke Interop Assistant [Justin Van Patten]
The Interop team recently released a new tool called the P/Invoke Interop Assistant. This tool automatically generates managed p/invoke declarations (in C# or VB) from native signatures. It includes a quick look-up for the common Win32 libraries and lets you generate p/invoke declarations for any other native library simply by passing it the native signature. This makes it a lot easier to do interop correctly, without having to understand all the rules and attributes used when marshalling between unmanaged and managed.
Here's a screenshot of the tool in action. Simply type in the name of a Win32 API, in this case CreateFile, and the tool will generate the correct p/invoke declaration.
The tool was first released as part of an MSDN Magazine article on Marshaling between Managed and Unmanaged Code back in January. Now it's on CodePlex along with the source code. I highly recommend checking it out.
Comments
Anonymous
June 23, 2008
Is there an effort at MS to examine all the p/Invokes that people are using? To include some of that in the BCL I mean. Mono had a tool like that, it basically analyzes source code looking for "stuff" that's missing in either Mono or Linux/OSX/whatever (P/Invokes).Anonymous
June 23, 2008
A efficient tool, but the signature is very detailed. For example, the 'InAttribute', it is really inform it? But, 'LPWStrAttribute', is a good idea, inform it explicitly, to avoid problems :-)Anonymous
June 24, 2008
Ask the Directory Services Team : Custom Certificate Request in Windows Vista Microsoft Security DevelopmentAnonymous
June 24, 2008
The http://pinvoke.net/ wiki is another great place to get interop signatures.Anonymous
June 25, 2008
Shouldn't Create file return a SafeFileHandle instead of an IntPtr?Anonymous
June 25, 2008
Staffan, Ideally yes. Right now the tool just automatically translates unmanaged code into the managed interop definitions. There's no way to figure out from the unmanaged API alone what kind of SafeHandle to use or what API to call to close the handle. It'd be trivial to implement if there was a map of functions, parameter positions, and which function to close with. The team is considering enabling this with a plugin which could do just that based on an input file. Thanks, JustinAnonymous
June 26, 2008
Since today I'm putting my blog reading in day, here's another post announcing what seems toAnonymous
June 26, 2008
Since today I'm putting my blog reading in day, here's another post announcing what seems toAnonymous
June 26, 2008
Looks like a fabulous tool. Could I suggest that the team reaches out to http://pinvoke.net to ask them to advertise it on their front page as well? I know they already have a plugin there, but choice is a good thing...Anonymous
June 27, 2008
It would be nice to have the same for COM Interop.