小编Ass*_*saf的帖子

从c#上的c ++获取字符串数组时出现System.OutOfMemoryException

我的C++功能

    void FillArray(wchar_t** arr)
    {
         // some code
         for(i= 0;i<end;++i)
         {
             wcsncpy(arr[i],InforArray[i],MaxLength);
             count++;
         } 
     }
Run Code Online (Sandbox Code Playgroud)

我的C#签名是

[DllImport("Native.dll", CharSet = CharSet.Unicode,EntryPoint = "FillArray")]
        internal static extern void FillArray(
            [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)] 
            IntPtr[] OutBuff);
Run Code Online (Sandbox Code Playgroud)

而C#代码本身:

int maxLen = 256;


int count = GetPropertyCount(ref eData);
IntPtr[] buffer = new IntPtr[count];
for (int i = 0; i < count; i++)
     buffer[i] = Marshal.AllocHGlobal(maxLen);

FillArray(buffer);

string[] output = new string[count];

for (int i = 0; i < count; i++)
{
      output[i] = Marshal.PtrToStringUni(buffer[i]); 
      Marshal.FreeHGlobal(buffer[i]);
}
Run Code Online (Sandbox Code Playgroud)

在c …

c# c++ dll interop exception

12
推荐指数
1
解决办法
1155
查看次数

标签 统计

c# ×1

c++ ×1

dll ×1

exception ×1

interop ×1