471,605 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Marshalling an array of strings allocated in C to C#

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.

Nov 29 '05 #1
2 1872
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 29 '05 #2
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.

Dec 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Simon Schaap | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
4 posts views Thread by Simon Mansfield | last post: by
3 posts views Thread by David Mathog | last post: by
6 posts views Thread by alex323 | last post: by
1 post views Thread by Keaven Pineau | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.