Is it possible to P/Invoke a pure C++ library, or does it have to be wrapped in C?
Asked
Active
Viewed 1,525 times
2 Answers
2
C++ libraries can be P/invoked, but you'll need to use "depends" to find the mangled method names (names like "@0!classname@classname@zz") and for instance methods use "ThisCall" calling convention in the p/invoke and pass the reference of the instance as the first argument (you can store the result of the constructor within an IntPtr).

Danny Varod
- 17,324
- 5
- 69
- 111
-
5
-
3This is terrible advice. Even if it's technically possible, you'll run into many MANY problems trying to p/invoke into C++ classes. For example, constructors and destructors won't be called properly, .NET memory handling isn't compatible with C++ `new` and `delete`, and so on. C++/CLI is a much much better idea. – Ben Voigt Sep 18 '11 at 05:29
-
1The question was how to PInvoke which I answered. The create/delete issues can easily be overcome by find static create and delete methods to the C++ and calling them from the wrapping C# class. – Danny Varod Sep 18 '11 at 12:08
-
1@DannyVarod Your answer is indeed correct. The major differences between a C++ class method(non-pure-virtual-method, non-static-method) call and a C-style call are just what you have mentioned, 1. Mangled name, 2. Calling convention of "ThisCall" 3. Passing the reference of the instance as the first argument. I have written a tool to automate that. [PInvoke Interop SDK](http://www.xinterop.com/index.php/2013/04/13/introduction-to-c-pinvoke-interop-sdk/) – xInterop Apr 28 '13 at 19:30
-
Instead of usind `depends` to get the mangled name, you can also use `dumpbin /exports <...>.dll`. – Corbie Sep 01 '22 at 09:18
1
A "pure" C++ library will have its name mangled by the compiler, so it will be hard to get the P/Invoke declaration correct. And a C method gets an underscore at the beginning, which may not be there in C++. And a C++ method needs a this instance as a first parameter, you'd have to give it yourself.
I think that you need to wrap your C++ API in a C-compatible series of methods.

Timores
- 14,439
- 3
- 46
- 46
-
-
Of course, but I can't imagine a C++ library using only static methods. – Timores Mar 01 '10 at 06:47
-
-
@jalf, yes, I agree with you, as well as with 280Z28. But, and I apologize for insisting, can you imagine writing a C++ library and using only non-member methods ? – Timores Mar 01 '10 at 08:55
-
Yes. I can imagine a C++ library which uses member methods internally, but whose public interface is all non-member functions. Still, I agree with your point: Typical C++ libraries do expose member methods are part of their interface. – jalf Mar 01 '10 at 09:54
-
C++ methods have to be passed `this`, but the calling convention is not simply "put it into the first parameter". – Ben Voigt Sep 18 '11 at 05:30
-
@jalf: Normally the word "method" implies a non-static member. Others are called *functions* instead. – Ben Voigt Sep 18 '11 at 05:31
-
@Ben true, and if we want to be pedantic, C++ doesn't even *have* methods (the standard refers to member *functions*, not methods). I'm not sure why I said methods. I guess I was trying to use the same terminology as the answer I commented on. – jalf Sep 18 '11 at 09:24
-
@jalf: I know that, but *method* is a well-defined computer science term in the area of OOP, and in that sense C++ definitely does have them (although possibly only *virtual* member functions qualify). Just like C++ definitely has implementation inheritance, although the standard calls this a "derived class". – Ben Voigt Sep 18 '11 at 15:58