Using/converting legacy structs from a stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U2V0aEluTUk=?=

    Using/converting legacy structs from a stream

    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.B yValArray, SizeConst=XX )] attribute, but does that only work
    with unmanaged code?

    Thanks,

    Seth
  • John Vottero

    #2
    Re: Using/converting legacy structs from a stream

    "SethInMI" <SethInMI@discu ssions.microsof t.comwrote in message
    news:CA8B0658-E124-4BBB-B45A-CD6B3E5C0C68@mi crosoft.com...
    >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 LayoutSequentia l
    and
    appropriate Marshalling attributes to mimic the layout of legacy C struct.
    >
    Byte[] RawData = sock.Receive(re f RawAddr);
    If (RawData.Length == Marshal.SizeOf( myStruct) {
    Convert RawData into myStruct (using marshalling? just a cast?)
    }
    To convert RawData into myStruct, you would do something like:

    IntPtr adr = Marshal.UnsafeA ddrOfPinnedArra yElement(RawDat a, 0);
    MyStruct myStruct = Marshal.PtrToSt ructure(adr, typeof(MyStruct ));

    When you use the LayoutSequentia l 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.


    Comment

    • =?Utf-8?B?U2V0aEluTUk=?=

      #3
      Re: Using/converting legacy structs from a stream

      Thanks John.

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

      "John Vottero" wrote:
      "SethInMI" <SethInMI@discu ssions.microsof t.comwrote in message
      news:CA8B0658-E124-4BBB-B45A-CD6B3E5C0C68@mi crosoft.com...
      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 LayoutSequentia l
      and
      appropriate Marshalling attributes to mimic the layout of legacy C struct.

      Byte[] RawData = sock.Receive(re f RawAddr);
      If (RawData.Length == Marshal.SizeOf( myStruct) {
      Convert RawData into myStruct (using marshalling? just a cast?)
      }
      >
      To convert RawData into myStruct, you would do something like:
      >
      IntPtr adr = Marshal.UnsafeA ddrOfPinnedArra yElement(RawDat a, 0);
      MyStruct myStruct = Marshal.PtrToSt ructure(adr, typeof(MyStruct ));
      >
      When you use the LayoutSequentia l 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.
      >
      >

      Comment

      Working...