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.