tags:

views:

193

answers:

2

I have a function in win32 dll with signature as:

void func1(int a, char*** outData)

int a --> input parameter
char*** outData --> output parameter - pointer to array of char strings


Any idea how to access this in C# using dll import & what should be the signature.

+2  A: 

For complicated types like triple pointers, I find the best approach is to go simple and marshal it as just a IntPtr

[DllImport("Some.dll")]
private static extern void func1(int a, out IntPtr ptr)

Once this function returns the IntPtr value will essentially represent a char**.

Using that value is nearly impossible though because we don't know the length. You'll need to alter your function signature to pass back out the length of the array before it can be used in managed code.

JaredPar
The array of char* might be terminated with a NULL pointer instead of storing the length explicitly.
Ben Voigt
Can you use a StringBuilder in any way, which usually is what you can pass in as a parameter?
Mikael Svenson
@Mikael: A StringBuilder can't handle multiple strings.
Ben Voigt
The Marshal class will allow you to retrieve the original char* from that char* array, and then turn each valid char* into a string. It may be complicated, but I don't think I'd agree with "nearly impossible".
Ben Voigt
@Ben the nearly impossible part refers to ability to determine the length of the array. If that is known then converting to a `string[]` is trivial. True it could be terminated with a `NULL` but that is not standard practice and since the OP didn't mention it I can't assume it to be true.
JaredPar
mavrick23
A: 

Hi, sorry for the late reply. was not well. Now for the problem.

Say suppose I change the function signature to return the length.

void func1(int a, char*** outData, int* len)

Then my signature in C# will be:

DllImport("Some.dll")]

private static extern void func1(int a, out IntPtr ptr, out int len)

Now how can I use this IntPtr to get the values. Need some code snippet. Sorry for directly asking for code snippet, but my problem is that I am comparatively newbie to C# & this weired problem. thanks for the help.

PS : I know I should not be posting any reply as Answer, but just wanted to make my comment explicitly visible.

mavrick23