469,579 Members | 1,040 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

User Defined Types and DWord alignment

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
2 4436
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
Hi J,

Thanks for the clarification.

Paul
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by __PPS__ | last post: by
reply views Thread by Peder Y | last post: by
20 posts views Thread by pinkfloydhomer | last post: by
31 posts views Thread by Francine.Neary | last post: by
14 posts views Thread by n3o | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.