PtrToStructure for unknown structure?

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

    PtrToStructure for unknown structure?

    Hi,

    Here, sample code where a byte array is used to fill a particular structure:

    [...]

    fs = File.OpenRead(p ath); // FileStream

    BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
    b = new byte[(int)bfh.Size()];

    fs.Read(b, 0, b.Length);
    fixed (byte* pb = &b[0])
    bfh = (BITMAPFILEHEAD ER)Marshal.PtrT oStructure((Int Ptr)pb, typeof(BITMAPFI LEHEADER));

    [...]

    What I would like to have is a function capable of accepting an [in] byte array and an [out] unkown structure type.
    Is that possible?

    Thanks in advance,

    Carles
  • baretta

    #2
    Re: PtrToStructure for unknown structure?

    Using a generic method that takes a type parameter:
    public static unsafe T ByteArrayToStru cture<T( byte [ ] array )
    where T : struct
    {
    fixed ( byte* pb = &b [ 0 ] )
    return ( T ) Marshal.PtrToSt ructure( (IntPtr)pb, typeof(T));
    }

    If you are using unsafe anyway, you dont need to use Marshal if you dont
    want to,
    you could cast it old C++ style if you want. Probably more efficient

    Dennis Myren


    "carles" <carles.pv.2@gm ail.comwrote in message
    news:eotq$znuIH A.5096@TK2MSFTN GP02.phx.gbl...
    Hi,

    Here, sample code where a byte array is used to fill a particular structure:

    [...]

    fs = File.OpenRead(p ath); // FileStream

    BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
    b = new byte[(int)bfh.Size()];

    fs.Read(b, 0, b.Length);
    fixed (byte* pb = &b[0])
    bfh =
    (BITMAPFILEHEAD ER)Marshal.PtrT oStructure((Int Ptr)pb,
    typeof(BITMAPFI LEHEADER));

    [...]

    What I would like to have is a function capable of accepting an [in] byte
    array and an [out] unkown structure type.
    Is that possible?

    Thanks in advance,

    Carles


    Comment

    • carles

      #3
      Re: PtrToStructure for unknown structure?

      Thanks a lot,

      New on "C#" (and C)... could you please tell me how to do it old C++ style?

      Carles

      "baretta" <baretta2@gmail .comha escrit al missatge del grup de discussió:
      uxXXsCouIHA.396 @TK2MSFTNGP04.p hx.gbl...
      Using a generic method that takes a type parameter:
      public static unsafe T ByteArrayToStru cture<T( byte [ ] array )
      where T : struct
      {
      fixed ( byte* pb = &b [ 0 ] )
      return ( T ) Marshal.PtrToSt ructure( (IntPtr)pb, typeof(T));
      }
      >
      If you are using unsafe anyway, you dont need to use Marshal if you dont
      want to,
      you could cast it old C++ style if you want. Probably more efficient
      >
      Dennis Myren
      >
      >
      "carles" <carles.pv.2@gm ail.comwrote in message
      news:eotq$znuIH A.5096@TK2MSFTN GP02.phx.gbl...
      Hi,
      >
      Here, sample code where a byte array is used to fill a particular
      structure:
      >
      [...]
      >
      fs = File.OpenRead(p ath); // FileStream
      >
      BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
      b = new byte[(int)bfh.Size()];
      >
      fs.Read(b, 0, b.Length);
      fixed (byte* pb = &b[0])
      bfh =
      (BITMAPFILEHEAD ER)Marshal.PtrT oStructure((Int Ptr)pb,
      typeof(BITMAPFI LEHEADER));
      >
      [...]
      >
      What I would like to have is a function capable of accepting an [in] byte
      array and an [out] unkown structure type.
      Is that possible?
      >
      Thanks in advance,
      >
      Carles
      >

      Comment

      • baretta

        #4
        Re: PtrToStructure for unknown structure?

        I am sorry if i was misleading.


        Actually it is not possible to use a C-style approach,
        it would only work with structure whose types we know during compile time.

        This approach was what i was thinking about(without really thinking):
        public static unsafe T ByteArrayToStru cture<T( byte [ ] buffer )
        where T : struct
        {
        fixed ( byte* pBuff = buffer )
        {
        return *( T* ) pBuff;
        }
        }

        But actually, although we limit T to being a struct using where keyword,
        we cannot at this point guarantee that a pointer to this type can be
        obtained at runtime.
        It can only be obtained if a struct has only primitive fields, and this we
        cant guarantee at this point.
        If we would change the return value to a known struct made up completely of
        primitives it would compile.


        This one is totally managed. At least it compiles:
        public static T ByteArrayToStru cture<T( byte [ ] buffer )
        where T : struct
        {
        IntPtr ptr = Marshal.AllocHG lobal ( buffer.Length );
        Marshal.Copy ( buffer, 0x0, ptr, buffer.Length );
        T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );

        Marshal.FreeHGl obal ( ptr );
        return ret;
        }

        and you will use it like this:
        BITMAPFILEHEADE R header = ByteArrayToStru cture<BITMAPFIL EHEADER(
        yourByteArray );


        I would recommend modifying ByteArrayToStru cture, so that it takes the
        System.IO.Strea m as parameter rather than the byte array.
        Otherwise, it is assumed that the buffer passed in is of the correct size
        already(which is the size of the struct).
        It is better if this is handled by the function.

        So, this is the final result:
        public static T ReadStructure<T ( Stream stream )
        where T : struct
        {
        byte [ ] buff = new byte [ Marshal.SizeOf ( typeof ( T ) ) ];
        IntPtr ptr = Marshal.AllocHG lobal ( buff.Length );
        Marshal.Copy ( buff, 0x0, ptr, buff.Length );
        T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );

        Marshal.FreeHGl obal ( ptr );
        return ret;
        }


        Dennis Myren


        "carles" <carles.pv.2@gm ail.comskrev i melding
        news:eK7BnXouIH A.4476@TK2MSFTN GP06.phx.gbl...
        Thanks a lot,
        >
        New on "C#" (and C)... could you please tell me how to do it old C++
        style?
        >
        Carles
        >
        "baretta" <baretta2@gmail .comha escrit al missatge del grup de
        discussió: uxXXsCouIHA.396 @TK2MSFTNGP04.p hx.gbl...
        >Using a generic method that takes a type parameter:
        >public static unsafe T ByteArrayToStru cture<T( byte [ ] array )
        > where T : struct
        >{
        > fixed ( byte* pb = &b [ 0 ] )
        > return ( T ) Marshal.PtrToSt ructure( (IntPtr)pb, typeof(T));
        >}
        >>
        >If you are using unsafe anyway, you dont need to use Marshal if you dont
        >want to,
        >you could cast it old C++ style if you want. Probably more efficient
        >>
        >Dennis Myren
        >>
        >>
        >"carles" <carles.pv.2@gm ail.comwrote in message
        >news:eotq$znuI HA.5096@TK2MSFT NGP02.phx.gbl.. .
        >Hi,
        >>
        >Here, sample code where a byte array is used to fill a particular
        >structure:
        >>
        > [...]
        >>
        > fs = File.OpenRead(p ath); // FileStream
        >>
        > BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
        > b = new byte[(int)bfh.Size()];
        >>
        > fs.Read(b, 0, b.Length);
        > fixed (byte* pb = &b[0])
        > bfh =
        >(BITMAPFILEHEA DER)Marshal.Ptr ToStructure((In tPtr)pb,
        >typeof(BITMAPF ILEHEADER));
        >>
        > [...]
        >>
        >What I would like to have is a function capable of accepting an [in] byte
        >array and an [out] unkown structure type.
        >Is that possible?
        >>
        >Thanks in advance,
        >>
        >Carles
        >>
        >
        >

        Comment

        • carles

          #5
          Re: PtrToStructure for unknown structure?

          Thanks again.
          Really enlightening answer.

          One thing: Marshal.SizeOf( ) seems to return 32-bit (depends on what?)
          aligned sizes.
          In my case, I sequentially read several structures from file, so I need to
          exactly read structure byte-lenghts (ie, BITMAPFILEHEADE R is 14
          byte-lenght). I decided to add a static Size() function for each structure
          definition, returning its byte-lenght size. Error handling is done at time
          to read from file.

          So, another question could be next: how to determine byte-length size of the
          passed (unknown) structure. This would allow to compact ByteArrayToStru cture
          to your suggested final approach, sizing buffer and reading from stream
          inside same function.

          Carles

          "baretta" <baretta2@gmail .comha escrit al missatge del grup de discussió:
          OzMmUdpuIHA.576 @TK2MSFTNGP05.p hx.gbl...
          >I am sorry if i was misleading.
          >
          >
          Actually it is not possible to use a C-style approach,
          it would only work with structure whose types we know during compile time.
          >
          This approach was what i was thinking about(without really thinking):
          public static unsafe T ByteArrayToStru cture<T( byte [ ] buffer )
          where T : struct
          {
          fixed ( byte* pBuff = buffer )
          {
          return *( T* ) pBuff;
          }
          }
          >
          But actually, although we limit T to being a struct using where keyword,
          we cannot at this point guarantee that a pointer to this type can be
          obtained at runtime.
          It can only be obtained if a struct has only primitive fields, and this we
          cant guarantee at this point.
          If we would change the return value to a known struct made up completely
          of primitives it would compile.
          >
          >
          This one is totally managed. At least it compiles:
          public static T ByteArrayToStru cture<T( byte [ ] buffer )
          where T : struct
          {
          IntPtr ptr = Marshal.AllocHG lobal ( buffer.Length );
          Marshal.Copy ( buffer, 0x0, ptr, buffer.Length );
          T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );
          >
          Marshal.FreeHGl obal ( ptr );
          return ret;
          }
          >
          and you will use it like this:
          BITMAPFILEHEADE R header = ByteArrayToStru cture<BITMAPFIL EHEADER(
          yourByteArray );
          >
          >
          I would recommend modifying ByteArrayToStru cture, so that it takes the
          System.IO.Strea m as parameter rather than the byte array.
          Otherwise, it is assumed that the buffer passed in is of the correct size
          already(which is the size of the struct).
          It is better if this is handled by the function.
          >
          So, this is the final result:
          public static T ReadStructure<T ( Stream stream )
          where T : struct
          {
          byte [ ] buff = new byte [ Marshal.SizeOf ( typeof ( T ) ) ];
          IntPtr ptr = Marshal.AllocHG lobal ( buff.Length );
          Marshal.Copy ( buff, 0x0, ptr, buff.Length );
          T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );
          >
          Marshal.FreeHGl obal ( ptr );
          return ret;
          }
          >
          >
          Dennis Myren
          >
          >
          "carles" <carles.pv.2@gm ail.comskrev i melding
          news:eK7BnXouIH A.4476@TK2MSFTN GP06.phx.gbl...
          >Thanks a lot,
          >>
          >New on "C#" (and C)... could you please tell me how to do it old C++
          >style?
          >>
          >Carles
          >>
          >"baretta" <baretta2@gmail .comha escrit al missatge del grup de
          >discussió: uxXXsCouIHA.396 @TK2MSFTNGP04.p hx.gbl...
          >>Using a generic method that takes a type parameter:
          >>public static unsafe T ByteArrayToStru cture<T( byte [ ] array )
          >> where T : struct
          >>{
          >> fixed ( byte* pb = &b [ 0 ] )
          >> return ( T ) Marshal.PtrToSt ructure( (IntPtr)pb, typeof(T));
          >>}
          >>>
          >>If you are using unsafe anyway, you dont need to use Marshal if you dont
          >>want to,
          >>you could cast it old C++ style if you want. Probably more efficient
          >>>
          >>Dennis Myren
          >>>
          >>>
          >>"carles" <carles.pv.2@gm ail.comwrote in message
          >>news:eotq$znu IHA.5096@TK2MSF TNGP02.phx.gbl. ..
          >>Hi,
          >>>
          >>Here, sample code where a byte array is used to fill a particular
          >>structure:
          >>>
          >> [...]
          >>>
          >> fs = File.OpenRead(p ath); // FileStream
          >>>
          >> BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
          >> b = new byte[(int)bfh.Size()];
          >>>
          >> fs.Read(b, 0, b.Length);
          >> fixed (byte* pb = &b[0])
          >> bfh =
          >>(BITMAPFILEHE ADER)Marshal.Pt rToStructure((I ntPtr)pb,
          >>typeof(BITMAP FILEHEADER));
          >>>
          >> [...]
          >>>
          >>What I would like to have is a function capable of accepting an [in]
          >>byte array and an [out] unkown structure type.
          >>Is that possible?
          >>>
          >>Thanks in advance,
          >>>
          >>Carles
          >>>
          >>
          >>
          >

          Comment

          • carles

            #6
            Re: PtrToStructure for unknown structure?

            At structure definition, for example:

            [StructLayout(La youtKind.Sequen tial, Pack=1)]
            private struct BITMAPFILEHEADE R
            {
            public ushort bfType;
            public uint bfSize;
            public short bfReserved1;
            public short bfReserved2;
            public uint bfOffBits;
            }

            adding that 'Pack=1 specification', no padding is done, and 'packed' size is
            returned, 14.
            Hope nothing more has to be taken into account.

            Carles


            "carles" <carles.pv.2@gm ail.comha escrit al missatge del grup de
            discussió: %23tiPVIyuIHA.4 848@TK2MSFTNGP0 5.phx.gbl...
            Thanks again.
            Really enlightening answer.
            >
            One thing: Marshal.SizeOf( ) seems to return 32-bit (depends on what?)
            aligned sizes.
            In my case, I sequentially read several structures from file, so I need to
            exactly read structure byte-lenghts (ie, BITMAPFILEHEADE R is 14
            byte-lenght). I decided to add a static Size() function for each structure
            definition, returning its byte-lenght size. Error handling is done at time
            to read from file.
            >
            So, another question could be next: how to determine byte-length size of
            the passed (unknown) structure. This would allow to compact
            ByteArrayToStru cture to your suggested final approach, sizing buffer and
            reading from stream inside same function.
            >
            Carles
            >
            "baretta" <baretta2@gmail .comha escrit al missatge del grup de
            discussió: OzMmUdpuIHA.576 @TK2MSFTNGP05.p hx.gbl...
            >>I am sorry if i was misleading.
            >>
            >>
            >Actually it is not possible to use a C-style approach,
            >it would only work with structure whose types we know during compile
            >time.
            >>
            >This approach was what i was thinking about(without really thinking):
            >public static unsafe T ByteArrayToStru cture<T( byte [ ] buffer )
            > where T : struct
            >{
            > fixed ( byte* pBuff = buffer )
            > {
            > return *( T* ) pBuff;
            > }
            >}
            >>
            >But actually, although we limit T to being a struct using where keyword,
            >we cannot at this point guarantee that a pointer to this type can be
            >obtained at runtime.
            >It can only be obtained if a struct has only primitive fields, and this
            >we cant guarantee at this point.
            >If we would change the return value to a known struct made up completely
            >of primitives it would compile.
            >>
            >>
            >This one is totally managed. At least it compiles:
            >public static T ByteArrayToStru cture<T( byte [ ] buffer )
            > where T : struct
            >{
            > IntPtr ptr = Marshal.AllocHG lobal ( buffer.Length );
            > Marshal.Copy ( buffer, 0x0, ptr, buffer.Length );
            > T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );
            >>
            > Marshal.FreeHGl obal ( ptr );
            > return ret;
            >}
            >>
            >and you will use it like this:
            >BITMAPFILEHEAD ER header = ByteArrayToStru cture<BITMAPFIL EHEADER(
            >yourByteArra y );
            >>
            >>
            >I would recommend modifying ByteArrayToStru cture, so that it takes the
            >System.IO.Stre am as parameter rather than the byte array.
            >Otherwise, it is assumed that the buffer passed in is of the correct size
            >already(whic h is the size of the struct).
            >It is better if this is handled by the function.
            >>
            >So, this is the final result:
            >public static T ReadStructure<T ( Stream stream )
            > where T : struct
            >{
            > byte [ ] buff = new byte [ Marshal.SizeOf ( typeof ( T ) ) ];
            > IntPtr ptr = Marshal.AllocHG lobal ( buff.Length );
            > Marshal.Copy ( buff, 0x0, ptr, buff.Length );
            > T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );
            >>
            > Marshal.FreeHGl obal ( ptr );
            > return ret;
            >}
            >>
            >>
            >Dennis Myren
            >>
            >>
            >"carles" <carles.pv.2@gm ail.comskrev i melding
            >news:eK7BnXouI HA.4476@TK2MSFT NGP06.phx.gbl.. .
            >>Thanks a lot,
            >>>
            >>New on "C#" (and C)... could you please tell me how to do it old C++
            >>style?
            >>>
            >>Carles
            >>>
            >>"baretta" <baretta2@gmail .comha escrit al missatge del grup de
            >>discussió: uxXXsCouIHA.396 @TK2MSFTNGP04.p hx.gbl...
            >>>Using a generic method that takes a type parameter:
            >>>public static unsafe T ByteArrayToStru cture<T( byte [ ] array )
            >>> where T : struct
            >>>{
            >>> fixed ( byte* pb = &b [ 0 ] )
            >>> return ( T ) Marshal.PtrToSt ructure( (IntPtr)pb, typeof(T));
            >>>}
            >>>>
            >>>If you are using unsafe anyway, you dont need to use Marshal if you
            >>>dont want to,
            >>>you could cast it old C++ style if you want. Probably more efficient
            >>>>
            >>>Dennis Myren
            >>>>
            >>>>
            >>>"carles" <carles.pv.2@gm ail.comwrote in message
            >>>news:eotq$zn uIHA.5096@TK2MS FTNGP02.phx.gbl ...
            >>>Hi,
            >>>>
            >>>Here, sample code where a byte array is used to fill a particular
            >>>structure:
            >>>>
            >>> [...]
            >>>>
            >>> fs = File.OpenRead(p ath); // FileStream
            >>>>
            >>> BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
            >>> b = new byte[(int)bfh.Size()];
            >>>>
            >>> fs.Read(b, 0, b.Length);
            >>> fixed (byte* pb = &b[0])
            >>> bfh =
            >>>(BITMAPFILEH EADER)Marshal.P trToStructure(( IntPtr)pb,
            >>>typeof(BITMA PFILEHEADER));
            >>>>
            >>> [...]
            >>>>
            >>>What I would like to have is a function capable of accepting an [in]
            >>>byte array and an [out] unkown structure type.
            >>>Is that possible?
            >>>>
            >>>Thanks in advance,
            >>>>
            >>>Carles
            >>>>
            >>>
            >>>
            >>
            >
            >

            Comment

            • baretta

              #7
              Re: PtrToStructure for unknown structure?

              That is correct, i have used both Marshal.SizeOf and the unsafe keyword
              sizeof myself to retrieve sizes of structures correctly.
              All my structures has the Pack=1 property set.

              You should be OK.




              "carles" <carles.pv.2@gm ail.comwrote in message
              news:uXjxbvyuIH A.4952@TK2MSFTN GP05.phx.gbl...
              At structure definition, for example:
              >
              [StructLayout(La youtKind.Sequen tial, Pack=1)]
              private struct BITMAPFILEHEADE R
              {
              public ushort bfType;
              public uint bfSize;
              public short bfReserved1;
              public short bfReserved2;
              public uint bfOffBits;
              }
              >
              adding that 'Pack=1 specification', no padding is done, and 'packed' size
              is returned, 14.
              Hope nothing more has to be taken into account.
              >
              Carles
              >
              >
              "carles" <carles.pv.2@gm ail.comha escrit al missatge del grup de
              discussió: %23tiPVIyuIHA.4 848@TK2MSFTNGP0 5.phx.gbl...
              >Thanks again.
              >Really enlightening answer.
              >>
              >One thing: Marshal.SizeOf( ) seems to return 32-bit (depends on what?)
              >aligned sizes.
              >In my case, I sequentially read several structures from file, so I need
              >to exactly read structure byte-lenghts (ie, BITMAPFILEHEADE R is 14
              >byte-lenght). I decided to add a static Size() function for each
              >structure definition, returning its byte-lenght size. Error handling is
              >done at time to read from file.
              >>
              >So, another question could be next: how to determine byte-length size of
              >the passed (unknown) structure. This would allow to compact
              >ByteArrayToStr ucture to your suggested final approach, sizing buffer and
              >reading from stream inside same function.
              >>
              >Carles
              >>
              >"baretta" <baretta2@gmail .comha escrit al missatge del grup de
              >discussió: OzMmUdpuIHA.576 @TK2MSFTNGP05.p hx.gbl...
              >>>I am sorry if i was misleading.
              >>>
              >>>
              >>Actually it is not possible to use a C-style approach,
              >>it would only work with structure whose types we know during compile
              >>time.
              >>>
              >>This approach was what i was thinking about(without really thinking):
              >>public static unsafe T ByteArrayToStru cture<T( byte [ ] buffer )
              >> where T : struct
              >>{
              >> fixed ( byte* pBuff = buffer )
              >> {
              >> return *( T* ) pBuff;
              >> }
              >>}
              >>>
              >>But actually, although we limit T to being a struct using where keyword,
              >>we cannot at this point guarantee that a pointer to this type can be
              >>obtained at runtime.
              >>It can only be obtained if a struct has only primitive fields, and this
              >>we cant guarantee at this point.
              >>If we would change the return value to a known struct made up completely
              >>of primitives it would compile.
              >>>
              >>>
              >>This one is totally managed. At least it compiles:
              >>public static T ByteArrayToStru cture<T( byte [ ] buffer )
              >> where T : struct
              >>{
              >> IntPtr ptr = Marshal.AllocHG lobal ( buffer.Length );
              >> Marshal.Copy ( buffer, 0x0, ptr, buffer.Length );
              >> T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );
              >>>
              >> Marshal.FreeHGl obal ( ptr );
              >> return ret;
              >>}
              >>>
              >>and you will use it like this:
              >>BITMAPFILEHEA DER header = ByteArrayToStru cture<BITMAPFIL EHEADER(
              >>yourByteArr ay );
              >>>
              >>>
              >>I would recommend modifying ByteArrayToStru cture, so that it takes the
              >>System.IO.Str eam as parameter rather than the byte array.
              >>Otherwise, it is assumed that the buffer passed in is of the correct
              >>size already(which is the size of the struct).
              >>It is better if this is handled by the function.
              >>>
              >>So, this is the final result:
              >>public static T ReadStructure<T ( Stream stream )
              >> where T : struct
              >>{
              >> byte [ ] buff = new byte [ Marshal.SizeOf ( typeof ( T ) ) ];
              >> IntPtr ptr = Marshal.AllocHG lobal ( buff.Length );
              >> Marshal.Copy ( buff, 0x0, ptr, buff.Length );
              >> T ret = ( T ) Marshal.PtrToSt ructure ( ptr, typeof ( T ) );
              >>>
              >> Marshal.FreeHGl obal ( ptr );
              >> return ret;
              >>}
              >>>
              >>>
              >>Dennis Myren
              >>>
              >>>
              >>"carles" <carles.pv.2@gm ail.comskrev i melding
              >>news:eK7BnXou IHA.4476@TK2MSF TNGP06.phx.gbl. ..
              >>>Thanks a lot,
              >>>>
              >>>New on "C#" (and C)... could you please tell me how to do it old C++
              >>>style?
              >>>>
              >>>Carles
              >>>>
              >>>"baretta" <baretta2@gmail .comha escrit al missatge del grup de
              >>>discussió: uxXXsCouIHA.396 @TK2MSFTNGP04.p hx.gbl...
              >>>>Using a generic method that takes a type parameter:
              >>>>public static unsafe T ByteArrayToStru cture<T( byte [ ] array )
              >>>> where T : struct
              >>>>{
              >>>> fixed ( byte* pb = &b [ 0 ] )
              >>>> return ( T ) Marshal.PtrToSt ructure( (IntPtr)pb, typeof(T));
              >>>>}
              >>>>>
              >>>>If you are using unsafe anyway, you dont need to use Marshal if you
              >>>>dont want to,
              >>>>you could cast it old C++ style if you want. Probably more efficient
              >>>>>
              >>>>Dennis Myren
              >>>>>
              >>>>>
              >>>>"carles" <carles.pv.2@gm ail.comwrote in message
              >>>>news:eotq$z nuIHA.5096@TK2M SFTNGP02.phx.gb l...
              >>>>Hi,
              >>>>>
              >>>>Here, sample code where a byte array is used to fill a particular
              >>>>structure :
              >>>>>
              >>>> [...]
              >>>>>
              >>>> fs = File.OpenRead(p ath); // FileStream
              >>>>>
              >>>> BITMAPFILEHEADE R bfh = new BITMAPFILEHEADE R();
              >>>> b = new byte[(int)bfh.Size()];
              >>>>>
              >>>> fs.Read(b, 0, b.Length);
              >>>> fixed (byte* pb = &b[0])
              >>>> bfh =
              >>>>(BITMAPFILE HEADER)Marshal. PtrToStructure( (IntPtr)pb,
              >>>>typeof(BITM APFILEHEADER));
              >>>>>
              >>>> [...]
              >>>>>
              >>>>What I would like to have is a function capable of accepting an [in]
              >>>>byte array and an [out] unkown structure type.
              >>>>Is that possible?
              >>>>>
              >>>>Thanks in advance,
              >>>>>
              >>>>Carles
              >>>>>
              >>>>
              >>>>
              >>>
              >>
              >>
              >
              >

              Comment

              Working...