all groups > dotnet clr > june 2007 >
You're in the

dotnet clr

group:

Marshalling a self-referring structure (from C++ to C#)


Marshalling a self-referring structure (from C++ to C#) antuantuan
6/26/2007 1:30:23 PM
dotnet clr:
Hello there.

I'll try to describe shortly my problem, hoping you can help me to find a
solution to it.

I have a C function like this:

[DllImport("library")]
static extern message getMessage(....)

It returns a structure of this kind:

struct message
{
char *value;
int i_children;
message **children;
};

How am I supposed to map this struct in C# in order to access its children?

The only way I can call the function without getting any error is mapping it
this way:

[StructLayout(LayoutKind.Sequential)]
internal class message
{
internal string psz_name;
internal string psz_value;
internal Int32 i_child;
internal IntPtr child;
};

But then I have no idea how to access the children array. Moreover it works
only if I declare message as a class instead of a structure.

I admit my ignorance about the subjet, but I look forward to receiving your
helpful suggestions!

Re: Marshalling a self-referring structure (from C++ to C#) Mattias Sjögren
6/26/2007 11:20:43 PM

[quoted text, click to view]

Probably using Marshal.Copy and Marshal.PtrToStructure.



Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button