Marshaling Nested Structs

M

MSDousti

Hi all,

I read some Q&As in the Net, discussing Marshalization of nested
structs in C# (or VB.NET). Some guys stated that .NET framework does
not support this feature yet. Are they right?

BTW, by nested structs I mean structures such as this:



[StructLayout(LayoutKind.Sequential)]
struct InnerStruct
{
Int32 member1;
....
};
struct OuterStruct
{
Int32 member1;
....
[MarshalAs(UnmanagedType.ByValArray, SizeConst=15)]
InnerStruct[] innerStruct;
};



I tried marshaling the above code, and I receivied an error message
when I tried to get the size of OuterStruct using Marshal.SizeOf()
function.

Is there a way to work around this?
(If possible, please give more more than just some hints, 'coz I'm
totally new to marshaling)

Thnx in advanced
MSD.
 
M

Morten Wennevik

Hi MSDousti,

I'm far from any expert on Marshalling, but if you set the innerstruct member as a byte array you should be able to copy the data to an innerstruct using Marshal.Copy or the opposite if you prefer.
 
M

MSDousti

Morten Wennevik said:
I'm far from any expert on Marshalling, but if you set the innerstruct member as a byte array you should be able to copy the data to an innerstruct using Marshal.Copy or the opposite if you prefer.

Of course, but that'll be a pity if .NET does not support such
important (for API programming or so) structures.

Yours,
MSD.
 
W

Willy Denoyette [MVP]

The overall purpose of an OO development environment like .NET is to get rid
of the C-API programming paradigm in its supported languages, not to offer a
set of new languages for this same C-API programming style.
However, if you need this kind of interop support and you don't want to use
what's offered by the InteropServices.Marshal class to do your own
marshaling, .NET has excellent support , you just have to use the right
language tool which is MC++ or the upcoming C++/CLI binding.

Willy.
 
M

MSDousti

Willy Denoyette said:
of the C-API programming paradigm in its supported languages, not to offer a
set of new languages for this same C-API programming style.

Thnx Willy,
I want to use nested structs not only to call C-Style APIs, but to
read a file header (In fact, PE files). Some predefined structures
like "Directories" in PE file header, use nested structs (for more
info, u can look at "Optinal Header" in WINNT.H or PE_and_COFF
specification by MS).
However, if you need this kind of interop support and you don't want to use
what's offered by the InteropServices.Marshal class to do your own
marshaling, .NET has excellent support , you just have to use the right
language tool which is MC++ or the upcoming C++/CLI binding.

In fact I hate C/C++ style, and love C# (& VB.NET). It's so easy to
learn and program. So I'm seeking ways to port early codes to the new
style.

BTW: I wish the framework was installed on my WIN98/XP by default!
(Longhorn is said to support this feature)

OK, any suggestion for reading PE headers when encountering nested
structs?
thnx, anyway.
MSD.
 

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