API Questions - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: API Questions (/showthread.php?tid=1918) |
API Questions - TerryRitchie - 08-18-2023 Since I've started diving into API calls I figured a dedicated thread for API related questions would be better. Here is my first question. In the Wiki here: https://qb64phoenix.com/qb64wiki/index.php/Windows_Libraries#Window_Focus It's shown how to determine the foreground window (the one in focus). The Microsoft docs for GetForegroundWindow are located here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow I noticed in the Wiki example that GetForegroundWindow is declared as an _OFFSET (%&). How was this determined? Looking at the Microsoft docs there is no indication of the type of variable returned. With other window handle (hWnd) related functions I've noticed a variable type of LONG is used. Why was _OFFSET needed here instead of LONG? This has me confused. Any clarification would be greatly appreciated. RE: API Questions - SpriggsySpriggs - 08-18-2023 (08-18-2023, 07:35 PM)TerryRitchie Wrote: Since I've started diving into API calls I figured a dedicated thread for API related questions would be better.HWND is 4 bytes in 32 bit and 8 bytes in 64. The reason you probably see it often declared as a LONG is because many people use 32 bit QB64. Using OFFSET makes it automatically the right size for either system. From Microsoft, the declaration for GetForegroundWindow is: HWND GetForegroundWindow(); The HWND is a handle to the window. Most handles in Win32 are going to be translated best to OFFSET in QB64. RE: API Questions - TerryRitchie - 08-18-2023 (08-18-2023, 07:51 PM)SpriggsySpriggs Wrote:Ah, ok, so basically it's acting like an _INTEGER64 in this sense. So the rule of thumb is if a handle is returned best practice is to use _OFFSET.(08-18-2023, 07:35 PM)TerryRitchie Wrote: Since I've started diving into API calls I figured a dedicated thread for API related questions would be better.HWND is 4 bytes in 32 bit and 8 bytes in 64. The reason you probably see it often declared as a LONG is because many people use 32 bit QB64. Using OFFSET makes it automatically the right size for either system. From Microsoft, the declaration for GetForegroundWindow is: HWND GetForegroundWindow(); Thank you for the quick reply and explanation. RE: API Questions - SMcNeill - 08-18-2023 In a lot of cases, like in the wiki example you reference, the return type really isn't that important. Think of making a function like so: FUNCTION Foo&& 'Stuff END FUNCTION Now that function returns a value which is an Integer64... Yet, you'll see folks write code like: DIM whatever AS INTEGER whatever = Foo And, it works! Well, it works as long as the return value is less than 32536, otherwise you get overflow results as you can't put values larger than that in an INTEGER. Since a lot of windows API calls just return simple error/success codes, it often doesn't matter what the return type is. Nearly any variable type can successfully receive a value of 1 for success, 0 for failure. RE: API Questions - SMcNeill - 08-18-2023 Such as here: DECLARE DYNAMIC LIBRARY "kernel32" FUNCTION QueryDosDeviceA~& (BYVAL lpDeviceName AS _UNSIGNED _OFFSET, BYVAL lpTargetPath AS _UNSIGNED _OFFSET, BYVAL ucchMax AS _UNSIGNED LONG) FUNCTION GetLastError~& () END DECLARE DIM sizeofbuffer AS _UNSIGNED LONG DIM buffer AS STRING DIM i AS _UNSIGNED LONG DIM x AS _UNSIGNED LONG DIM n AS _UNSIGNED LONG sizeofbuffer = 1024 buffer = SPACE$(sizeofbuffer) DO x = 0 IF QueryDosDeviceA~&(0, _OFFSET(buffer), sizeofbuffer) = 0 THEN x = GetLastError~& IF x = &H7A THEN GetLastError is defined as an offset to return values, but x is defined as an Unsigned Long, and yet it works perfectly fine. Why? Because we only care if the return value in this case is &H7A -- which would fit properly in an Unsigned Byte! (Snippet taken from wiki for Window Ports in the windows dll examples.) It's not the *PROPER* return type, but it'll work a lot of times with any glitches. RE: API Questions - SpriggsySpriggs - 08-18-2023 As our one-eyed friend put, it doesn't often matter about return types. However, it makes the most sense to do your best to match up sizes and types, for professionalism and consistency. RE: API Questions - TerryRitchie - 08-19-2023 Can anyone figure out why the Windows Menu code here in the wiki: https://qb64phoenix.com/qb64wiki/index.php/Windows_Libraries#Windows_Menu does not work? I went extensively through the Microsoft docs and found two things that may be a problem. First, the UDT MENUITEMINFO: TYPE MENUITEMINFO cbSize AS LONG fMask AS LONG fType AS LONG fState AS LONG wID AS LONG hSubMenu AS LONG hbmpChecked AS LONG hbmpUnchecked AS LONG dwItemData AS _OFFSET dwTypeData AS _OFFSET cch AS LONG 'hbmpItem AS LONG <--- this not in original but specified in Microsoft docs. END TYPE seems to be missing hbmpItem (I added it above remmed out). Second, the function DrawMenuBar is not used anywhere in the code to actually generate the menu. I'm wondering if somehow some of the code has been omitted during a copy/paste into this Wiki? CreateMenu is returning a valid hMenu handle value. I believe the problem is with the first InsertMenuItemA function encountered, but according to what I've read in the docs it's structure appears fine. Code: (Select All) DEFLNG A-Z RE: API Questions - SpriggsySpriggs - 08-19-2023 I think this was due to the padding and such. I wrote code that worked for 64 bit a while back but I think I lost it. I'd need to look at it again to check the offsets of each member. RE: API Questions - TerryRitchie - 08-19-2023 (08-19-2023, 04:32 AM)SpriggsySpriggs Wrote: I think this was due to the padding and such. I wrote code that worked for 64 bit a while back but I think I lost it. I'd need to look at it again to check the offsets of each member.I was going through my code archives and found a copy of the menu library I copied back in 2012 when version .954 of QB64 was still the latest. Aside from the change to using _WINDOWHANDLE in the version in the Wiki the code is identical. It appears a change in QB64 somewhere along the line broke the code. RE: API Questions - Dav - 08-19-2023 The wiki code is working for me as is. Im In windows 7 32-bit using the current version of QB64-PE. - Dav |