News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

are you sure that 4+4+1=9 ?

Started by TouEnMasm, September 06, 2012, 11:04:06 PM

Previous topic - Next topic

TouEnMasm


I have tested the STORAGE_PROPERTY_QUERY structure here his:
Quote
STORAGE_PROPERTY_QUERY   STRUCT
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE 1 dup (?)
STORAGE_PROPERTY_QUERY      ENDS
For each individual part of the structure the c++ agree with the size
stock = sizeof(STORAGE_PROPERTY_QUERY);      12
  stock = sizeof(mesure.PropertyId);                     4
  stock = sizeof(mesure.QueryType);                     4
  stock = sizeof(mesure.AdditionalParameters);     1

4+4+1=12    ???
I suppose that an alignment or a round is made for the byte.
How to do the same thing with masm ??????









Fa is a musical note to play with CL

TouEnMasm


to know where are added the three bytes for alignment ,I try to get the adress of each member of the structure.

Quote
DWORD TestSize(STORAGE_PROPERTY_QUERY * essai)
{
   STORAGE_PROPERTY_QUERY * machin;
    STORAGE_PROPERTY_QUERY * chose;

   chose = essai;                ok here,adress of the structure
   machin = * essai->PropertyId; ????? failed
   return 0;
}
Perhaps somebody here know how to get the adress of essai.PropertyId ?


Fa is a musical note to play with CL

qWord

Quote from: ToutEnMasm on September 07, 2012, 12:50:24 AMto know where are added the three bytes for alignment ,I try to get the adress of each member of the structure.
The bytes are appended. To get a reference:  &essai->PropertyId.
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

STORAGE_PROPERTY_QUERY   STRUCT DWORD
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE ?
STORAGE_PROPERTY_QUERY      ENDS


doesn't that work ???
otherwise - you might try the OPTION directive to set the structure alignment

if neither of those works....
STORAGE_PROPERTY_QUERY   STRUCT
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE ?
  BYTE 3 dup(?)
STORAGE_PROPERTY_QUERY      ENDS


:P

MichaelW

The attachment contains a C test and an ASM test. I think for structures that end with a 1-element array, the general expectation is that you allocate memory for the structure and use the structure definition as a template.
Well Microsoft, here's another nice mess you've gotten us into.

TouEnMasm


Ok , the & is an answer.
last question is , how i declare machin to made this work?
i have tried int dword ...
Quote
machin = (&essai - &essai->AdditionalParameters );
Fa is a musical note to play with CL

MichaelW

This should work:

int machin;
. . .
machin = ((int)&essai->PropertyId - (int)&essai->AdditionalParameters );
printf("%d\n\n", machin);


But if you want the offset of the AdditionalParameters member from the start of the structure you will need to reverse the subtraction, so the result will be 8 instead of -8.
Well Microsoft, here's another nice mess you've gotten us into.

qWord

The rules for aligment in structures can read up here: Structure Alignment
MREAL macros - when you need floating point arithmetic while assembling!

japheth


to get the offset of a member within a struct, the C offsetof() macro is probably the best choice:

mbrofs = offsetof( struct xxx, member );

TouEnMasm

Thanks at all for answers.
Here a resume of my test:
Quote
DWORD TestSize(STORAGE_PROPERTY_QUERY * essai)
{
    int machin;
    STORAGE_PROPERTY_QUERY * chose;
   chose = essai;
   machin = ((int)&essai->PropertyId - (int)&essai->AdditionalParameters );
   //machin = 8 or -8
   machin = offsetof( STORAGE_PROPERTY_QUERY,AdditionalParameters );
   // machin = 8 need  #include <stddef.h>
      return 0;
}

And for masm the good method to declare an alignment for a structure
Quote
STORAGE_PROPERTY_QUERY   STRUCT DWORD
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE 1 dup(?) ; dup (?)
STORAGE_PROPERTY_QUERY      ENDS   
This give the good size and the good alignment
I have learn much today.
Fa is a musical note to play with CL

jj2007

AdditionalParameters BYTE 1 dup(?) ; dup (?)
and
AdditionalParameters BYTE ?

are identical.

AdditionalParameters BYTE 4 dup(?)

achieves align DWORD, too.

dedndave

hiya Jochen
this one yields the correct result for SIZEOF AdditionalParameters
STORAGE_PROPERTY_QUERY   STRUCT
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE ?
  BYTE 3 dup(?)
STORAGE_PROPERTY_QUERY      ENDS

TouEnMasm


The interest of all this is to give at masm the correct alignment for each structure as do the c++.
here is a sample of translate,winhttp sample:
Quote
// defaut alignment for c++ is 4 ,DWORD for masm
#if defined(_WIN64)
#include <pshpack8.h>
#else
#include <pshpack4.h> //return to the defaut alignment
#endif
................ here structures ----------------------
#include <poppack.h>

can be done as this with masm
Quote
IF DEFINED(_WIN64)
;Include pshpack8.SDK
DEFALIGNMASM TEXTEQU <QWORD>
ELSE
;Include pshpack4.SDK
DEFALIGNMASM TEXTEQU <DWORD>
ENDIF
................ here structures ----------------------
     ANYSTRUCTURE STRUCT DEFALIGNMASM
        ..........
     ANYSTRUCTURE ENDS
;include <poppack.h>
DEFALIGNMASM TEXTEQU <DWORD>   ;return to defaut alignment
I have verify that this soluce work.
adding bytes to the structure isn't a good way
Fa is a musical note to play with CL

japheth

> I have verify that this soluce work.

It works most of the time, but AFAIR the Win32 alignment is also 8 (=QWORD), not DWORD.

It just doesn't matter usually because in Win32 there are very few QWORD members found in the structures.

But for an example where alignment DWORD doesn't work in Win32, see structure PARTITION_INFORMATION in winioctl.h.

So the simplest solution may just be to add -Zp8 as cmdline parameter when you want all Windows structures to be correct.

TouEnMasm


The Zp4 or Zp8 option are only a partial solution.
This don't avoid the "Invalid parameter" send by the function receiving a bad size of the structure.
With such an error,there is only to search , can be a long time.
Fa is a musical note to play with CL