How to read binary data into a STRUCT?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Van Altena via .NET 247

    #1

    How to read binary data into a STRUCT?

    I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayou t" attribute with both LayoutKind.Expl icit andLayoutKind.S equential options. I can get this to worksuccessfull y, but only when I'm NOT dealing with arrays in thestructure definition.

    For example: In C, you are able to simply read a binary file intoa structure as follows:

    ---------------------------------------------------------------------------------------------------------
    /* The structure definition */
    typedef struct
    {
    short anShorts[5];
    char strString[32];
    double adDoubles[10];
    } FORMATDEF;

    /* Open the file */
    FILE * pFile;
    pFile = fopen("Test.bin ", "rb");

    /* Read into the structure */
    FORMATDEF structFormatDef ;
    fread(pFile, &structFormatDe f, sizeof(FORMATDE F));

    /* Close the file */
    fclose(pFile);
    ---------------------------------------------------------------------------------------------------------

    Note: The above example is using arrays in the STRUCT definition.This is the main root of the problem I am trying to solve. (i.e.this is like the StructLayout attribute with theLayoutKind.S equential option (Pack=8 in my particular problem) -but I can't get this to work with arrays in the structuredefini tion in C#).

    In C++ another possibility would be to use a UNION definitioninste ad of a class to union a structure definition with an arrayof characters (bytes), and then just read in the bytes andaccess the data through the structure. (i.e. this is like theStructLayout attribute with the LayoutKind.Expl icit option - butI can't get this to work with arrays in the structure definitionin C#).

    I can't believe with all the great features that C# has to offer,that there isn't a simple way to read in a binary file into aformatted structure and/or class. Can someone out there tell mewhat I'm missing?

    Thanks,

    Michael.

    --------------------------------
    From: Michael Van Altena

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>7dFE/rBp9kufEMK5ukIc hQ==</Id>
  • Cor Ligthert

    #2
    Re: How to read binary data into a STRUCT?

    Michael,
    Did you know that there is a newsgroup
    microsoft.publi c.dotnet.langua ges.csharp

    There is a possibility you get quicker an answer there,

    Cor

    "Michael Van Altena via .NET 247"

    I'm trying to figure out how to read a formatted binary file into a
    structure definition in C#. I've tried using the "StructLayo ut" attribute
    with both LayoutKind.Expl icit and LayoutKind.Sequ ential options. I can get
    this to work successfully, but only when I'm NOT dealing with arrays in the
    structure definition.

    For example: In C, you are able to simply read a binary file into a
    structure as follows:

    ----------------------------------------------------------------------------
    -----------------------------
    /* The structure definition */
    typedef struct
    {
    short anShorts[5];
    char strString[32];
    double adDoubles[10];
    } FORMATDEF;

    /* Open the file */
    FILE * pFile;
    pFile = fopen("Test.bin ", "rb");

    /* Read into the structure */
    FORMATDEF structFormatDef ;
    fread(pFile, &structFormatDe f, sizeof(FORMATDE F));

    /* Close the file */
    fclose(pFile);
    ----------------------------------------------------------------------------
    -----------------------------

    Note: The above example is using arrays in the STRUCT definition. This is
    the main root of the problem I am trying to solve. (i.e. this is like the
    StructLayout attribute with the LayoutKind.Sequ ential option (Pack=8 in my
    particular problem) - but I can't get this to work with arrays in the
    structure definition in C#).

    In C++ another possibility would be to use a UNION definition instead of a
    class to union a structure definition with an array of characters (bytes),
    and then just read in the bytes and access the data through the structure.
    (i.e. this is like the StructLayout attribute with the LayoutKind.Expl icit
    option - but I can't get this to work with arrays in the structure
    definition in C#).

    I can't believe with all the great features that C# has to offer, that there
    isn't a simple way to read in a binary file into a formatted structure
    and/or class. Can someone out there tell me what I'm missing?

    Thanks,

    Michael.

    --------------------------------
    From: Michael Van Altena

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>7dFE/rBp9kufEMK5ukIc hQ==</Id>


    Comment

    • Dennis Myrén

      #3
      Re: How to read binary data into a STRUCT?

      byte [] buff = new byte [Marshal.SizeOf( STRUCT)];
      IntPtr ptr = Marshal.AllocHG lobal(buff.Leng th);
      Marshal.Copy(bu ff, 0x0, ptr, buff.Length);
      STRUCT result = (STRUCT) Marshal.PtrToSt ructure(ptr, typeof(STRUCT)) ;
      Marshal.FreeHGl obal(ptr);

      [ StructLayout( LayoutKind.Sequ ential, Pack = 0x1 ) ]
      struct STRUCT
      {
      public ushort field1;
      [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 0x3C ) ]
      public byte [] field2;
      ...
      }

      --
      Regards,
      Dennis JD Myrén
      Oslo Kodebureau
      "Michael Van Altena via .NET 247" <anonymous@dotn et247.com> wrote in message
      news:OWJ957rnEH A.3428@TK2MSFTN GP11.phx.gbl...
      I'm trying to figure out how to read a formatted binary file into a
      structure definition in C#. I've tried using the "StructLayo ut" attribute
      with both LayoutKind.Expl icit and LayoutKind.Sequ ential options. I can get
      this to work successfully, but only when I'm NOT dealing with arrays in the
      structure definition.

      For example: In C, you are able to simply read a binary file into a
      structure as follows:

      ---------------------------------------------------------------------------------------------------------
      /* The structure definition */
      typedef struct
      {
      short anShorts[5];
      char strString[32];
      double adDoubles[10];
      } FORMATDEF;

      /* Open the file */
      FILE * pFile;
      pFile = fopen("Test.bin ", "rb");

      /* Read into the structure */
      FORMATDEF structFormatDef ;
      fread(pFile, &structFormatDe f, sizeof(FORMATDE F));

      /* Close the file */
      fclose(pFile);
      ---------------------------------------------------------------------------------------------------------

      Note: The above example is using arrays in the STRUCT definition. This is
      the main root of the problem I am trying to solve. (i.e. this is like the
      StructLayout attribute with the LayoutKind.Sequ ential option (Pack=8 in my
      particular problem) - but I can't get this to work with arrays in the
      structure definition in C#).

      In C++ another possibility would be to use a UNION definition instead of a
      class to union a structure definition with an array of characters (bytes),
      and then just read in the bytes and access the data through the structure.
      (i.e. this is like the StructLayout attribute with the LayoutKind.Expl icit
      option - but I can't get this to work with arrays in the structure
      definition in C#).

      I can't believe with all the great features that C# has to offer, that there
      isn't a simple way to read in a binary file into a formatted structure
      and/or class. Can someone out there tell me what I'm missing?

      Thanks,

      Michael.

      --------------------------------
      From: Michael Van Altena

      -----------------------
      Posted by a user from .NET 247 (http://www.dotnet247.com/)

      <Id>7dFE/rBp9kufEMK5ukIc hQ==</Id>


      Comment

      • Dennis Myrén

        #4
        Re: How to read binary data into a STRUCT?

        And of course the "buff" byte array has to be filled with the appropriate
        data.

        --
        Regards,
        Dennis JD Myrén
        Oslo Kodebureau
        "Dennis Myrén" <dennis@oslokb. no> wrote in message
        news:F9x3d.8048 $WW4.124390@new s4.e.nsc.no...[color=blue]
        > byte [] buff = new byte [Marshal.SizeOf( STRUCT)];
        > IntPtr ptr = Marshal.AllocHG lobal(buff.Leng th);
        > Marshal.Copy(bu ff, 0x0, ptr, buff.Length);
        > STRUCT result = (STRUCT) Marshal.PtrToSt ructure(ptr, typeof(STRUCT)) ;
        > Marshal.FreeHGl obal(ptr);
        >
        > [ StructLayout( LayoutKind.Sequ ential, Pack = 0x1 ) ]
        > struct STRUCT
        > {
        > public ushort field1;
        > [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 0x3C ) ]
        > public byte [] field2;
        > ...
        > }
        >
        > --
        > Regards,
        > Dennis JD Myrén
        > Oslo Kodebureau
        > "Michael Van Altena via .NET 247" <anonymous@dotn et247.com> wrote in
        > message news:OWJ957rnEH A.3428@TK2MSFTN GP11.phx.gbl...
        > I'm trying to figure out how to read a formatted binary file into a
        > structure definition in C#. I've tried using the "StructLayo ut" attribute
        > with both LayoutKind.Expl icit and LayoutKind.Sequ ential options. I can
        > get this to work successfully, but only when I'm NOT dealing with arrays
        > in the structure definition.
        >
        > For example: In C, you are able to simply read a binary file into a
        > structure as follows:
        >
        > ---------------------------------------------------------------------------------------------------------
        > /* The structure definition */
        > typedef struct
        > {
        > short anShorts[5];
        > char strString[32];
        > double adDoubles[10];
        > } FORMATDEF;
        >
        > /* Open the file */
        > FILE * pFile;
        > pFile = fopen("Test.bin ", "rb");
        >
        > /* Read into the structure */
        > FORMATDEF structFormatDef ;
        > fread(pFile, &structFormatDe f, sizeof(FORMATDE F));
        >
        > /* Close the file */
        > fclose(pFile);
        > ---------------------------------------------------------------------------------------------------------
        >
        > Note: The above example is using arrays in the STRUCT definition. This is
        > the main root of the problem I am trying to solve. (i.e. this is like the
        > StructLayout attribute with the LayoutKind.Sequ ential option (Pack=8 in my
        > particular problem) - but I can't get this to work with arrays in the
        > structure definition in C#).
        >
        > In C++ another possibility would be to use a UNION definition instead of a
        > class to union a structure definition with an array of characters (bytes),
        > and then just read in the bytes and access the data through the structure.
        > (i.e. this is like the StructLayout attribute with the LayoutKind.Expl icit
        > option - but I can't get this to work with arrays in the structure
        > definition in C#).
        >
        > I can't believe with all the great features that C# has to offer, that
        > there isn't a simple way to read in a binary file into a formatted
        > structure and/or class. Can someone out there tell me what I'm missing?
        >
        > Thanks,
        >
        > Michael.
        >
        > --------------------------------
        > From: Michael Van Altena
        >
        > -----------------------
        > Posted by a user from .NET 247 (http://www.dotnet247.com/)
        >
        > <Id>7dFE/rBp9kufEMK5ukIc hQ==</Id>
        >[/color]


        Comment

        Working...