P/调用一个纯c++库?

时间:2021-01-15 17:41:39

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

是否可以调用一个纯c++库,还是必须用C来包装?

2 个解决方案

#1


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

可以P /调用c++库,但您需要使用“取决于”找到支离破碎方法名称(如“@0 ! classname@classname@zz”)和实例方法使用“ThisCall”P / invoke调用协定,将实例的引用作为第一个参数传递(你可以存储IntPtr中的构造函数的结果)。

#2


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.

“纯”c++库的名称将被编译器打乱,因此很难使P/Invoke声明正确。C方法在开始时得到一个下划线,在c++中可能不存在。c++方法需要这个实例作为第一个参数,你必须自己给它。

I think that you need to wrap your C++ API in a C-compatible series of methods.

我认为您需要将c++ API封装在C兼容的一系列方法中。

#1


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

可以P /调用c++库,但您需要使用“取决于”找到支离破碎方法名称(如“@0 ! classname@classname@zz”)和实例方法使用“ThisCall”P / invoke调用协定,将实例的引用作为第一个参数传递(你可以存储IntPtr中的构造函数的结果)。

#2


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.

“纯”c++库的名称将被编译器打乱,因此很难使P/Invoke声明正确。C方法在开始时得到一个下划线,在c++中可能不存在。c++方法需要这个实例作为第一个参数,你必须自己给它。

I think that you need to wrap your C++ API in a C-compatible series of methods.

我认为您需要将c++ API封装在C兼容的一系列方法中。