Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

P/Invoke a purely C++ library?

Tags:

c++

.net

pinvoke

Is it possible to P/Invoke a pure C++ library, or does it have to be wrapped in C?

like image 691
joemoe Avatar asked Mar 01 '10 06:03

joemoe


2 Answers

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).

like image 123
Danny Varod Avatar answered Oct 26 '22 03:10

Danny Varod


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.

like image 23
Timores Avatar answered Oct 26 '22 03:10

Timores