The above title if for your Huh :grin:

Im wondering if anyone knows the rule-of-thumb for transcribing VC++ structures. We all use Iczelions hard work (windows.inc) but put little thought to the effort taken to do so.

I reciently transcribed a good amount of C++ structures to masm (SAPI.H) and are correct according to their H files, however im learning that the C++ compiler tends to ignore some fields.

For example, I have found that some structure entries casts as a CHAR in VC++ (.h file), however, windows actualy uses a DWORD in practice.

I found this out by dumpin the memory filled out by windows. This structure the first cbSize field filled in by the OS to a number that is larger than the amount of bytes the .h file suggests it is to work upon. Since my mind is focused in assembly, when i read CHAR I write BYTE (not DWORD). By tediously tracking through the memory dump, and cross referencing it to the structure i came to this discovery.

Does anyone know of any other 'types' that are misleading and are viewed as something else by the os??

Here is the example Structure:
SPPHRASE struc 

[b] cbSize dd ? ;ULONG [/b]
LangID dw ? ;WORD
wReserved dw ? ;WORD
ullGrammarID dq ? ;ULONGLONG
ftStartTime dq ? ;ULONGLONG
ullAudioStreamPosition dq ? ;ULONGLONG
ulAudioSizeBytes dd ? ;ULONG
ulRetainedSizeBytes dd ? ;ULONG
ulAudioSizeTime dd ? ;ULONG
[b] Rule SPPHRASERULE <> ;SPPHRASERULE [/b]
pProperties dd ? ;const SPPHRASEPROPERTY *
pElements dd ? ;const SPPHRASEELEMENT *
cReplacements dd ? ;ULONG
pReplacements dd ? ;const SPPHRASEREPLACEMENT *
SREngineID GUID <>;
ulSREnginePrivateDataSize dd ? ;ULONG
pSREnginePrivateData dd ? ;const BYTE *
SPPHRASE ends
The embeded SPPHARERULE structure is defined as:
SPPHRASERULE struc 

pszName dd ? ;const WCHAR *
ulId dd ? ;ULONG
ulFirstElement dd ? ;ULONG
ulCountOfElements dd ? ;ULONG
pNextSibling dd ? ;const SPPHRASERULE *
pFirstChild dd ? ;const SPPHRASERULE *
SREngineConfidence REAL4 ? ;float
[b] Confidence dd ? ;signed char (Modified by NaN Empirically)[/b]
SPPHRASERULE ends


The size in the sbSize is 120 bytes when a memory pointer is given back by the OS (to be used as a SPPHRASE type structure of memory). However, when i asked the compiler to echo the SIZEOF( SPPHRASE ) the number origionally was 113 bytes. I found the char to be a dword and made the changes to the structure, but im still short another 4 bytes.. so im wonderinging what else i can expect.

The Rule structure shows up at the correct index into the memory dump, so this tells me that the WORDS for langId and Reserved are truely word types. And everything else looks un-elusive.

However, I wonder if a type FLOAT is *really* stored as a REAL4. Or perhaps memory is allocated for a DOUBE (REAL8) and only uses the first 4 bytes. This would add to 120 then...

Anywho, if anyone knows what im talking about and can add to or correct my assumptions, please do... thanx!

:alright:
NaN
Posted on 2002-12-08 22:43:58 by NaN
too lazy to read and think about the whole thread. ;) But I do believe why char(1 byte) is treated as a dword(4 bytes) is because of alignment stuff in memory... ;)

it's much better to treat everything in multiples of 4(DWORD size)

I don't know. Myabe I'm wrong. :)

as for Float, to me that's a DWORD size (4 bytes - REAL4) and Doubles as QWORD size(8 bytes - REAL8).

are you sure you didn't left any fields out... maybe you have skipped some of them...
Posted on 2002-12-09 00:24:58 by stryker
Hi NaN,

A char is still a char. VC rounds the structure size up to the nearest 2^n byte boundary. You can change this with the /Zp switch or the pack pragma.

Try this:
[size=12]#pragma pack( push, 1 )

#include <sapi.h>
#pragma pack( pop )

int main()
{
printf( "SPPHRASE = %i bytes\n", sizeof( SPPHRASE ) );
printf( "SPPHRASERULE = %i bytes\n", sizeof( SPPHRASERULE ) );
return 0;
}


...and it will report the byte sizes. :alright:
Posted on 2002-12-09 01:43:10 by iblis
The reason for rounding up is for maintaining alignment in arrays of structures.
Posted on 2002-12-09 17:02:37 by tenkey
Gotcha...

And i agree REAL4 is 4 bytes.. but no, Im not missing anything. All entries are there and transcribed properly. I mean its not to hard to check. After the comment ( ; ) is the C++ discriptor for the tag name. I used a macro with UltraEdit to do this for me to take the tediousness out, but i still initiated the macro line-by-line and watched it perform, so i wont miss any problem.

So thats the C++ stucture, litterally. As well, i didnt do the accounting by hand, i had the assembler do it for me:


ANum equ SIZEOF( SPPHRASE)
AText TEXTEQU %ANum
%echo SPPHRASE == &AText


NaN
Posted on 2002-12-09 17:35:54 by NaN
Hmmm.. I just accounted by hand the documentation on the structure, and i get 116 bytes as well. So basically windows is handing out an oversized amount of memory. I can only assume thats why, and as such it places 120 in the field for the structure size.

NaN
Posted on 2002-12-09 17:55:26 by NaN