1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c#调用c++ dll const char* String类型转换问题。传值 与接收返回值问题

c#调用c++ dll const char* String类型转换问题。传值 与接收返回值问题

时间:2019-11-01 13:25:16

相关推荐

c#调用c++   dll  const char*  String类型转换问题。传值 与接收返回值问题

C++原型

extern "C" __declspec(dllexport) const char* GetUnicoide(const char* gb2312){int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len+1];memset(str, 0, len+1);WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);if(wstr) delete[] wstr;return str;}

C#调用

[DllImport("strlen.dll", CallingConvention = CallingConvention.Cdecl)]extern static IntPtr GetUnicoide(string s);string a ="hello 123";IntPtr b = GetUnicoide(a);string c= Marshal.PtrToStringAnsi(b);

还有一种情况是C++ 中参数是 const char*我们在C# 中通常是用String进行传值。某些情况下,我们传的数据在C++指针地址里可以很好的表示,但是用C#的 String是表示不出来或表示不完整的,这时候就鸡肋了。。遇到这种情况IntPtr 是可以很好的表示的。。。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。