The topic of P/Invoke came up this morning in a discussion with Stephen Preston and Viru Aithal. Stephen mentioned a couple of interesting P/Invoke-related tools he'd stumbled upon, and Viru pointed me at an interesting technique he'd used for P/Invoking unmanaged member functions.

In this post I'll present the tools, in a later post I'll show Viru's code.

The first tool is the PInvoke Visual Studio Add-in from Red Gate Software (the UK-based provider of various development tools, including Reflector Pro). It appears to be a Visual Studio Add-in for VS2003 and 2005 (I can't see an update for VS2008 or 2010, which also means I can't try it). The tool provides simplified access to the excellent PInvoke.net website, to which I often refer when developing .NET apps. I don't actually use that many P/Invoke statements that I feel I need an Add-In, though – which is perhaps why the add-in doesn't appear to have been updated for more recent Visual Studio versions – but it seemed worth mentioning.

PInvoke Visual Studio Add-in menu itemPInvoke Visual Studio Add-in dialog

The second – in my opinion more interesting – tool is the P/Invoke Interop Assistant (here's a more in-depth article, the CodePlex hosting site and a Channel 9 interview with one of the authors). This tool generates P/Invoke signatures for use from C# and VB.NET, as well as the ability to determine the P/Invoke signatures used in compiled binaries:

  • Generating P/Invoke declarations while searching for commonly-used (presumably Win32) functions, structures, messages, etc.
    • This seems the more useful capability, in my opinion.

SigImp Search

  • Translating C code snippets into equivalent P/Invoke declarations.
    • May be useful, but not for most "pure" .NET developers.

SigImp Translate Snippet

  • Reverse-engineers the C/C++ method declaration used within managed binaries.
    • An interesting intellectual exercise, perhaps (and potentially interesting information for some), but I can't imagine needing to use this, myself.

SigExp

The tool has been around for a while, but I hope some of you find the information in this post useful (it was certainly news to me). Does anyone else use a P/Invoke tool they'd like to share with this blog's readership?

  1. Hi Kean,

    Thanks for the article. So far, I was in an opinion that PInvoking cannot be done for member functions as the initialisation of a type is not possible. So, I am using PInvoke only for global and C function calls (not sure about static functions).

    I was little surprised to read your blog saying "Viru pointed me at an interesting technique he’d used for P/Invoking unmanaged member functions". Can you invoke a member function without instantiating an object? I am eager to know how.

    Thanks,
    Narayanan

  2. It is possible, but you need an object instance... all will be revealed in the next post (or the one after).

    Kean

  3. J. Daniel Smith Avatar
    J. Daniel Smith

    My preference is to switch to C++/CLI once P/Invoke starts to get complicated; and I roughly define "complicated" as needing classes/structs from C/C++.

  4. David McClarnon Avatar

    You might also want to try out pinvoker addin at http://www.pinvoker.com.

    It's a free addin to visual studio which parses entire header files (even windows.h !) and then generates pinvoke signatures which can be inserted into your code.

  5. Hi, you may want to take a look at C# .NET PInvoke Interop SDK, it generates C# Wrapper for native C++ DLL exporting C++ classes.

Leave a Reply to Shawn Cancel reply

Your email address will not be published. Required fields are marked *