2

My company uses a third-party C++ DLL which is updated periodically. I've been manually creating C# DLLImport statements, but in this last update the number of functions nearly doubled. Is there any tool (preferably free) that will create C# DLLImport's from an unmanaged C++ .dll or .lib file? (The DLL exports decorated C++ functions, not C++ class).

user206645
  • 51
  • 3

3 Answers3

2

Microsoft's Managed, Native, and COM Interop Team provides some tools on Codeplex, amongst them a tool called

P/Invoke Interop Assistant

I haven't tried it myself, but it looks as if it can do what you are looking for.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • Not what I need: "To use the tool, you either feed in an MSIL assemblies and get out C source for the proper unmanaged signatures or feed in C source with the unmanaged signature and get out proper VB/C#." – user206645 Mar 10 '10 at 12:18
  • @steve-weeks: As far as I know you will need the C/C++ sources because native DLLs don't store the signature of the exported functions (see http://stackoverflow.com/questions/386133/get-signatures-of-exported-functions-in-a-dll/386488#386488) and even a lib file will store the signature in a compiler specific way. – Dirk Vollmar Mar 10 '10 at 13:02
  • +1 divo @Steve-weeks Presumably you've been using C++ headers from the 3rd party to create the DLLImport statements? Just put those headers into the PInvoke Assistant and it will generat C# – MarkJ Mar 10 '10 at 13:56
0

As far as I know such a tool does not exist. You must write the P/Invoke declaration for each function yourself. I think its just as well since C++ functions are usually full of subtleties including in/out context, c-style strings, c-style arrays, pointers, references and what not. Many times the expected input/output can only be expressed by documentation and a tool cannot possibly output correct marshalling declarations for the functions.

logicnp
  • 5,796
  • 1
  • 28
  • 32
  • Some truth in this, but still a tool like the P/Invoke Interop Assistant can do 90% of the conversion automatically, saving you hours of tedious error prone drudgery. – MarkJ Mar 10 '10 at 13:58
-1

I'd write a C++/CLI wrapper.

Dan Byström
  • 9,067
  • 5
  • 38
  • 68