问题
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