Marshal a double char pointer return value

家住魔仙堡 提交于 2020-01-24 19:52:04

问题


A C library I'm using has a function which returns a pointer to an array of char pointers:

extern char** getIds();

The example of use:

char **list, **list_save;
list_save = list = getIds();
while (list && *list)
{
    printf("Id: %s\n", *list);
    list++;
}
freeIds(list_save);

Where freeIds is also from the C library, and frees the memory allocated during getIds.

They provide a .Net Interop assembly too, but it doesn't import the getIds function. I have access to the interop assembly source code.

My program is in C# so I'd like to know if it possible to add a import to the interop assembly and what it should look like, as well as what my C# code must look like to call the function.

My guess for the dll import is:

[DllImport("foo.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr getIds();

But I haven't been able to get C# to call it, therefore I don't know if the dll import is correct.


回答1:


Have a look at Marshaling char**. Your dllimport should work.



来源:https://stackoverflow.com/questions/11572631/marshal-a-double-char-pointer-return-value

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