Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2003

Tip: Looking for answers? Try searching our database.

Marshaling structs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lars Roith - 24 Oct 2003 08:38 GMT
Hello everbody,

I want to marshal structs but I always recieve following error
"Can not marshal field ValueF of type STRUCT_B: This type can not be
marshaled as a structure field."

I have the following two structs:

typedef struct STRUCT_A
{
 int ValueA;
 char ValueB[64];
 int ValueC;
 int ValueD;
}

typedef struct STRUCT_B
{
 int  ValueE;
 STRUCT_A ValueF[128];
 int  ValueG;
}

and defined the in C# as

[StructLayout(LayoutKind.Sequential)]
public class STRUCT_A
{
 public Int32  ValueA;
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
 public string ValueB;
 public Int32  ValueC;
 public Int32  ValueD;
}

and

[StructLayout(LayoutKind.Sequential)]
public struct STRUCT_B
{
 public Int32  ValueE;
 [MarshalAs(UnmanagedType.ByValArray, SizeConst=128,
ArraySubType=UnmanagedType.Struct)]
 public STRUCT_A[] ValueF;
 public Int32  ValueG;
}

Can anyone tell me how to correctly marshal the structs?

Thanks in advance,

Lars
Mattias Sj?gren - 24 Oct 2003 11:10 GMT
Lars,

>Can anyone tell me how to correctly marshal the structs?

See if this anssers your question

http://www.dotnetinterop.com/faq/?q=StructWithStructArray

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Lars Roith - 24 Oct 2003 14:45 GMT
Mattias,

I was able to marshal the structure by hand using the description
at http://www.dotnetinterop.com/faq/default.aspx?q=VariableLengthStruct

Unfortunately I'm currently only able to marshal from IntPtr to structure.
The other way currently doesn't work. But I think it's is only a mistake in
the offsets.

Nevertheless thanks for the great help that save much time.

Lars
> Lars,
>
[quoted text clipped - 5 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.