开发一个接口需要用C++调用C#的接口,总结一些自己在调用接口时遇到的问题以及解决方法与大家共同学习;
调用的引入
1.C++调用C#dll时需要使用#using<./*.dll>或者使用#using"./**.dll"进行包含所使用的dll;
2.使用using namespace 程序集(assembly)::类名(classname)进行引用程序集或类
3.声明引用对象时需要在变量之前添加^;
编译阶段
调用
在引入库的时候可以引入,但在运行时一直存在该问题,经过排查一部分是将所使用的dll放在C:\Windows\System32或Debug所在目录下,但无法成功,后将其放在启动应用的目录下可以运行。
对象定义与C++与C#字符串类型转换
using namespace system;
system::string ^ str =gcnew system::string("");
/将char*转换为System::String^
String^ str1= System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)ch1);
//System::String^转换为char*
char* ch2 = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str1);
Console::WriteLine(str1);