1. Phix: get_struct_size() returns eventually wrong size

Hallo With Phix version 1.0.3 get_struct_size() returns eventually wrong size. It seems to happen with the WORD Datatype.

I have put togheter this example:

include cffi.e 
constant tPD=""" 

 
typedef struct tagPD { 
  DWORD           lStructSize; 
  HWND            hwndOwner; 
  HGLOBAL         hDevMode; 
  HGLOBAL         hDevNames; 
  HDC             hDC; 
  DWORD           Flags; 
  WORD            nFromPage; 
  WORD            nToPage; 
  WORD            nMinPage; 
  WORD            nMaxPage; 
  WORD            nCopies; 
  HINSTANCE       hInstance; 
  LPARAM          lCustData; 
  LPPRINTHOOKPROC lpfnPrintHook; 
  LPSETUPHOOKPROC lpfnSetupHook; 
  LPCTSTR         lpPrintTemplateName; 
  LPCTSTR         lpSetupTemplateName; 
  HGLOBAL         hPrintTemplate; 
  HGLOBAL         hSetupTemplate; 
} PRINTDLG, *LPPRINTDLG; 
            
""" 

 
global constant integer idPD=define_struct(tPD) 
global atom pPD=allocate_struct(idPD) 
 
printf(1,"Size of struct : %d should be 66",get_struct_size(idPD)) 
 
set_struct_field(idPD,pPD,"let_it_crash",0) 
 

Output:

Size of struct : 68 should be 66 
D:\devpool\phix\builtins\cffi.e:1128 in procedure set_struct_field() 
index 0 out of bounds, reading sequence length 19 
    id = 1 
    pStruct = 10122496.0 
    field = "let_it_crash" 
    v = 0 
    membernames = {"lStructSize","hwndOwner","hDevMode","hDevNames","hDC","Flags","nFromPage","nToPage","nMinPage","nMaxPage","nCopies","hInstance","lCustData","lpfnPrintHook","lpfnSetupHook","lpPrintTemplateName","lpSetupTemplateName","hPrintTemplate","hSetupTemplate"} 
    details = {{"DWORD",4,0,0},{"HWND",4,4,1},{"HGLOBAL",4,8,1},{"HGLOBAL",4,12,1},{"HDC",4,16,1},{"DWORD",4,20,0},{"WORD",2,24,1},{"WORD",2,26,1},{"WORD",2,28,1},{"WORD",2,30,1},{"WORD",2,32,1},{"HINSTANCE",4,36,1},{"LPARAM",4,40,1},{"LPPRINTHOOKPROC",4,44,1},{"LPSETUPHOOKPROC",4,48,1},{"LPCTSTR",4,52,1},{"LPCTSTR",4,56,1},{"HGLOBAL",4,60,1},{"HGLOBAL",4,64,1}} 
    k = 0 
    size = <novalue> 
    offset = <novalue> 
... called from D:\devpool\phix\demo\edita\tinewg\cffi_test.exw:33 

Thank you Andreas

new topic     » topic index » view message » categorize

2. Re: Phix: get_struct_size() returns eventually wrong size

My first reaction: as I understand it there should be two bytes of padding after nCopies so that hInstance is aligned on a dword boundary.

Then I found this: https://www.vbforums.com/showthread.php?843083-RESOLVED-Len-LenB-PrintDlg-API-and-the-PrintDlg-structure

Guess I'll have to think about adding some kind of "align" flag to define_struct(), or maybe better if struct_str begins with "#pragma pack(1)"...

OK, I've pushed an updated cffi.e to the repository ( https://raw.githubusercontent.com/petelomax/Phix/master/builtins/cffi.e ) for you to test.
Should you prefer, the diff is here: https://github.com/petelomax/Phix/commit/04144e4fb7092c4321661a3a1d059a1c0cbcb39d
You'll have to insert #pragma pack(1) at the start of the definition of tPD (the blank lines should be optional):

constant tPD=""" 

#pragma pack(1) 
  
typedef struct tagPD {  
... 
""" 

new topic     » goto parent     » topic index » view message » categorize

3. Re: Phix: get_struct_size() returns eventually wrong size

Thank you,
this works with 32bit, with 64bit i have to remove the #pragma pack(1) line to make it work.
My fault: I should have told you that it only failed with 32bit before and allready worked with 64bit.
Sorry

petelomax said...

My first reaction: as I understand it there should be two bytes of padding after nCopies so that hInstance is aligned on a dword boundary.

Then I found this: https://www.vbforums.com/showthread.php?843083-RESOLVED-Len-LenB-PrintDlg-API-and-the-PrintDlg-structure

Guess I'll have to think about adding some kind of "align" flag to define_struct(), or maybe better if struct_str begins with "#pragma pack(1)"...

OK, I've pushed an updated cffi.e to the repository ( https://raw.githubusercontent.com/petelomax/Phix/master/builtins/cffi.e ) for you to test.
Should you prefer, the diff is here: https://github.com/petelomax/Phix/commit/04144e4fb7092c4321661a3a1d059a1c0cbcb39d
You'll have to insert #pragma pack(1) at the start of the definition of tPD (the blank lines should be optional):

constant tPD=""" 

#pragma pack(1) 
  
typedef struct tagPD {  
... 
""" 

new topic     » goto parent     » topic index » view message » categorize

4. Re: Phix: get_struct_size() returns eventually wrong size

andreasWagner said...

with 64bit i have to remove the #pragma pack(1) line to make it work.

Good catch, I didn't check/realise commdlg.h actually contains

#if !defined(_WIN64) 
#include <pshpack1.h>         /* Assume byte packing throughout */ 
#endif 

so I've made that pragma only apply under 32-bit. A new version of cffi.e has been pushed.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Phix: get_struct_size() returns eventually wrong size

petelomax said...
andreasWagner said...

with 64bit i have to remove the #pragma pack(1) line to make it work.

Good catch, I didn't check/realise commdlg.h actually contains

#if !defined(_WIN64) 
#include <pshpack1.h>         /* Assume byte packing throughout */ 
#endif 

so I've made that pragma only apply under 32-bit. A new version of cffi.e has been pushed.

Yes, that's it.

Works like expected with 32 and 64bit.

Thank you.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu