首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c# 调用c++写的dll,该如何处理

2013-04-20 
c# 调用c++写的dll折腾了好几天。想用c# 调用c++写的dll,先上代码c++ 里如下 externC __declspec(dllexpo

c# 调用c++写的dll
折腾了好几天。想用c# 调用c++写的dll,先上代码
c++ 里如下
 extern  "C" __declspec(dllexport) int GetString(string*& a)
 { 
 a = new string[1];
 a[0] = "a|aa|aa3aa|";
  
 return 1;
 } 

c# 代码如下:

 
 [DllImport("RenderingPlugin.dll", EntryPoint = "GetString", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
    public static extern int GetString(ref IntPtr a);
    IntPtr a = new IntPtr();

 IntPtr current = new IntPtr(a.ToInt32() + IntPtr.Size);
 string temp;
 temp = Marshal.PtrToStringAnsi(current);
 print(temp);


这里能得到字符串 "a|aa|aa3aa|" ,但是长度有限,太长时,c++里大概超过10个字符时,就得到NULL,满足不了要求。请问如何才能得到长字符串数据,如200个字符长度。 C++ C# String dll
[解决办法]
当然拿不到,要用也得用char*,不能用string作参数
[解决办法]
C#部分还是用顶楼的
C++
extern  "C" __declspec(dllexport) int GetString(char*& a)
 { 
 a = "a
[解决办法]
aa
[解决办法]
aa3aa
[解决办法]
"; 
//或者 a = new char[20]; strcpy(a,"a
[解决办法]
aa
[解决办法]
aa3aa
[解决办法]
");
 return 1;
 } 
C#不懂
字符串大小根据ptr指针指向值去算,遇到asc码0结束
[解决办法]
1\c++中导出时,加上__stdcall
2\直接使用a="a
[解决办法]
aa
[解决办法]
"的形式,不要再new了
3\C#中使用StringBuilder试试

热点排行