hii
i want to translet this


typedef struct {
BYTE type;
union {
BYTE bVal; char cVal;
WORD wVal; short sVal;
DWORD dVal; long lVal;
struct {
char *pszVal;
WORD cchVal; //only used for db/contact/getsettingstatic
};
struct {
WORD cpbVal;
BYTE *pbVal;
};
};
} DBVARIANT;


if found a translation to


DBVARIANT struct
ttype db ?;
val dd ?
pblob dd ?
DBVARIANT ends;


is this right translation ?
i know what is union but i dont know what to do with the struct(s)

bye
thanks
eko
Posted on 2004-09-03 05:21:04 by eko
eko,

The trick with unions is that they are like structures with the exception that at any given location where multiple sizes are placed together, you can use any ONE of them. This allows you to pass the address of the structure / union with different data sizes in it.

Regards,

hutch at movsd dot com
Posted on 2004-09-03 05:57:28 by hutch--
lets count the bytes of this var without the union

two bytes = 2
3 word *2 = 6
(1 dword + 2 pointers (dword) ) *4 = 12
_____________________________
20 bytes totaly with out the union

with the union we have


somestruct struct
onebyte db ? ;not include in the union
somedword dd ? ; byte word and dword all on the dword location
somebytes db 6 dup (?)
somestruct ends

EDIT:
or maybe somebytes can include somedword
and the struct will be
somestruct struct
onebyte db ?
somebytes db 6 dup (?)
somestruct ends
and

right ?

bye
thanks
eko
Posted on 2004-09-03 06:11:30 by eko
anyone?
Posted on 2004-09-03 09:48:41 by eko
I don't know how to do it with MASM, but with TASM in Ideal mode, it's:



TYPEDEF CHAR BYTE ; Works if a "char" is 1 byte
TYPEDEF SHORT WORD ; Works if a "short" is 2 bytes
TYPEDEF LONG DWORD ; Works if a "long" is 4 bytes
TYPEDEF POINTER DWORD ; Assuming an address is 4 bytes

STRUC DBVARIANT
vtype DB ?
UNION
bVal DB ?
cVal CHAR ?
wVal DW ?
sVal SHORT ?
dVal DD ?
lVal LONG ?
STRUC
pszVal POINTER ?
cchVal WORD ?
ENDS
STRUC
cpbVal WORD ?
pbVal POINTER ?
ENDS
ENDS
ENDS DBVARIANT


I guess it would be something similar in MASM, if nested aggregate types are possible at all.

Note that I've changed the name of the member "type" to be "vtype". This is because "type" is already a predefined symbol in TASM, it confuses the assembler if you use a predefined symbol as a member name.

Also, with TASM there is a utility called H2ASH which converts a .H file with these kinds of typedefs into ASM code. Maybe something similar exists with MASM.
Posted on 2004-09-05 17:02:55 by yessopotamus
eko,

With a union, you have different length possible so the longest length sets the member size.

For a number of data types or sizes,


item1 db 0
item2 dw 0
item3 dd 0


The actual member size will be that of the largest member which here is 4 bytes for the DWORD. If the union is used with a BYTE sized operand, it will in fact only use 1 byte or two for a WORD sized operand but the basic logic of a structure is a commonly used location set to the size of the largest possible data size.
Posted on 2004-09-05 18:23:03 by hutch--