1.报错System.DllNotFoundException
编译c++库如果是x64,c#项目也选x64
c++

c#项目

2.报错System.BadImageFormatException
c#项目属性设置:目标平台x64

编译后,bin目录生成x64文件夹

拷贝c++库到此目录,c++如果依赖其它库也一并拷贝
3.System.AccessViolationException
c#传参数:string给c++函数,或接收返回字符串时,字符串需转换
c++的函数
std::string转char*
td::string str_data = "abcdefg";
char* charArr = new char[str_data.length() + 1];
strcpy(charArr, str_data.c_str());
char*转std:string
char* aa ="abdefg";
std::string str = aa;
char*可直接接收c# string
char* __stdcall ImgRotate(char* charstr, int degree)
{
std::string imgPath = charstr;
//const char* chardata = c_Obj.ImgRotate(imgPath, degree).c_str();
std::string str_data = c_Obj.ImgRotate(imgPath, degree);
char* charArr = new char[str_data.length() + 1];
strcpy(charArr, str_data.c_str());
// strcpy_s(charArr, sizeof(charArr), str_data.c_str());
return charArr;
}
c#调用
[DllImport("CdtxwVisionLibNet.dll")]
private static extern int Add(int n1, int n2);
[DllImport("CdtxwVisionLibNet.dll",EntryPoint = "ImgRotate",CharSet=CharSet.Ansi,CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ImgRotate(string path,int degree);
c#IntPtr对应c++的char*
调用方法:
