KazeXT
KazeXT

Reputation: 11

Returning list of null-terminated strings from external function into .NET

I am trying to call an external C++ function using VB.NET (answers in C# are fine, I can convert back) which returns a list of device names. This comes in the form of a pointer to a null-terminated array of null-terminated char arrays (correct me if I've misunderstood what I'm dealing with) with a function signature:

long GetNames(char*** names)

Most of the answers to similar questions I've found have involved something more like:

long GetNames(char** names, int length)

It appears from what I've read that I need to give it an IntPtr but I'm uncertain as to how this should be initialized, and in my case I don't know the length of the array. At the moment, in the VB module, I have:

<DllImport("MyExternal.dll", CallingConvention:=CallingConvention.Cdecl)> _
Function GetNames(ByRef names as IntPtr) As UInteger
End Function

I've tried calling the function by passing an IntPtr initialized as IntPtr.Zero or using Marshall.AllocHGlobal, but I always get an AccessViolationException.

Any ideas on how I should be calling this function and how I get the return value into a managed string array would be greatly appreciated.

EDIT: I've found a sample function call in native C as follows:

char **tmplist;
GetNames(&tmplist)

Upvotes: 1

Views: 443

Answers (1)

weismat
weismat

Reputation: 7411

* would mean Pointer to a pointer to a pointer, which is very uncommon.
I would expect that you are dealing with a pointer to pointer in c term which is a ByRef Call of a StringBuilder. If you do not receive the length you might need to do pointer arithmetics on your own until you find the null.

Upvotes: -1

Related Questions