Questions about StructLayout Pack values

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

    #1

    Questions about StructLayout Pack values

    I have this and it seems to be working OK but now I wonder if the Pack value
    shouldn't be 1.

    What do you think?

    <StructLayout(L ayoutKind.Seque ntial, Pack:=4, CharSet:=CharSe t.Auto)_

    Public Structure CHARFORMAT2

    Public cbSize As Integer

    Public dwMask As Integer

    Public dwEffects As Integer

    Public yHeight As Integer

    Public yOffset As Integer

    Public crTextColor As Integer

    Public bCharSet As Byte

    Public bPitchAndFamily As Byte

    ....snip

    The Marshaller doesn't know enough to remove the alignment additions does
    it?

    ====

    for the following Pack:=2 would work, right?

    But Pack=1 would also work, yes?

    Public Structure BITMAP

    Public bmType As Integer

    Public bmWidth As Integer

    Public bmHeight As Integer

    Public bmWidthBytes As Integer

    Public bmPlanes As Short

    Public bmBitsPixel As Short

    Public bmBits As Integer

    End Structure

    In fact, when dealing with Windows API struc's doesn't make sense to always
    use Pack:=1, Why even try to find the maximum value that works? Just use 1.
    What do you think about that?



    Thanks






  • Jack Jackson

    #2
    Re: Questions about StructLayout Pack values

    On Fri, 20 Jul 2007 15:25:57 -0400, " active"
    <activeNOSPAM @a-znet.comwrote:
    >
    >In fact, when dealing with Windows API struc's doesn't make sense to always
    >use Pack:=1, Why even try to find the maximum value that works? Just use 1.
    >What do you think about that?
    1 will work only if the structure doesn't have any holes. For
    example:

    Public Structure S

    Public f1 As Integer
    Public f2 As Byte
    Public f2 as Integer

    will give different result for packing of 1, 2 and 4. I don't know if
    Windows API structures ever do this.

    I believe the default for Windows is to align fields on the boundary
    that is the same as their size (byte on byte boundary, short on even
    boundary, 4-byte integer on 4-byte boundary) but I'm not sure how to
    specify that here.

    Comment

    • active

      #3
      Re: Questions about StructLayout Pack values

      I've been assuming that a Windows structs are packed so that there are no
      holes.
      But if there is structs like the following would there be three bytes unused
      after j2 and one unused after j22.


      Or would they all be crammed together?

      typedef struct junk1

      {

      DWORD j1;

      BYTE j2;

      DWORD j3;

      }



      typedef struct junk2

      {

      DWORD j21;

      BYTE j22;

      WORD j23;

      }







      "Jack Jackson" <jacknospam@peb bleridge.comwro te in message
      news:8j72a319c8 5d8m8s8nd444rdp 2i631em5c@4ax.c om...
      On Fri, 20 Jul 2007 15:25:57 -0400, " active"
      <activeNOSPAM @a-znet.comwrote:
      >
      >>
      >>In fact, when dealing with Windows API struc's doesn't make sense to
      >>always
      >>use Pack:=1, Why even try to find the maximum value that works? Just use
      >>1.
      >>What do you think about that?
      >
      1 will work only if the structure doesn't have any holes. For
      example:
      >
      Public Structure S
      >
      Public f1 As Integer
      Public f2 As Byte
      Public f2 as Integer
      >
      will give different result for packing of 1, 2 and 4. I don't know if
      Windows API structures ever do this.
      >
      I believe the default for Windows is to align fields on the boundary
      that is the same as their size (byte on byte boundary, short on even
      boundary, 4-byte integer on 4-byte boundary) but I'm not sure how to
      specify that here.

      Comment

      • Jack Jackson

        #4
        Re: Questions about StructLayout Pack values

        I'm pretty sure Windows structures have to have items on a boundary
        equal to their lengths, because some CPU types (other than i386) can't
        load 4 byte on other than a 4-byte boundary.

        On Fri, 20 Jul 2007 19:04:40 -0400, " active"
        <activeNOSPAM @a-znet.comwrote:
        >I've been assuming that a Windows structs are packed so that there are no
        >holes.
        >But if there is structs like the following would there be three bytes unused
        >after j2 and one unused after j22.
        >
        >
        >Or would they all be crammed together?
        >
        >typedef struct junk1
        >
        >{
        >
        >DWORD j1;
        >
        >BYTE j2;
        >
        >DWORD j3;
        >
        >}
        >
        >
        >
        >typedef struct junk2
        >
        >{
        >
        >DWORD j21;
        >
        >BYTE j22;
        >
        >WORD j23;
        >
        >}
        >
        >
        >
        >
        >
        >
        >
        >"Jack Jackson" <jacknospam@peb bleridge.comwro te in message
        >news:8j72a319c 85d8m8s8nd444rd p2i631em5c@4ax. com...
        >
        >On Fri, 20 Jul 2007 15:25:57 -0400, " active"
        ><activeNOSPAM@ a-znet.comwrote:
        >>
        >>>
        >>>In fact, when dealing with Windows API struc's doesn't make sense to
        >>>always
        >>>use Pack:=1, Why even try to find the maximum value that works? Just use
        >>>1.
        >>>What do you think about that?
        >>
        >1 will work only if the structure doesn't have any holes. For
        >example:
        >>
        >Public Structure S
        >>
        >Public f1 As Integer
        >Public f2 As Byte
        >Public f2 as Integer
        >>
        >will give different result for packing of 1, 2 and 4. I don't know if
        >Windows API structures ever do this.
        >>
        >I believe the default for Windows is to align fields on the boundary
        >that is the same as their size (byte on byte boundary, short on even
        >boundary, 4-byte integer on 4-byte boundary) but I'm not sure how to
        >specify that here.
        >

        Comment

        • active

          #5
          Re: Questions about StructLayout Pack values


          "Jack Jackson" <jacknospam@peb bleridge.comwro te in message
          news:n5h2a31utk 5hhmvjj5p8b7pj8 3fiil1o1p@4ax.c om...
          I'm pretty sure Windows structures have to have items on a boundary
          equal to their lengths, because some CPU types (other than i386) can't
          load 4 byte on other than a 4-byte boundary.
          >
          >
          I've been looking and almost all satisfy that. But the one below is what
          started me wondering. I know for a fact that it is supposed to fits into 14
          bytes. Seems like they should have put bfType last and bfOffBits first.

          I do believe what you said fits everything else I've seen.

          thanks
          <StructLayout(L ayoutKind.Seque ntial, Pack:=2)_

          Public Structure BITMAPFILEHEADE R

          Public bfType As Int16 '19778 = "BM"

          Public bfSize As Int32 'Entire file including this header

          Public bfReserved1 As Int16

          Public bfReserved2 As Int16

          Public bfOffBits As Int32 'From the beginning of file (bytes)

          End Structure


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Questions about StructLayout Pack values

            "Jack Jackson" <jacknospam@peb bleridge.comsch rieb:
            I'm pretty sure Windows structures have to have items on a boundary
            equal to their lengths, because some CPU types (other than i386) can't
            load 4 byte on other than a 4-byte boundary.
            The Win32 API typically uses structures with their members packed on 4-byte
            boundaries. However, there are some exceptions in the Shell API where
            members are packed on single-byte boundaries.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • active

              #7
              Re: Questions about StructLayout Pack values

              What does vb.net do.

              I'm wondering when I need to include Pack:



              "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
              news:%23ld3ig6y HHA.4476@TK2MSF TNGP06.phx.gbl. ..
              "Jack Jackson" <jacknospam@peb bleridge.comsch rieb:
              >I'm pretty sure Windows structures have to have items on a boundary
              >equal to their lengths, because some CPU types (other than i386) can't
              >load 4 byte on other than a 4-byte boundary.
              >
              The Win32 API typically uses structures with their members packed on
              4-byte boundaries. However, there are some exceptions in the Shell API
              where members are packed on single-byte boundaries.
              >
              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Questions about StructLayout Pack values

                " active" <activeNOSPAM @a-znet.comschrieb :
                What does vb.net do.
                >
                I'm wondering when I need to include Pack:
                IIRC the default is a packing on 'DWORD'-boundaries (4-byte boundaries).
                You need to include 'Pack' when the target system expects another packing,
                such as packing on single-byte boundaries.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                Comment

                • active

                  #9
                  Re: Questions about StructLayout Pack values


                  I've been getting by with things like what's below.

                  If I understand you correctly this should not work or is just that the
                  4-byte items must be on 4-byte boundaries?

                  Or are the Shorts followed by 2 unused bytes?

                  I do know of a case something like a Short followed by a DWORD, and there
                  were two extra bytes after the short.

                  If you have only four bytes must you use Pack:1

                  How about a Structure containing only 4 Shorts, need Pack:2

                  Comments?

                  Thanks
                  Public Structure PARAFORMAT

                  Public cbSize As Integer

                  Public dwMask As Integer

                  Public wNumbering As Short

                  Public wReserved As Short

                  Public dxStartIndent As Integer

                  Public dxRightIndent As Integer

                  ....

                  End Structure


                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                  news:eC5DL17yHH A.988@TK2MSFTNG P02.phx.gbl...
                  >" active" <activeNOSPAM @a-znet.comschrieb :
                  >What does vb.net do.
                  >>
                  >I'm wondering when I need to include Pack:
                  >
                  IIRC the default is a packing on 'DWORD'-boundaries (4-byte boundaries).
                  You need to include 'Pack' when the target system expects another packing,
                  such as packing on single-byte boundaries.
                  >
                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                  Comment

                  Working...