我的c代码是

void GetCharArray(char* arrayNew[5])
{
    arrayNew[0] = "Test";
    arrayNew[1] = "Test2";
    arrayNew[2] = "Test4";
    arrayNew[3] = "Test5";
    arrayNew[4] = "Test6";
}

extern "C" __declspec(dllexport) void GetCharArray(char* arrayNew[5]);

我想在C代码中用字符串填充数组
[DllImport(@"C:/Test.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void GetCharArray([MarshalAs(UnmanagedType.LPArray, SizeConst=5)] string[] sbOut);

List<string> testStr = new List<string>();
GetCharArray(test.ToArray());

我希望用C代码中的字符串填充我的testStr

最佳答案

找到解决办法

public static extern void GetCharArray(IntPtr[] results);

IntPtr[] pointers = new IntPtr[1000];
GetCharArray(pointers);
string[] results = new string[1000];
for (int i = 0; i < 1000; i++)
{
    results[i] = Marshal.PtrToStringAnsi(pointers[i]);
}

我希望它能帮助别人。

10-05 22:22