For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > April 2005 > Data alignment









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Data alignment
Danny

2005-04-06, 12:46 pm

I need to align data (structures) on 8-byte boundaries. How does one
go about doing that in VB?

Are there any VB compiler directives or switches to do that?

Thanks!

Danny

(You guessed it! Remove NOSPAMFOR before emailing.)
Mike D Sutton

2005-04-06, 12:46 pm

> I need to align data (structures) on 8-byte boundaries. How does one
> go about doing that in VB?
>
> Are there any VB compiler directives or switches to do that?


VB does not support this, you'll need to use a byte array or similar and delegate translation between the buffer and UDT
yourself.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/
----------------------------------------------------------------------------
Read. Decide. Sign the petition to Microsoft.
http://classicvb.org/petition/


Jim Mack

2005-04-06, 12:46 pm

Danny wrote:
> I need to align data (structures) on 8-byte boundaries. How does one
> go about doing that in VB?
>
> Are there any VB compiler directives or switches to do that?


VB doesn't support that directly. If you have control of the data
definition, you can almost always arrange things so that alignment is
not an issue, by placing all 8-byte items first in the structure,
followed by all 4-byte items, etc. Then no padding will be needed, and
so no misalignments possible.

If you don't have that control, then you have to do it manually, by
inserting individual bytes or integers where the 8-byte side would
expect padding bytes. If you need to know when that would happen, post
the structure and we can tell you.

The rule is simple: padding is required before any element that would
not otherwise align at an address that matches its size. In other
words, any 8-byte element must have an address that has its lowest 3
bits clear; a 4-byte element must have the lowest 2 bits clear in its
address, and so on.

--

Jim Mack
MicroDexterity Inc
www.microdexterity.com


Jim Carlock

2005-04-06, 12:46 pm

I was looking at the following link tonight...

http://www.devx.com/vb2themax/Article/19830/1763/page/2

It indicates you can compile your program (to an ActiveX .dll) then
use OLE View (found in the Visual Studio tools) to open the .dll.

After you start OLE View, you'll be inside an OLE/COM Object
Viewer window. Click on File, then "View TypeLib...", navigate to
the path of your compiled dll, then select that file and click on the
Open button.

The file will open up in the ITypeLib Viewer. You can then export
the TypeLib to an IDL file and use the MIDL compiler to align it to
an 8-byte boundary. You'll end up with a Type-Lib (.tlb) file. I've
just started exploring this and don't know much about it. I'm a little
lost in this at the moment.

The link above describes 2 other alternate methods of exporting an
ActiveX .dll's type-library information.

Don't know if that helps at all, and if someone else can add some
information, that would be great.

--
Jim Carlock
Please post replies to newsgroup.

"Danny" wrote:
I need to align data (structures) on 8-byte boundaries. How does one
go about doing that in VB?

Are there any VB compiler directives or switches to do that?

Thanks!

Danny

(You guessed it! Remove NOSPAMFOR before emailing.)


Danny

2005-04-06, 9:01 pm

Date: Wed, 6 Apr 2005 00:42:01 +0100
Name: "Mike D Sutton" <EDais@mvps.org>

>
>VB does not support this, you'll need to use a byte array or similar and delegate translation between the buffer and UDT
>yourself.


That was my fallback solution, but I thought I'd ask first just in
case.

Danny

(You guessed it! Remove NOSPAMFOR before emailing.)
Danny

2005-04-06, 9:01 pm

Date: Tue, 5 Apr 2005 20:54:12 -0400
Name: "Jim Mack" <jmack@mdxi.nospam.com>

>Danny wrote:
>
>VB doesn't support that directly. If you have control of the data
>definition, you can almost always arrange things so that alignment is
>not an issue, by placing all 8-byte items first in the structure,
>followed by all 4-byte items, etc. Then no padding will be needed, and
>so no misalignments possible.
>
>If you don't have that control, then you have to do it manually, by
>inserting individual bytes or integers where the 8-byte side would
>expect padding bytes.


The order of parameters within any one structure is outside of my
control but I could use VarPtr to interrogate the structure's address
and then shift things around as needed. However, that's going to get
quite messy which is why I was hoping for a cleaner solution.

Danny

(You guessed it! Remove NOSPAMFOR before emailing.)
Danny

2005-04-06, 9:01 pm

Date: Tue, 5 Apr 2005 23:10:21 -0400
Name: "Jim Carlock" <anonymous@localhost>

>I was looking at the following link tonight...
>
>http://www.devx.com/vb2themax/Article/19830/1763/page/2
>
>It indicates you can compile your program (to an ActiveX .dll) then
>use OLE View (found in the Visual Studio tools) to open the .dll.
>
>After you start OLE View, you'll be inside an OLE/COM Object
>Viewer window. Click on File, then "View TypeLib...", navigate to
>the path of your compiled dll, then select that file and click on the
>Open button.
>
>The file will open up in the ITypeLib Viewer. You can then export
>the TypeLib to an IDL file and use the MIDL compiler to align it to
>an 8-byte boundary. You'll end up with a Type-Lib (.tlb) file. I've
>just started exploring this and don't know much about it. I'm a little
>lost in this at the moment.
>
>The link above describes 2 other alternate methods of exporting an
>ActiveX .dll's type-library information.
>
>Don't know if that helps at all, and if someone else can add some
>information, that would be great.


That's a very interesting lateral thought! I'd be following this too!

Danny

(You guessed it! Remove NOSPAMFOR before emailing.)
Rob

2005-04-09, 8:58 am

You can change the alignment of a single struct within a typelib with MIDL
using the pragma pack directive.

MIDL code to change alignment of a single struct

#pragma pack(8)
typedef struct MyStruct
{
short MyThing1;
long MyThing2;
};
#pragma pack()



"Jim Carlock" <anonymous@localhost> wrote in message
news:uo7BlXlOFHA.3512@TK2MSFTNGP15.phx.gbl...
> I was looking at the following link tonight...
>
> http://www.devx.com/vb2themax/Article/19830/1763/page/2
>
> It indicates you can compile your program (to an ActiveX .dll) then
> use OLE View (found in the Visual Studio tools) to open the .dll.
>
> After you start OLE View, you'll be inside an OLE/COM Object
> Viewer window. Click on File, then "View TypeLib...", navigate to
> the path of your compiled dll, then select that file and click on the
> Open button.
>
> The file will open up in the ITypeLib Viewer. You can then export
> the TypeLib to an IDL file and use the MIDL compiler to align it to
> an 8-byte boundary. You'll end up with a Type-Lib (.tlb) file. I've
> just started exploring this and don't know much about it. I'm a little
> lost in this at the moment.
>
> The link above describes 2 other alternate methods of exporting an
> ActiveX .dll's type-library information.
>
> Don't know if that helps at all, and if someone else can add some
> information, that would be great.
>
> --
> Jim Carlock
> Please post replies to newsgroup.
>
> "Danny" wrote:
> I need to align data (structures) on 8-byte boundaries. How does one
> go about doing that in VB?
>
> Are there any VB compiler directives or switches to do that?
>
> Thanks!
>
> Danny
>
> (You guessed it! Remove NOSPAMFOR before emailing.)
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com