Marshal an array of strings from C# to C code using p/invoke

£可爱£侵袭症+ 提交于 2019-11-28 13:00:18

You can simply declare your function in C# like this:

[DllImport(my_C_dll, CallingConvention=CallingConvention.Cdecl)]
static extern void print_string_array([In] string[] str_array, IntPtr length);

As it is written, your C++ code would appear to use the cdecl calling convention. So you may need to make the C# declaration match. I suspect that is the main problem you are facing.

Note also that size_t, which you use for the length parameter is 32 bits wide in a 32 bit process, and 64 bits wide in a 64 bit process. So the correct C# type is IntPtr. Personally I'd declare it to be int in both the C++ and C# code.


One final word of advice. When you encounter failures, include the error messages in your questions. I suspect your first code failed with this error:

A call to PInvoke function 'MyApp!MyApp.Program::print_string_array' has unbalanced the stack.

And if you had included that in the question it would have been a great help.

Your second attempt is quite close. Try the following:

string[] foo = {"testing", "one", "two", "three"};  
IntPtr[] s_array = new IntPtr[foo.Length];

for(int i = 0; i < foo.Length; ++i) 
{
    s_array[i] = Marshal.StringToHGlobalAnsi(foo[i])
}
GCHandle gH = GCHandle.Alloc(s_array, GCHandleType.Pinned);
print_string_array( gH.AddrOfPinnedObject(), s_array.Length);
gH.Free();
for(int i = 0; i < foo.Length; ++i) 
{
    Marshal.FreeHGlobal(s_array[i])
}

[DllImport(my_C_dll, CharSet = CharSet.Ansi)]
private static extern int print_string_array(IntPtr sa, int length);

Why the "print_string_array(IntPtr sa, int length);" instead of "print_string_array(IntPtr[] sa, int length);" ? And what does print_string_array look at the C-side? print_string_array(char** sa, int length); ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!