Jump to content
Sign in to follow this  
MarkRobbins

Why is Scripting.Dictionary.Add(hwnd) munging its reference?

Recommended Posts

MarkRobbins

I am posting this to get information on other gotchas like this... what shouldn't I be passing to a dictionary?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


global $gc_oForms=ObjCreate("Scripting.Dictionary")
global $make_error_happen=false
$make_error_happen=true

; Example shows destruction of hwnd by passing it to a dictionary object .Add, evidently by ref
; Is there something to know about handles and dictionaries?
;
Example1()


; example 1
Func Example1()
Local $msg
;
local $hGui=GUICreate("My GUI") ; will create a dialog box that when displayed is centered
;A valid hwnd
MsgBox(0,'Handle1',$hGui)
if $make_error_happen Then
     ;pass handle directly which will mung the $hGui variable up
     $gc_oForms.Add('stuff',$hGui)
     ;$hGui is invalid now, has been transformed - Autoit at fault or Dictionary?
     MsgBox(0,'Handle2',$hGui)
     MsgBox(0,'Handle3',$gc_oForms.Item('stuff'))
endif
local $cid=GUICtrlCreateButton("Darn Button",0,0)
;if $make_error_happen then this next call fails
local $p=ControlGetPos($hGui, "", $cid );xywh
if @error then
     MsgBox(0,"ERROR HERE",'@error:'&@error)
endif
;
GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
     $msg = GUIGetMsg()

     If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
EndFunc ;==>Example1

Share this post


Link to post
Share on other sites
BrewManNH

It's not "munging" it up, it's displaying it as a decimal number instead of a hexadecimal number.

Change the second message box to this "MsgBox(0, 'Handle2', hex($hGui))" to see what I'm talking about, you will see that the 2 numbers are the same.

EDIT: It is converting the variable from a handle to a number though, so I suppose that could be a bug, although I'm not sure if it's AutoIt or the dictionary object that is doing it. This is why the controlgetpos isn't working, you no longer have a handle reference, you have a number.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
kylomas

MarkRobbins,

Try this

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


global $gc_oForms=ObjCreate("Scripting.Dictionary")
global $make_error_happen=false
$make_error_happen=true

; Example shows destruction of hwnd by passing it to a dictionary object .Add, evidently by ref
; Is there something to know about handles and dictionaries?
;
Example1()


; example 1
Func Example1()
Local $msg
;
local $hGui=GUICreate("My GUI") ; will create a dialog box that when displayed is centered
;A valid hwnd
MsgBox(0,'Handle1',$hGui)

; force type to "hwnd"

if $make_error_happen Then
     $gc_oForms.Add('stuff',hwnd($hGui))                
     MsgBox(0,'Handle2',$hGui)
     MsgBox(0,'Handle3',hwnd($gc_oForms.Item('stuff')))
endif
local $cid=GUICtrlCreateButton("Darn Button",0,0)
;if $make_error_happen then this next call fails
local $p=ControlGetPos($hGui, "", $cid );xywh
if @error then
     MsgBox(0,"ERROR HERE",'@error:'&@error)
endif
;
GUISetState(@SW_SHOW) ; will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
     $msg = GUIGetMsg()

     If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()
EndFunc ;==>Example1

kylomas

edit:spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
BrewManNH

@kylomas

Nice approach, I never knew about the HWnd function.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
kylomas

Thanks...


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
MarkRobbins

@kylomas

The HWnd($hGui) solution operates the same as assigning $hGui to a var you know will get trashed, and then passing that. AFAIK a Scripting.Dictionary should be able to hold any type and be keyed on any type that supports == comparison (ie no Arrays).

Poking around I found this interesting:

local $h=WinGetHandle(...;Get a valid hwnd however you like

;make sure the window gets destroyed
WinClose($h)
IsValidHwnd($h);==false

Func IsValidHwnd($h)
if int('0x'&HEX($h))==0 then ; this part is interesting, value of hwnd is now zero since it is closed
return false;
endif
return true;
EndFunc

I have always been looking at hwnds as special ints, not PVOIDS. Which makes me wonder how this is implemented - things get so confusing with memory these days... does windows have a dedicated block that grows (so that invalid handles are left to point at zeroed memory)?

AutoIt must be confusing me, since Hwnd(<arbritrary int>) will produce a zeroed Hwnd if the window does not exist, which I am pretty sure is not the case in C.

Anyhow, I will just put things in a dictionary 'by proxy' whenever I am unsure. Having an HWnd transformed is not expected behavior though, I guess complex AutoIt Types are not capable of being held by Scripting.Dictionary.

Share this post


Link to post
Share on other sites
BrewManNH

$hGUI is is a Windows handle, represented by a hex number, although it's not a number or a string. The dictionary add appears to be doing a ByRef with the number when it's added to the dictionary object, and it's taking the hex number as a string, which converts the original handle to a string.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.