User Defined Types and DWord alignment

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

    #1

    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
  • J French

    #2
    Re: User Defined Types and DWord alignment

    On 31 May 2004 08:46:22 -0700, paulsmith5@hotm ail.com (Paul) wrote:
    [color=blue]
    >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.[/color]

    In memory the UDT is 'aligned'

    When written to disk it undergoes a 'magical' transformation

    - UDTs are safe for disk reading/writing

    Comment

    • Paul

      #3
      Re: User Defined Types and DWord alignment

      Hi J,

      Thanks for the clarification.

      Paul

      Comment

      Working...