Declare array size in Struct?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robert.waters

    #1

    Declare array size in Struct?

    I have to parse a binary file having a number of fixed records, each
    record containing datas in fixed positions. I would like to parse
    this binary file into an array of structures having members that
    represent those fields, so that I can access the records in a
    meaningful way.
    Using C, I would have defined a struct that I could cast a byte array
    into, which held exactly one of these fixed records. The struct would
    be defined as such:
    struct rec {
    char index[1] ;
    char padding[50];
    char name[28];
    };
    No big deal, (rec)char_array ;.

    I tried to do something similar in C#, and I am getting an error
    telling me that:
    "Array size cannot be specified in a variable declaration"

    How might I create a data structure with fixed-size members at design
    time?
    Do I really have to encapsulate this into a class that contains logic
    to parse the record sequentially when it's instantiated?

    Thanks.
  • =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?=

    #2
    Re: Declare array size in Struct?

    Hi Robert,

    i think, you are looking for this (PInvoke Style):

    [StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi)
    public struct rec
    {
    [MarshalAs(Unman agedType.LPStr, SizeConst=1)]
    string index; //could also be a byte
    [MarshalAs(Unman agedType.LPStr, SizeConst=50)]
    string padding;//could also be a byte
    [MarshalAs(Unman agedType.LPStr, SizeConst=28)]
    string name;//could also be a byte
    }

    then you go like this:

    rec your_rec = (rec)
    Marshal.PtrToSt ructure(ptrPoin terToYourBytesB lockOfMemory,ty peof(rec));
    MessageBox.Show (your_rec.name) ;

    Regards

    Kerem

    --
    -----------------------
    Beste Grüsse / Best regards / Votre bien devoue
    Kerem Gümrükcü
    Latest Project: http://www.codeplex.com/restarts
    Latest Open-Source Projects: http://entwicklung.junetz.de
    -----------------------
    "This reply is provided as is, without warranty express or implied."
    "robert.wat ers" <robert.waters@ gmail.comschrie b im Newsbeitrag
    news:9b88c175-3ba3-4339-8957-aea3539110f8@t6 5g2000hsf.googl egroups.com...
    >I have to parse a binary file having a number of fixed records, each
    record containing datas in fixed positions. I would like to parse
    this binary file into an array of structures having members that
    represent those fields, so that I can access the records in a
    meaningful way.
    Using C, I would have defined a struct that I could cast a byte array
    into, which held exactly one of these fixed records. The struct would
    be defined as such:
    struct rec {
    char index[1] ;
    char padding[50];
    char name[28];
    };
    No big deal, (rec)char_array ;.
    >
    I tried to do something similar in C#, and I am getting an error
    telling me that:
    "Array size cannot be specified in a variable declaration"
    >
    How might I create a data structure with fixed-size members at design
    time?
    Do I really have to encapsulate this into a class that contains logic
    to parse the record sequentially when it's instantiated?
    >
    Thanks.

    Comment

    • robert.waters

      #3
      Re: Declare array size in Struct?

      On Oct 20, 2:10 am, Kerem Gümrükcü <kareem...@hotm ail.comwrote:
      Hi Robert,
      >
      i think, you are looking for this (PInvoke Style):
      >
      [StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi)
      public struct rec
      {
          [MarshalAs(Unman agedType.LPStr, SizeConst=1)]
          string index; //could also be a byte
          [MarshalAs(Unman agedType.LPStr, SizeConst=50)]
          string padding;//could also be a byte
          [MarshalAs(Unman agedType.LPStr, SizeConst=28)]
          string name;//could also be a byte
      >
      }
      >
      then you go like this:
      >
      rec your_rec = (rec)
      Marshal.PtrToSt ructure(ptrPoin terToYourBytesB lockOfMemory,ty peof(rec));
      MessageBox.Show (your_rec.name) ;
      >
      Regards
      >
      Kerem
      >
      Thank you, that's exactly what I needed.

      Comment

      • robert.waters

        #4
        Re: Declare array size in Struct?

        On Oct 20, 2:10 am, Kerem Gümrükcü <kareem...@hotm ail.comwrote:
        Hi Robert,
        >
        i think, you are looking for this (PInvoke Style):
        >
        [StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi)
        public struct rec
        {
            [MarshalAs(Unman agedType.LPStr, SizeConst=1)]
            string index; //could also be a byte
            [MarshalAs(Unman agedType.LPStr, SizeConst=50)]
            string padding;//could also be a byte
            [MarshalAs(Unman agedType.LPStr, SizeConst=28)]
            string name;//could also be a byte
        >
        }
        >
        then you go like this:
        >
        rec your_rec = (rec)
        Marshal.PtrToSt ructure(ptrPoin terToYourBytesB lockOfMemory,ty peof(rec));
        MessageBox.Show (your_rec.name) ;
        >
        I have a problem: I get an 'attempted to read protected memory"
        exception.
        [StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi)]
        public struct PbEntry // size=385b
        {
        [MarshalAs(Unman agedType.LPStr, SizeConst=1)]
        string index;
        [MarshalAs(Unman agedType.LPStr, SizeConst=5)]
        string nameHeader;
        [MarshalAs(Unman agedType.LPStr, SizeConst=123)]
        string contactName;
        [MarshalAs(Unman agedType.LPStr, SizeConst = 11)]
        string numsHeader;
        [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
        string cellNumber;
        [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
        string homeNumber;
        [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
        string workNumber;
        [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
        string otherNumber1;
        [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
        string otherNumber2;
        }

        ...... program and FileStream ('fs') set-up .....

        byte[] pbData = new byte[385];
        fs.Read(pbData, 0, 385);
        // pbData is now populated correctly
        PbEntry pb=new PbEntry();
        IntPtr ptr = Marshal.AllocHG lobal(pbData.Le ngth);
        try
        {
        Marshal.Copy(pb Data, 0, ptr, pbData.Length);
        pb = (PbEntry)Marsha l.PtrToStructur e(ptr,
        typeof(PbEntry) ); // exception happens here
        Console.WriteLi ne(pb.ToString( ));
        }
        finally
        {
        Marshal.FreeHGl obal(ptr);
        }

        Any reason why that is protected memory?

        Thanks!

        Comment

        • robert.waters

          #5
          Re: Declare array size in Struct?

          On Oct 20, 7:16 pm, "robert.wat ers" <robert.wat...@ gmail.comwrote:
          On Oct 20, 2:10 am, Kerem Gümrükcü <kareem...@hotm ail.comwrote:
          >
          >
          >
          Hi Robert,
          >
          i think, you are looking for this (PInvoke Style):
          >
          [StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi)
          public struct rec
          {
              [MarshalAs(Unman agedType.LPStr, SizeConst=1)]
              string index; //could also be a byte
              [MarshalAs(Unman agedType.LPStr, SizeConst=50)]
              string padding;//could also be a byte
              [MarshalAs(Unman agedType.LPStr, SizeConst=28)]
              string name;//could also be a byte
          >
          }
          >
          then you go like this:
          >
          rec your_rec = (rec)
          Marshal.PtrToSt ructure(ptrPoin terToYourBytesB lockOfMemory,ty peof(rec));
          MessageBox.Show (your_rec.name) ;
          >
          I have a problem: I get an 'attempted to read protected memory"
          exception.
              [StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Ansi)]
              public struct PbEntry  // size=385b
              {
                  [MarshalAs(Unman agedType.LPStr, SizeConst=1)]
                  string index;
                  [MarshalAs(Unman agedType.LPStr, SizeConst=5)]
                  string nameHeader;
                  [MarshalAs(Unman agedType.LPStr, SizeConst=123)]
                  string contactName;
                  [MarshalAs(Unman agedType.LPStr, SizeConst = 11)]
                  string numsHeader;
                  [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
                  string cellNumber;
                  [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
                  string homeNumber;
                  [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
                  string workNumber;
                  [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
                  string otherNumber1;
                  [MarshalAs(Unman agedType.LPStr, SizeConst = 49)]
                  string otherNumber2;
               }
          >
          ..... program and FileStream ('fs') set-up .....
          >
                          byte[] pbData = new byte[385];
                          fs.Read(pbData, 0, 385);
                          // pbData is now populated correctly
                          PbEntry pb=new PbEntry();
                          IntPtr ptr = Marshal.AllocHG lobal(pbData.Le ngth);
                          try
                          {
                              Marshal.Copy(pb Data, 0, ptr, pbData.Length);
                              pb = (PbEntry)Marsha l.PtrToStructur e(ptr,
          typeof(PbEntry) ); // exception happens here
                              Console.WriteLi ne(pb.ToString( ));
                          }
                          finally
                          {
                              Marshal.FreeHGl obal(ptr);
                          }
          >
          Any reason why that is protected memory?
          >
          Thanks!
          Replacing UnmanagedType.L PStr with UnmanagedType.B yValTStr for each
          struct member fixed this problem.
          Does anyone have any idea why?
          Note: the data inside the records had many nulls (\0), which I figured
          wouldn't be a problem because they typically follow strings of non-
          null characters, and so would fit the model of 'null-terminated ANSI
          character string'.
          Thank you for any input that would help.

          Comment

          Working...