#758 closed Bug (No Bug)
ControlGetHandle() does not work if controlID is passed as string type variable
| Reported by: | shEiD | Owned by: | |
|---|---|---|---|
| Milestone: | Component: | AutoIt | |
| Version: | 3.3.0.0 | Severity: | None |
| Keywords: | Cc: |
Description
In my script, I get controlID variable from registry with RegRead(). RegRead() returns value as string, although it is a number.
After some confusing errors, I found, that ControlGetHandle("title", "text", controlID) function does not work, if you pass a string type variable for controlID.
In the example I simply set the controlID variable and change it to string and back to int:
#include <GuiToolBar.au3>
Opt("WinTitleMatchMode", 2)
$toolbar_id = 59392
$h_wnd = WinGetHandle($win_title)
; does not work
$toolbar_id = String($toolbar_id)
$h_toolbar = ControlGetHandle($h_wnd, "", $toolbar_id)
ConsoleWrite('$h_toolbar = ' & $h_toolbar & @CRLF)
; convert back to Int and it works!
$toolbar_id = Int($toolbar_id)
$h_toolbar = ControlGetHandle($h_wnd, "", $toolbar_id)
ConsoleWrite('$h_toolbar = ' & $h_toolbar & @CRLF)
The console output after running this code:
$h_toolbar =
$h_toolbar = 0x008908D6
Attachments (0)
Change History (2)
comment:1 by , 17 years ago
| Resolution: | → No Bug |
|---|---|
| Status: | new → closed |
comment:2 by , 17 years ago
Just to expand on my previous comment, it's not a bug. AutoIt only looks at "is the variable a number or is it a string". The contents of the string don't matter. This is the correct behavior because if it tried to treat all numeric strings as control ID's it would become impossible to find a control that had numeric text.
So once again, casting is the correct thing to do. Any time you read from a file, an INI file, the registry, user input via GUI or just about anywhere, you're probably going to need to cast the returned value via the Number() function because almost invariably it will be returned as a string.

This is not a bug and the behavior is correct. Casting via Number() or Int() is the correct thing to do.