Groups | Blog | Home
all groups > dotnet interop > july 2003 >

dotnet interop : Struct with array of strings



Christoph Schittko [MVP XML]
7/28/2003 2:36:32 PM
All,

I am trying to write a P/invoke wrapper for a method that
takes a pointer to a struct that has an array of char*
strings.

It goes something like this:

Unmanaged Method:

int unmanaged_foo(
Struct1 * s1,
const char* class_name,
StringSet * instances
)
{
// ...
}

The managed definition is:
[DllImport( LIB_PATH, CharSet=CharSet.Ansi )]
public static extern int
unmanaged_foo(
ref Struct1 s1,
String class_name,
ref StringSet instances );


where the unmanaged definition of StringSet is:
struct StringSet
{
int size;
char ** elements;
}

Now I thought I should come up with the correct
parameters for the MarshalAs attribute on the elments
field, but so far that's been pretty much wishful
thinking. I've tried passing various UnmanagedTypes
values, LPArray, ByValArray and none whatsoever, but as
soon as I attach MarshalAs I get an exception:

Cannot marshal field elements of type foo: This type
cannot be marshaled as a structure field.

Now I found some postings on google that seem to suggest
that it is very much possible to do this. In fact, the
FrameworkSDK docs show an example as well.

If I don't attach a MarshalAs then my method call
actually makes it into the unmanaged method without
causing an exception. The int field of the StringSet
struct has the correct value, but the elements pointer
that's supposed to point to the first string in the array
points to laa-laa land.

I know that I will run into more issues marshalling the
string array back from the unamanaged method, but first I
would like to be able to call that method without getting
an exception AND the string array making it in there.

Can anybody point out to me what I am missing?

Any help would be greatly appreciated.
TIA,
Christoph Schittko
yirutang NO[at]SPAM microsoft.com
7/29/2003 9:40:17 PM
Have you tried
UnmanagedType.ByValTStr
It is supposed to be used for struct fields.


--------------------
[quoted text, click to view]
AddThis Social Bookmark Button