By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

User Defined Types and DWord alignment

P: n/a
Hi,

Please somebody clarify this following for me:

I am writing my own data packets to a binary file. Each packet I write
to the file is of equal length and format. For example each packet
will be 50 bytes in length and I know that the first byte of each
packet is the message header, the second 2 bytes is an integer
representing some value or other, and so on. My question is, can I use
a user defined type to read the data back out from the file without
DWord alignment affecting things. I have defined a type to match the
structure of my packet, e.g.

Private myType as Type
MsgHeader as Byte ' 1 byte
Field1 As Integer ' 2 bytes
etc.
End Type

and have read the values back in from the file using

Open "myfile" For Binary Access Read Lock Read Write As #FileNum
Get #FileNum, 1, myType
Close #FileNum

and I seem to get the correct values. As a comparison I declared each
element as a separate variable

Dim MsgHeader As Byte
Dim Field1 As Integer

and read in the values as follows

Open "myfile" For Binary Access Read Lock Read Write As #FileNum
Get #FileNum, 1, MsgHeader
Get #FileNum, 2, Field1
Close #FileNum

and they match. However when I use Len(B), e.g.

Debug.Print LenB(myType)

to get the number of bytes required to store my type I get more than
the 50 expected. Does VB always take care of DWord alignment when
reading values back out from a UDT in these situations? I don't output
to my binary file using UDTs because of the alignment issue but am
wondering if I can always use them when reading data back in.

Thanks

Paul
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 31 May 2004 08:46:22 -0700, pa********@hotmail.com (Paul) wrote:
Hi,

Please somebody clarify this following for me:

I am writing my own data packets to a binary file. Each packet I write
to the file is of equal length and format. For example each packet
will be 50 bytes in length and I know that the first byte of each
packet is the message header, the second 2 bytes is an integer
representing some value or other, and so on. My question is, can I use
a user defined type to read the data back out from the file without
DWord alignment affecting things. I have defined a type to match the
structure of my packet, e.g.


In memory the UDT is 'aligned'

When written to disk it undergoes a 'magical' transformation

- UDTs are safe for disk reading/writing
Jul 17 '05 #2

P: n/a
Hi J,

Thanks for the clarification.

Paul
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.