C#调用C++ DLL 字符串处理
C#调用C++ DLL 直接传string 容易出现非法指针等问题,针对该问题做以下处理:
-
C# 传入 字符串到C++ DLL
[DllImportAttribute("CPLUS.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int setInfo(ref byte _Info); string str = "hello world\n"; Byte[] Info = System.Text.Encoding.Default.GetBytes(str); setInfo(Info );
-
C# 接收 C++ DLL 字符串
[DllImportAttribute("CPLUS.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int getInfo(ref byte info);
Byte[] Info = new Byte[2048];
int ret = getInfo(ref Info[0]);