# Tuesday, 24 June 2008

P/Invoke (aka DllImport) signatures are not the world's most fun things to create. You start with a native declaration, and then hand-map native types to the equivalent (you hope) managed types. If at all possible, you head to www.pinvoke.net and look up the API you are calling and paste in whatever some kind soul put on the wiki. If not, well you have some mechanical work to do.

But now the Interop team has a little tool for you! Look up the API you want, choose a language, click the Generate button and -tada!- your declaration is ready to be copied and pasted. (Even has doc comments explaining the params.) Not calling a well-known API (maybe it's your own code from a native project?) No problem, paste in your native C++ signature and translate. Need to go the other way around (what native signature corresponds to a managed one?) No problem.

Sweet. The code it generates isn't pretty (for example it doesn't add any using/Imports statements, so everything gets the full dotted name every time, and everything is explicitly declared) but then again, who reads generated code? Stick a comment in front of it saying you generated it and leave it alone. Even if it needs a little hand tweaking now and again (and I honestly don't know whether it does or not) it will still save a TON of time. It's on CodePlex, so go get it.

Kate

ps: The list of APIs is in a XML file, and I noticed the Vista-only ones I tried (restart and recovery related mostly) weren't found, so if you wanted to make a contribution to the project...

Tuesday, 24 June 2008 19:56:13 (Eastern Daylight Time, UTC-04:00)  #