[solved] Gadget number from hWnd?

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

[solved] Gadget number from hWnd?

Post 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.
Last edited by BarryG on Sat Mar 02, 2024 1:09 pm, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Gadget number from hWnd?

Post by BarryG »

I see. So that other code is using props as well, instead of GetDlgCtrlID_(). Is GetDlgCtrlID_() no longer usable?
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Gadget number from hWnd?

Post 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
AZJIO
Addict
Addict
Posts: 2226
Joined: Sun May 14, 2017 1:48 am

Re: Gadget number from hWnd?

Post 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.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Gadget number from hWnd?

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Gadget number from hWnd?

Post 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()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Gadget number from hWnd?

Post 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
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Gadget number from hWnd?

Post 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 !
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: [solved] Gadget number from hWnd?

Post by BarryG »

Thanks! :D
Post Reply