【发布时间】:2011-02-06 17:46:07
【问题描述】:
我在一个 DLL 文件中有一个 C++ 函数(它是使用多字节字符集选项编译的):
_declspec(dllexport) void TestArray(char** OutBuff,int Count,int MaxLength)
{
for(int i=0;i<Count;i++)
{
char buff[25];
_itoa(i,buff,10);
strncpy(OutBuff[i],buff,MaxLength);
}
}
我想C#原型一定是下一个:
[DllImport("StringsScetch.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern void TestArray([MarshalAs(UnmanagedType.LPArray)] IntPtr[] OutBuff, int Count, int MaxLength);
但是我应该准备 IntPtr 对象来接收来自非托管代码的字符串吗?
【问题讨论】:
-
我也无法使用 StringBuilder[]。除了通过 Marshal.AllocHGlobal 编组之外,别无他法。
-
是的,这种方法很有效!非常感谢!
标签: c# c++ string marshalling