Struct with array(s)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olav Langeland

    #1

    Struct with array(s)

    I am reading data to/from a file which contains C++ structures like this

    struct StructCPP
    {
    long lDgType;
    FILETIME ftDgTime;
    char cName[128];
    char cVersion[30];
    char cSpare[98];
    long lCount;
    }

    I am trying to access the data in the file using C# structures like this:

    struct StructCSharp1 // structure without array(s)
    {
    public uint DgType;
    public ulong DgTime;
    public int Count;
    };

    struct StructCSharp2 // structure with arrays
    {
    public uint DgType;
    public ulong DgTime;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=128)]
    public byte[] Name;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=30)]
    public byte[] Version;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=98)]
    public byte[] Spare;
    public int Count;
    };

    Trying to access the data in the structures:
    ....
    unsafe
    {
    fixed (byte* pb = &pBuf[0]) // pointing to somewhere in the file
    {
    StructCSharp1 dg1 = new StructCSharp1() ; // OK
    StructCSharp2 dg2 = new StructCSharp2() ; // OK
    StructCSharp1 dg11 = *(StructCSharp1 *)(pb); // OK
    StructCSharp2 dg22 = *(StructCSharp2 *)(pb);} // Fails
    }

    The last statement (dg 22 = ...) gives the compiler error:
    "Cannot take the address or size of a variable of a managed type"
    ('...\StructCSh arp2')

    Any suggestions?


  • Mattias Sjögren

    #2
    Re: Struct with array(s)

    Olav,
    [color=blue]
    >Any suggestions?[/color]

    Any reason you can't fill the structs member by member instead?



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Olav Langeland

      #3
      Re: Struct with array(s)

      Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:
      [color=blue]
      >Olav,
      >[color=green]
      >>Any suggestions?[/color]
      >
      >Any reason you can't fill the structs member by member instead?
      >
      >
      >
      >Mattias[/color]

      Mathias,

      Do you really mean like this?

      unsafe
      {
      fixed (byte* pb = &pBuf[12])
      {
      int nCount = *(UInt32*)(pb +128+128+128+30 +98);
      }
      }

      If not, please advise me.

      Comment

      • Mattias Sjögren

        #4
        Re: Struct with array(s)

        Olav,
        [color=blue]
        >Do you really mean like this?
        >
        >unsafe
        >{
        > fixed (byte* pb = &pBuf[12])
        > {
        > int nCount = *(UInt32*)(pb +128+128+128+30 +98);
        > }
        >}
        >
        >If not, please advise me.[/color]

        You don't have to use unsafe code for this. I'd do

        int nCount = pBuf[i] | pBuf[i+1] << 8 |
        pBuf[i+2] << 16 | pBuf[i+3] << 32;

        or use the BitConverter class.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org

        Please reply only to the newsgroup.

        Comment

        Working...