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 / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Using/converting legacy structs from a stream

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SethInMI - 25 Mar 2008 14:45 GMT
I am a total newb at .net, and I have not been able to search out a best
practice answer to what must be a common problem.

My app must process binary data from a UDP socket, a MSMQ queue and a file.  
In C, the data is in nested structs, with mixed types, floats, ints, char
arrays, int arrays, variable length arrays of structs etc.

My preference would be to access the data in a similar fashion to C, casting
the byte array of received data to the struct I want, and then sending out
the struct when it is time to transmit.

How can I format a struct to be able to mimic legacy structs, especially
with arrays?  I see how I can do it for marshalled code, with [ MarshalAs(
UnmanagedType.ByValArray, SizeConst=XX )] attribute, but does that only work
with unmanaged code?

Thanks,

Seth
John Vottero - 25 Mar 2008 19:35 GMT
>I am a total newb at .net, and I have not been able to search out a best
> practice answer to what must be a common problem.
[quoted text clipped - 14 lines]
> work
> with unmanaged code?

Define your structs using the StructLayout and MarshlAs attributes then use
Marshal.UnsafeAddrOfPinnedArrayElement() and Marshal.PtrToStructure() to
convert from an array of bytes to a managed object.  The Marshal class has a
number of useful methods.
SethInMI - 25 Mar 2008 20:09 GMT
I understand how to at least get started using the Marshal attributes in
defining my struct in C#, but I am still I think missing one step.  Does
marshalling work if no unmanaged code or memory is accessed?

here is my pseudo code, where myStruct is defined using LayoutSequential and
appropriate Marshalling attributes to mimic the layout of legacy C struct.

  Byte[] RawData = sock.Receive(ref RawAddr);
  If  (RawData.Length == Marshal.SizeOf(myStruct)  {
     Convert RawData into myStruct (using marshalling? just a cast?)
  }

> >I am a total newb at .net, and I have not been able to search out a best
> > practice answer to what must be a common problem.
[quoted text clipped - 19 lines]
> convert from an array of bytes to a managed object.  The Marshal class has a
> number of useful methods.
John Vottero - 26 Mar 2008 16:02 GMT
>I understand how to at least get started using the Marshal attributes in
> defining my struct in C#, but I am still I think missing one step.  Does
[quoted text clipped - 8 lines]
>      Convert RawData into myStruct (using marshalling? just a cast?)
>   }

To convert RawData into myStruct, you would do something like:

IntPtr adr = Marshal.UnsafeAddrOfPinnedArrayElement(RawData, 0);
MyStruct myStruct = Marshal.PtrToStructure(adr, typeof(MyStruct));

When you use the LayoutSequential and Marshaling attributes, you aren't
actually changing the structure, you're just providing instructions that are
used when marshaling the structure to/from unmanaged bytes.
SethInMI - 26 Mar 2008 16:29 GMT
Thanks John.  

You mentioned UnsafeAddrOfPinnedArrayElement in your first post, I just
didn't pick up how to use it.

> >I understand how to at least get started using the Marshal attributes in
> > defining my struct in C#, but I am still I think missing one step.  Does
[quoted text clipped - 17 lines]
> actually changing the structure, you're just providing instructions that are
> used when marshaling the structure to/from unmanaged bytes.
Ben Voigt [C++ MVP] - 26 Mar 2008 21:06 GMT
> I am a total newb at .net, and I have not been able to search out a
> best practice answer to what must be a common problem.
[quoted text clipped - 11 lines]
> with [ MarshalAs( UnmanagedType.ByValArray, SizeConst=XX )]
> attribute, but does that only work with unmanaged code?

Use the BitConverter class to convert each member, one at a time.

> Thanks,
>
> Seth

Rate this thread:







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.