473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(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


Jul 20 '07 #1
8 5609
On Fri, 20 Jul 2007 15:25:57 -0400, " active"
<ac**********@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.
Jul 20 '07 #2
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" <ja********@pebbleridge.comwrote in message
news:8j********************************@4ax.com...
On Fri, 20 Jul 2007 15:25:57 -0400, " active"
<ac**********@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.

Jul 20 '07 #3
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"
<ac**********@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" <ja********@pebbleridge.comwrote in message
news:8j********************************@4ax.com.. .
>On Fri, 20 Jul 2007 15:25:57 -0400, " active"
<ac**********@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.
Jul 20 '07 #4

"Jack Jackson" <ja********@pebbleridge.comwrote in message
news:n5********************************@4ax.com...
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
Jul 21 '07 #5
"Jack Jackson" <ja********@pebbleridge.comschrieb:
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/>

Jul 21 '07 #6
What does vb.net do.

I'm wondering when I need to include Pack:

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Jack Jackson" <ja********@pebbleridge.comschrieb:
>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/>

Jul 21 '07 #7
" active" <ac**********@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/>

Jul 21 '07 #8

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]" <hi***************@gmx.atwrote in message
news:eC*************@TK2MSFTNGP02.phx.gbl...
>" active" <ac**********@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/>

Jul 21 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
2
by: Al Bahr | last post by:
H I am try to convert C# to vb.net I don’t know what would be an equivalent statements in VB.net any help will be appreciated. bmiColors ' RGBQUAD structs... Blue-Green-Red-Reserved, repeat.....
19
by: MP | last post by:
I'm interested to learn how to use mdb files via ado/adox via vb6 Since I'm not using Access are those types of questions o.t. here? I see very few ado questions here. But there's a lot more...
13
by: M.Siler | last post by:
Let me clarify from my last post. I am not using these 4 questions as the sole screening method. Currently in, the Tampa Bay area (Florida) there is an extreme shortage of C# developers. We have...
1
by: tkpmep | last post by:
I write data to Excel files using PyExcelerator 0.6.3.a and have done so successfully for small files (10-15 cells). I'm experiencing an error when writing a big chunk of data (10,000 cells) to...
2
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible...
18
by: p.lavarre | last post by:
Can Python not express the idea of a three-byte int? For instance, in the working example below, can we somehow collapse the three calls of struct.pack into one? 08 12 34 56 80 00 I ask...
9
by: jezonthenet | last post by:
I started using Python a couple of days ago - here are a few questions: * Doesn't the __main__() method automatically execute when I run my python program? * Only when I do an import of my...
2
by: biganthony via AccessMonster.com | last post by:
Hi, I decided to install Office 2003 Service Pack 3 on my home computer to test (in full knowledge that there may be some issues with it). After installation, I have noticed that with a small...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.