Marshalling an array of strings allocated in C to C#

R

Rob

I have a function exposed from a C DLL which takes a pointer to an
array of strings as a param which then allocates that array of strings
and returns.

I'm using P/Invoke to call this function from C#, but I've been messing
with it for too long now and haven't got it working exactly as I'd
like.

Here's the C function prototype, which will fill in its parameters:
void GetStrings(LPTSTR **aStrings,int& iStrings);

And here's the C# version:
(Note that this is just the latest one I've been messing with - I've
tried dozens of combinations of different types - string[],
StringBuilder[], IntPtr[], with and without MarshalAs attributes...)
[DllImport("SomeDLL.dll")]
private static extern void GetStrings(ref IntPtr aStrings,ref int
iStrings);

So with this latest iteration I can get it to work, but it's not
'right'. Here's how I'm doing it:
int iStrings= 0;
IntPtr aStrings= IntPtr.Zero;
GetNames(ref aStrings,ref iStrings);
for (int index = 0;index < iStrings;++index)
{
IntPtr ptr = Marshal.ReadIntPtr(aStrings,4 * index);
string returnedString = Marshal.PtrToStringAuto(ptr);
}

Notice the hideous way of reading the IntPtrs using a hard-coded
offset?

The reason I've resorted to this is because all attempts at returning
an actual array of any type have failed. Well, not flat-out failed,
but they always return an array with exactly one element. I've seen it
written in other places that this is because the CLR has no way of
knowing how much data to marshal, so it just does the first piece.

So my question is, how can I do this a bit more correctly? How can I
return an actual array and have it come back with more than one
element?

At this point I'd actually even settle for a method that's more safe
than the one above, which feels very very hackish to me, so I'm
thinking surely there's a 'better' way.

Any help is appreciated,
Thanks.
 
M

Mattias Sjögren

At this point I'd actually even settle for a method that's more safe
than the one above, which feels very very hackish to me, so I'm
thinking surely there's a 'better' way.

There's nothing terribly wrong with your current solution. The only
change I's suggest is to replace the hardcoded 4 with IntPtr.Size.

In .NET 2.0 there's also a new Marshal.Copy overload that would let
you retrieve the entire array as an IntPtr[] in one call.

Other than that, I don't think you can make it much better without
using unsafe code.


Mattias
 
R

Rob

Thanks Mattias, if this is the best approach there seems to be then I'm
happy with it. Replacing the 4 with IntPtr.Size makes me feel better
about this method, thanks for pointing that one out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top