473,383 Members | 1,874 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,383 software developers and data experts.

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 4560
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
13
by: dawatson833 | last post by:
I have several stored procedures with parameters that are defined with user defined data types. The time it takes to run the procedures can take 10 - 50 seconds depending on the procedure. If I...
0
by: Peder Y | last post by:
Hi! I'm working on a C# project where we need to get glyph vertices from fonts. There are several examples on how to do this in C++, but interop not being my strongest side, I have some...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
2
by: ramu | last post by:
Hi I have to call a vc++ function in a c program. suppose i have a function dword fun(dword arg1, bstr arg2); in vc++. I have to call this function in c. But I don't have dword datatype in...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
1
by: ranju | last post by:
I am trying to spawn a process (say an exe file) with different user crendentials than that of the current user. 1) Called LogonUserEx() to logon the user and recieve a handle to the token that...
31
by: Francine.Neary | last post by:
One interesting thing to come out of the recent "Alignment" thread is that it is impossible to write a portable replacement for malloc in "user space" (not sure what the right term is - I mean an...
14
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.