Connecting Tech Pros Worldwide Forums | Help | Site Map

Questions about StructLayout Pack values

active
Guest
 
Posts: n/a
#1: Jul 20 '07
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(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.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
Guest
 
Posts: n/a
#2: Jul 20 '07

re: Questions about StructLayout Pack values


On Fri, 20 Jul 2007 15:25:57 -0400, " active"
<activeNOSPAM@a-znet.comwrote:
Quote:
>
>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.
active
Guest
 
Posts: n/a
#3: Jul 21 '07

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@pebbleridge.comwrote in message
news:8j72a319c85d8m8s8nd444rdp2i631em5c@4ax.com...
Quote:
On Fri, 20 Jul 2007 15:25:57 -0400, " active"
<activeNOSPAM@a-znet.comwrote:
>
Quote:
>>
>>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.

Jack Jackson
Guest
 
Posts: n/a
#4: Jul 21 '07

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:
Quote:
>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@pebbleridge.comwrote in message
>news:8j72a319c85d8m8s8nd444rdp2i631em5c@4ax.com.. .
>
Quote:
>On Fri, 20 Jul 2007 15:25:57 -0400, " active"
><activeNOSPAM@a-znet.comwrote:
>>
Quote:
>>>
>>>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.
>
active
Guest
 
Posts: n/a
#5: Jul 21 '07

re: Questions about StructLayout Pack values



"Jack Jackson" <jacknospam@pebbleridge.comwrote in message
news:n5h2a31utk5hhmvjj5p8b7pj83fiil1o1p@4ax.com...
Quote:
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(LayoutKind.Sequential, Pack:=2)_

Public Structure BITMAPFILEHEADER

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


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Jul 21 '07

re: Questions about StructLayout Pack values


"Jack Jackson" <jacknospam@pebbleridge.comschrieb:
Quote:
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/>

active
Guest
 
Posts: n/a
#7: Jul 21 '07

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.atwrote in message
news:%23ld3ig6yHHA.4476@TK2MSFTNGP06.phx.gbl...
Quote:
"Jack Jackson" <jacknospam@pebbleridge.comschrieb:
Quote:
>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/>

Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#8: Jul 21 '07

re: Questions about StructLayout Pack values


" active" <activeNOSPAM@a-znet.comschrieb:
Quote:
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/>

active
Guest
 
Posts: n/a
#9: Jul 21 '07

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.atwrote in message
news:eC5DL17yHHA.988@TK2MSFTNGP02.phx.gbl...
Quote:
>" active" <activeNOSPAM@a-znet.comschrieb:
Quote:
>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/>

Closed Thread


Similar Visual Basic .NET bytes