MENUBARINFO record problems lazarus / 34-Windows ---------------------------------------------------------------------- codewar65 · 2017-05-22 18:01 Puttering around with non client painting and noticed that I am getting error 87 (invalid parameter) when calling GetMenuBarInfo. Googling around it appears to be caused from invalid cbSize being set. I set mine to sizeof(MENUBARINFO). For grins, I made my own MENUBARINFO record and used what Microsoft says it should be. ( https://msdn.microsoft.com/en- us/library/windows/desktop/ms647564%28v=vs.85%29.aspx ) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type    MENUBARINFO = record        cbSize : DWORD;        rcBar : RECT;      hMenu : HMENU;        hwndMenu : HWND;        fBarFocused : BOOL;      fFocused : BOOL;    end;  Free Pascal has it defined in struct.inc as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type     tagMENUBARINFO = packed record       cbSize:DWORD;       rcBar:Windows.RECT; // rect of bar, popup, item       _hmenu:HMENU;     // real menu handle of bar, popup       hwndMenu:HWND;      // hwnd of item submenu if one//    fBarFocused:1:BOOL;  // bar, popup has the focus//    fFocused:1:BOOL;     // item has the focus      FocusedBits:DWORD;    end;  My record works. The one in struct.inc does not. Is there a compiler setting that I am missing. Just curious as to what is going on.. Thanks. ---------------------------------------------------------------------- marcov · 2017-05-23 18:53 Please file a bug in mantis ( https://bugs.freepascal.org ) ---------------------------------------------------------------------- Remy Lebeau · 2017-05-23 19:05 Quote from: codewar65 on May 22, 2017, 08:01:37 pmFor grins, I made my own MENUBARINFO record and used what Microsoft says it should be. ( https://msdn.microsoft.com/en- us/library/windows/desktop/ms647564%28v=vs.85%29.aspx ) The MSDN declaration is wrong, as is evident by looking at the real declaration of MENUBARINFO in winuser.h in the Win32 SDK: Code: [Select]/*  * Menubar information  */ typedef struct tagMENUBARINFO {     DWORD cbSize;     RECT rcBar;          // rect of bar, popup, item     HMENU hMenu;         // real menu handle of bar, popup     HWND hwndMenu;       // hwnd of item submenu if one     BOOL fBarFocused:1;  // bar, popup has the focus     BOOL fFocused:1;     // item has the focus } MENUBARINFO, *PMENUBARINFO, *LPMENUBARINFO; The FreePascal declaration is correct.  The last 2 fields really are bitfields that occupy only 4 bytes total, not 8 bytes when using separate BOOL fields. Quote from: codewar65 on May 22, 2017, 08:01:37 pmMy record works. The one in struct.inc does not. I can't reproduce this (in C++, I haven't tried in FreePascal).  In a 32-bit program, if cbSize is 36 (the size of your record, and MSDN's), GetMenuBarInfo() fails with error 87.  If cbSize is 32 (the size of FreePascal's record, and the SDK's record above), GetMenuBarInfo() succeeds. ---------------------------------------------------------------------- codewar65 · 2017-05-24 00:26 Quote from: Remy Lebeau on May 23, 2017, 09:05:19 pmQuote from: codewar65 on May 22, 2017, 08:01:37 pmFor grins, I made my own MENUBARINFO record and used what Microsoft says it should be. ( https://msdn.microsoft.com/en- us/library/windows/desktop/ms647564%28v=vs.85%29.aspx ) The MSDN declaration is wrong, as is evident by looking at the real declaration of MENUBARINFO in winuser.h in the Win32 SDK: Code: [Select]/*  * Menubar information  */ typedef struct tagMENUBARINFO {     DWORD cbSize;     RECT rcBar;          // rect of bar, popup, item     HMENU hMenu;         // real menu handle of bar, popup     HWND hwndMenu;       // hwnd of item submenu if one     BOOL fBarFocused:1;  // bar, popup has the focus     BOOL fFocused:1;     // item has the focus } MENUBARINFO, *PMENUBARINFO, *LPMENUBARINFO; The FreePascal declaration is correct.  The last 2 fields really are bitfields that occupy only 4 bytes total, not 8 bytes when using separate BOOL fields. Quote from: codewar65 on May 22, 2017, 08:01:37 pmMy record works. The one in struct.inc does not. I can't reproduce this (in C++, I haven't tried in FreePascal).  In a 32-bit program, if cbSize is 36 (the size of your record, and MSDN's), GetMenuBarInfo() fails with error 87.  If cbSize is 32 (the size of FreePascal's record, and the SDK's record above), GetMenuBarInfo() succeeds. Testing here, I get 40 bytes from the FP record (which errors out), 48 bytes from my MS record (which does not).  Maybe I'm compiling in 64 bit? This projects is using defaults, but here's a screen of the compiler target info: http://i7.photobucket.com/albums/y299/codewar/laz-1.png Weird. ---------------------------------------------------------------------- molly · 2017-05-24 01:39 Quote from: codewar65 on May 24, 2017, 02:26:53 amTesting here, I get 40 bytes from the FP record (which errors out), 48 bytes from my MS record (which does not).  Maybe I'm compiling in 64 bit? Must be then, not ? jedi is more explicit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //// Menubar information// type  LPMENUBARINFO = ^MENUBARINFO;  {$EXTERNALSYM LPMENUBARINFO}  tagMENUBARINFO = record    cbSize: DWORD;    rcBar: RECT;          // rect of bar, popup, item    hMenu: HMENU;         // real menu handle of bar, popup    hwndMenu: HWND;     // hwnd of item submenu if one    Flags: DWORD;    // BOOL  fBarFocused:1;  // bar, popup has the focus    // BOOL  fFocused:1;   // item has the focus  end;  {$EXTERNALSYM tagMENUBARINFO}  MENUBARINFO = tagMENUBARINFO;  {$EXTERNALSYM MENUBARINFO}  TMenuBarInfo = MENUBARINFO;  PMenuBarInfo = LPMENUBARINFO; Which should return 32 bytes in 32-bit mode and 40 bytes in 64-bit mode... BTW: in 16 bit era the msdn definition would be ok. Probably never updated ? I am not so sure about those bitfields being real bits, despite Remy Lebeau's paste from official sdk (not distrusting Remy, but the SDK). You can always try to define the bitfields as wordbool and see if that matches. In that case you can still file a bug-report to fpc mantis (or perhaps request for a correction) ---------------------------------------------------------------------- codewar65 · 2017-05-24 11:56 I'm not sure what I'm doing wrong, but I have abandoned this. Now I'm getting error 87 on other functions (GetMenuItemInfo) as well that have a similar cbSize. I think there might be a setting missing in my setup for compiling for windows x64 or FP is not yet ready for x64 compatibility. Anyway, onto other things. Thanks for the help and info. ---------------------------------------------------------------------- ASerge · 2017-05-24 16:36 If the record is not packed in winuser.h, then it is wrong to do this in FPC. This works in both Win32 and Win64: type   TMenuBarInfo = record     cbSize: DWORD;     rcBar: Windows.TRect; // rect of bar, popup, item     hmenu: HMENU;       // real menu handle of bar, popup     hwndMenu: HWND;      // hwnd of item submenu if one //    fBarFocused:1:BOOL;  // bar, popup has the focus //    fFocused:1:BOOL;     // item has the focus    FocusedBits: DWORD;  end; By the way, Win32 really changes only the two least significant bits. Win64 overwrites the whole FocusedBits. ---------------------------------------------------------------------- marcov · 2017-05-24 17:03 I removed packed in trunk. Please test. ---------------------------------------------------------------------- Remy Lebeau · 2017-05-24 19:02 Quote from: ASerge on May 24, 2017, 06:36:48 pmIf the record is not packed in winuser.h, then it is wrong to do this in FPC. Win32 structs are not packed. ---------------------------------------------------------------------- marcov · 2017-05-24 19:20 Quote from: Remy Lebeau on May 24, 2017, 09:02:36 pmQuote from: ASerge on May 24, 2017, 06:36:48 pmIf the record is not packed in winuser.h, then it is wrong to do this in FPC. Win32 structs are not packed. Afaik windows has several forms of packing. Most are naturally aligned (aka unpacked) though. But some are packed to 1,4,8 lines. An example of the first are iirc textmetric types (4) and some of the fileformat structures. Just grep for pshpack in the headers.