472,782 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 4539
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;...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.