By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

Marshaling Nested Structs

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

P: n/a
"Morten Wennevik" <Mo************@hotmail.com> wrote in message news:
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.
Nov 16 '05 #3

P: n/a
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.

"MSDousti" <MS******@myrealbox.com> wrote in message
news:bf**************************@posting.google.c om...
"Morten Wennevik" <Mo************@hotmail.com> wrote in message news:
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.

Nov 16 '05 #4

P: n/a
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message > 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.
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.
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.