Easiest way to generate P/Invoke code?
Asked Answered
H

2

22

I am an experienced .Net programer, but have not compiled a C/C++ program in my life. Now I have this C-dll, headers and documentation (3rd party, not from Win API), from which I need to call about ten methods.

I was thinking of using Platform Invoke. I found these three tools that would create the code for me:

and possibly

Pinvoker seems to have a bit different approach than the Interop assistant and the Wizard. Swig I just found when checking that this question has not been asked here.

What are the pros and cons of these tools?

What would be the best = easiest and safest way for me to produce the P/Invoke code given that I don't know much about C/C++?

Habakkuk answered 22/3, 2010 at 20:50 Comment(3)
mirror for P/Invoke Interop Assistant in the case codeplex is down: drive.google.com/file/d/0B_OGphRNB0qPcWU1QVFKLS1NeWs/…Comprador
Does this answer your question? Is there a tool that generates P/Invoke signatures for arbitrary unmanaged DLL?Stronghold
You may want to edit your post to let others know that the pinvoker link is currently owned by malicious actors.Reductive
C
3

See http://dotnetperls.com/dllimport-interop for an interop example, and this MSDN article for more info. The bible for this stuff is Adam Nathan's book.

Basically you will need to identify the functions in the DLL you want to call. These need to marked with extern to make them accessible to the outside world. The next step, which can get tricky is writing a DllImport for the function. This needs to map between the managed and unmanaged worlds. You will need to work out how to marshal any complex data structures from the C dll into managed code.

You should check to see if there is any sort of COM interface to the DLL. Make sure you really need to use P/Invoke first.

SWIG was originally for wrapping C/C++ code for use from scripting languages. It generates C# code from an interface description (see tutorial). I wouldn't recommend using it from C# if P/Invoke is an option.

Coakley answered 22/3, 2010 at 20:56 Comment(1)
Nope, no COM-interface. Need to call a native C-dll.Habakkuk
S
2

if the signatures of the functions are simple then it should take 10 minutes to setup - ie if they take 2 char * and return an int. Its only once they get complicated that it gets messy

The bible on pinvoke is Adam Nathans - .NET and COM: The Complete Interoperability Guide

And I agree with other poster - swig is not the right thing

Sialoid answered 22/3, 2010 at 21:9 Comment(4)
Interfaces are not simple: complex input and output structures. Or not super-complex, but 3-6 params in each call, some of them structures with several parameters. So I would really like the help in generation.Habakkuk
Can you post the function signatures here? You could obfuscate them to some degree.Coakley
I would need to check that. But the signatures (+ the related structs) are about 100 lines. Are you saying that these generation tools are somehow fundamentally not-good (if so why?). Or why should I do all that plubing by hand line-by-line and parameter-by-parameter instead of using these tools?Habakkuk
@Habakkuk Because these tools (swig) do not work on the original C++ header file, but on a special-language intermediate file that you must write, maintain, and generate the C++ header and .cs file from. So it's not natural and feels clumsy, compared to a tool that would just parse and convert the original C++ header.Acclimate

© 2022 - 2024 — McMap. All rights reserved.