P/Invoke Signature Generator

January 12, 2008


If you ever needed to call a Win32 function from .NET code, you know the dreaded feeling.  You fire up MSDN and start looking for the function signature, hoping it doesn’t invent a new pattern of parameter passing, returning a value and calling back a function you specify.  And while you’re at it, you still have to figure out what the function is expecting at offset 0x20 from the beginning of the input buffer when the value of the dwExtendedFlags parameter is non-zero.

This tedious process has long ago come to an end with the advent of PInvoke.net, a free Wiki-like developer portal which targets the problem of generating P/Invoke signatures (it also features a cool Visual Studio add-in).  For example, here’s the signature for CoMarshalInterThreadInterfaceInStream (one of COM’s longest API names):


  That’s all fun and games, but it won’t help you in two particularly annoying cases:

  1. The signature for the API you’re looking for is not in the database.  (For example, ConvertSecurityDescriptorToStringSecurityDescriptor or AccessCheckAndAuditAlarm);
  2. You’re not trying to use a Win32 API, but rather a function in a third-party (non MS) or your own DLL.

For these scenarios, you had to either perform the conversions by hand or resort to old third-party commercial conversion software (I was only able to find the P/Invoke wizard, last updated around May 2004).  I admit, I never tried the latter.  Oftentimes I was frustrated enough to just give up the P/Invoke interop approach and go ahead and write a C++/CLI bridge assembly instead (which has the interesting advantages and disadvantages which might be the subject of a future post :-)).

However, these days are over now.  In the January 2008 issue of the MSDN Magazine, Yi Zhang and Xiaoying Guo have published a CLR Inside Out column on marshaling between managed and unmanaged code.  In that column, they introduce the P/Invoke Interop Assistant, an automatic GUI and command-line utility for converting between managed and unmanaged signatures (in both directions).  This conversion, of course, is not limited just to Windows signatures; give the tool a snippet of your own C header files and it will dutifully convert them to pretty-printed C# [DllImport]‘s.

This tool has helped me already, and I plan to continue using it in my projects.  It also has the great advantage of working perfectly even if you’re not connected to the Internet.

It takes annoying long to load (about 30 seconds on my laptop), but when it’s loaded you can see the signature for most Windows APIs:


And of course translate your own code snippets:


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. ScottJanuary 25, 2008 ב 10:54 PM

    Thank you for posting *an actual link*. I’ve looked for this numerous times at the magazine website with no luck whatsoever. Now I finally have the tool.

  2. Keith PatrickMay 24, 2008 ב 12:43 AM

    One thing I wish it would do (unless it already does, but I couldn’t figure a way to) is to take in a .h filename instead of pasted snippets. I’ve got a 5k-line header I need to convert, and this tool will save days of conversion time for the file, there’s still some piecing together of snippets to recreate the single header file.

  3. Sasha GoldshteinJune 2, 2008 ב 10:31 PM

    Keith, I’m not sure that I would use the P/Invoke approach in this case. From my experience (I’ve written numerous interop solutions for customers), most of the time the managed side needs a relatively thin facade to the native API. And often enough, a thin C++/CLI facade is much more beneficial.

  4. David McClarnonMarch 29, 2009 ב 2:09 AM

    There is a new interop generator available at pinvoker.com. This will automatically convert the Win32 APIs or indeed any ‘C’ style C++ static dll.
    And it produces not source code but a final verifyable .NET interop dll.

    It uses a seperate marshalling assembly to provide functionality to the generated interop assembly but this is freely distributable.

    Please try it out and let me know what you think via the links on the site or via the support forums at http://www.pinvokerforums.com.

  5. GeoMan September 29, 2009 ב 7:40 PM

    Читаю Ваши статьи уже давно,очень интрестно пишите!Спасибо!Хороший ресурс.

  6. seoblogstApril 28, 2011 ב 5:43 AM

    mne ponravilsya messedG))))

  7. karlAugust 9, 2011 ב 1:04 PM

    Sorry,but i can’t find a link that i can download this tool,can someone help me?
    Sorry about my poor english.

  8. sektant23tmNovember 15, 2011 ב 5:22 PM

    Thank you for posting *an actual link*. I’ve looked for this numerous times at the magazine website with no luck whatsoever. Now I finally have the tool.

  9. SemibratovaMarch 28, 2012 ב 3:32 PM

    а вы отвечаете на комментарии на blogs.microsoft.co.il или просто их удаляете?

  10. Sasha GoldshteinApril 4, 2012 ב 10:44 AM

    @Semibratova: I try to answer all comments. There’s a plague of comment spam on this site, so sometimes comments might slip through the cracks, but I try my best.

  11. Alexandre Rocha Lima e MarcondesAugust 13, 2016 ב 3:57 PM

    Is there any plan to open-source the tool and/or let users contribute to the api mapping database?

    1. Sasha Goldshtein
      Sasha GoldshteinDecember 21, 2016 ב 11:03 AM

      I don’t know, I am not involved with its development. I very much doubt that it is still under active development.