Is there a tool that generates P/Invoke signatures for arbitrary unmanaged DLL?

22,026

Solution 1

Google quickly found http://www.pinvoker.com/ (Compatiblity listed as VS2005, 2008, and 2010; it doesn't seem to have been updated to work with newer versions)

Microsoft's C++/CLI compiler can also do this, if you use /clr:safe and #include the header file, it will generate p/invoke code which you can extract with e.g. ILSpy (free) or Red Gate Reflector (used to be free).

Solution 2

I use PInvoke Interop Assistant for unmanaged DLLs by using the third tab in the UI, marked "SigImp Translate Snippet". Simply copy-and-paste your header into the "Native Code Snippet" window and press Generate (or turn on Auto Generate). As an illustration here's some code from a question of mine. Note that for some reason errors don't appear in the Error panel but as comments at the top of the generated code.

As several people have already said, the generated code should be used as a guide - you may well have to make changes to get exactly what you want.

enter image description here

Solution 3

This project is active and looks promising for the task: https://github.com/mono/CppSharp

Solution 4

You can create C# wrapper for any native DLL including both C-style DLL exporting functions and C++ DLL exporting classes by using xInterop C++ .NET Bridge with .NET to Native C++ Bridge. It is available for free evaluation with some limitations.

Disclaimer: I am the author of xInterop C++ .NET Bridge.

Solution 5

Another alternative is the SharpGenTools. It is used by SharpDX to "automatically" create bindings of the directx api. There's also CppSharp, it's used by QtSharp to generate bindings to native C++/C libs. Until now CppSharp is only compatible with .Net Framework 6+.

Share:
22,026
GregC
Author by

GregC

Careers 2.0: http://careers.stackoverflow.com/gchernis LinkedIn: http://www.linkedin.com/in/gchernis

Updated on August 25, 2020

Comments

  • GregC
    GregC almost 4 years

    I stumbled upon a tool that generates P/Invoke signatures for Microsoft's own unmanaged DLLs: PInvoke Interop Assistant

    Is there a similar tool that will generate P/Invoke signatures for third-party unmanaged DLLs?

    Alternately, any way to feed a third-party DLL to PInvoke Interop Assistant

    EDIT: Actual issue I am trying to resolve

  • Ben Voigt
    Ben Voigt about 13 years
    @GregC: I appreciate that. It's the people who downvote me I'd like some explanation from.
  • AaronLbk
    AaronLbk about 13 years
    I would guess the downvotes are related to referencing that it was found via google. Sure, it probably doesn't add anything to the value of the answer but the answer itself is quite useful, so I personally choose to upvote instead.
  • IInspectable
    IInspectable about 10 years
    Dumpbin's output does not contain type information, since this information does not exist in the binary for functions that are exported as extern "C". C++ exports are exported using decorated names that Dumpbin can de-mangle. Still, not an answer to this question.
  • Artyom Sokolov
    Artyom Sokolov about 6 years
    Download link on the website displays "File Not Found"
  • Luigi Saggese
    Luigi Saggese almost 6 years
    It's no more possible to download this software. There are alternatives?
  • Ben Voigt
    Ben Voigt almost 6 years
    @LuigiSaggese: The Microsoft C++/CLI compiler is definitely still available.
  • Ben Voigt
    Ben Voigt almost 6 years
    @LuigiSaggese: And I'm pointing out that the alternative you asked about is already described in my answer, and was for seven whole years before you commented. Perhaps your comment didn't say what you actually meant?
  • Luigi Saggese
    Luigi Saggese almost 6 years
    @BenVoigt i want only to report that answer is outdated for the first part (normal after 7 years)
  • Ben Voigt
    Ben Voigt almost 6 years
    @LuigiSaggese: Alright, I added a note that should warn readers that it's out of date. As far as the ability to download it, I do see a server error from the main site but there are probably mirrors -- for example pinvoker-visual-studio-addin.software.informer.com)
  • gl3yn
    gl3yn about 5 years
    Looks like the link is dead now.
  • Dan Lewi Harkestad
    Dan Lewi Harkestad almost 5 years
    The code generated by CppSharp can be used on .NET Core 2, at least. You have to have .NET Framework or Mono to run the code generator.
  • trampster
    trampster over 3 years
    .NET is cross platform and c++/CLI is windows only, so this tool is no good for a lot of modern .NET.
  • GNZ
    GNZ over 2 years
    After requesting they send email "The download link for the requested software cannot be found." So please renew the link
  • GNZ
    GNZ over 2 years
    The link is dead
  • GNZ
    GNZ over 2 years
    Hey this is development I need the executable tool.
  • GNZ
    GNZ over 2 years
    Hello xInterop. I have a C API of a camera with a native DLL and header. There are around 20 functions I need to translate into C# for my WPF project. I tried by PInvoke but more than half are difficult for me. Is there a chance you may look at it?
  • GNZ
    GNZ over 2 years
    Using these tools are more complicated than using PInvoke. Why there no tool as input output fashion.
  • Francisco Neto
    Francisco Neto over 2 years
    @GNZ when you have to cover a huge api doing it by hand may not be an interesting choice. For example the Silk.Net guys covered a huge amount of native apis using tools to automate the creation of the bindings. There's no free launch. Everything that is simple will have limitations and usually more features generally implies more complexity. Choose your poison e be happy with it.