Page 1 of 1

[solved] Gadget number from hWnd?

Posted: Sat Mar 02, 2024 4:19 am
by BarryG
Hi all. Back after an extended break to relax and find myself. 8) Apologies to NicTheQuick.

Installed 6.10 beta 6 today, and my app crashes with this line now:

Code: Select all

Gadget_Num = GetDlgCtrlID_(Gadget_hWnd)
Is this no longer supported? I found this workaround:

Code: Select all

Gadget_Num = GetProp_(hWnd,"PB_ID")
So is this the "new" official and future-proof way to do it, or is it a bug with GetDlgCtrlID_() and 6.10?

Thanks.

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 8:04 am
by AZJIO

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 8:54 am
by BarryG
I see. So that other code is using props as well, instead of GetDlgCtrlID_(). Is GetDlgCtrlID_() no longer usable?

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 9:38 am
by breeze4me
The GetDlgCtrlID function returns only a 4-byte value.
I had that problem in versions prior to PB 6.10. It was just that high numbers were rarely assigned as gadget numbers, so it didn't matter.
I don't remember, but in 6.10 something changed and high numbers are assigned more often, which is problematic.
Nowadays I prefer to use "GetWindowLongPtr_(hwnd, #GWLP_ID)".

Code: Select all

If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  h = CreateWindowEx_(0, "BUTTON", "test", #BS_RIGHT | #WS_VISIBLE | #WS_CHILD, 10, 10, 100, 30, WindowID(0), $1234567890ABCDEF, 0, 0)
  If h
    Debug Hex(GetDlgCtrlID_(h))
    Debug Hex(GetWindowLongPtr_(h, #GWLP_ID))
    
    DestroyWindow_(h)
  EndIf
  
  CloseWindow(0)
EndIf
The result is ...
90ABCDEF
1234567890ABCDEF

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 10:24 am
by AZJIO
BarryG
I think external and internal identifiers are not the same thing. You can create an identifier not from 0, I think an array of identifiers is created inside and you access it by array cell, and there is a descriptor there. And the identifier created by WinAPI always starts with 1. If something coincided with you once, it will not necessarily coincide in the future.

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 10:40 am
by BarryG
breeze4me wrote: Sat Mar 02, 2024 9:38 amin 6.10 something changed and high numbers are assigned more often
Ah, okay. That explains why my source works in 6.04 but not 6.10 now. Thanks! I'll use the prop version from now on.

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 11:01 am
by mk-soft
Purebasic also assigns the PB_ID to the CtrlID internally.
This means you can quickly access the PB_ID in WM_NOTIFY via NMHDR\idFrom.
I assume that this is used internally by PB. But it is unofficial.
Since a secure memory management is used and therefore the value can be larger than 4 bytes, you have to check all old codes.

Code: Select all

;-TOP

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("File")
    
    ; StatusBar
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    btn = ButtonGadget(#PB_Any, 10, 10, 120, 25, "Ok")
    btn2 = ButtonGadget(2, 140, 10, 120, 25, "Ok 2")
    
    Debug "Button Any 0x" + RSet(Hex(btn), 16, "0")
    Debug "CtrlID Any 0x" + RSet(Hex(GetWindowLongPtr_(GadgetID(btn), #GWLP_ID)), 16, "0")
    Debug "Button Array 0x" + RSet(Hex(2), 16, "0")
    Debug "CtrlID Array 0x" + RSet(Hex(GetWindowLongPtr_(btn2, #GWLP_ID)), 16, "0")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case 0
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 11:09 am
by Fred
There is some more info here (ASLR flag is now default for the linker): https://www.purebasic.fr/english/viewtopic.php?t=83491

Re: Gadget number from hWnd?

Posted: Sat Mar 02, 2024 12:41 pm
by Mindphazer
BarryG wrote: Sat Mar 02, 2024 4:19 am Hi all. Back after an extended break to relax and find myself
Glad to see you back !

Re: [solved] Gadget number from hWnd?

Posted: Sat Mar 02, 2024 1:09 pm
by BarryG
Thanks! :D