Jump to content

Get Handle from file


russell
 Share

Recommended Posts

Ok been going in circles with this. I have a script that saves the active windows to windows.txt Then another script is to activate the window based on the handle in the txt as below

$file = FileOpen("window.txt", 0)
$read = FileRead($file, 100)
FileClose($file)

WinGetHandle ($read)
$window = $read
MsgBox (-1,"tilte",$window)

$handle = WinGetHandle($window)
WinActivate($handle)

The msgbox shows the correct handle but i from there is wont activate the windows. And the window with that handle is still open in background.

muppet hands are so soft :)

Link to comment
Share on other sites

WinGetHandle gets the window handle from the windows title. If the file your reading already contains the handle of window then you don't need WinGetHandle.

This will work just fine.

$file = FileOpen("window.txt", 0)
$window = FileRead($file, 100)
FileClose($file)

MsgBox (-1,"title",$window)
WinActivate($window)
Edited by Deltaforce229

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

That was the first thing i tried but for some reason it wont activate the window. So i made sure the window was open, Check. Then use window info tool to check handle, check! Same as files content. The msgbox was to let me know what it saw. = Same as window in foreground. Im so lost

Edited by russell

muppet hands are so soft :)

Link to comment
Share on other sites

Oh duh I'm an idiot. WinActivate requires a window title not a window handle. Give me a minute here while I get my head on straight.

Edit: Ok according to the help file you can use a window handle in place of a window title so the code should work.

Edit: OK try this.

AutoItSetOption("WinTitleMatchMode", 4)
$file = FileOpen("window.txt", 0)
$window = FileRead($file, 100)
FileClose($file)

MsgBox (-1,"title",$window)
WinActivate($window)
Edited by Deltaforce229

[size="1"]Please stop confusing "how to" with "how do"[/size]

Link to comment
Share on other sites

There are 2 reasons why this may not be working.

1) The value of $window as read from the file is not a windows handle, it is a string representation of the handle value. Before you can use it must be converted to a  HWnd  as shown below.

$window = HWnd ( $window )

2) A windows handle will be different every time it is created, so unless the handles stored in your text file are those of existing windows they are very unlikely to be still valid.  

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

the window is in the foreground and since it hasn't been closed the handle will remain the same. The pictures show that the file is pulling the correct handle. The only difference in the out come of the two pics is one activates it and the other does not. The one that doesn't id the lower one. Where i dont use the txt. I will try your addin and see if that has any effect on it

muppet hands are so soft :)

Link to comment
Share on other sites

Here is a general test done with two scripts and notepad. The first opens notepad and creates the txt with the handle. The second opens the txt shows the handle in a message box then is told to activate that window. Same scenario and same results.

CREATE HANDLE TXT

Global $tempdir = @ScriptDir
Global $savefile = "notepadhandle.txt"

$createhandles = FileOpen($tempdir & Chr(92) & $savefile, 10)

AutoItSetOption("WinTitleMatchMode", 4); switch WinGet to handle instead of class
Run("notepad.exe")
WinWait("[Notepad]")

Sleep (1000)
$NotepadH = WinGetHandle("[Active]"); get handle of active window


FileWrite($createhandles, $NotepadH  & @LF)

READ HANDEL txt

$file = FileOpen("notepadhandle.txt", 0)
$window = FileRead ($file, 100)


MsgBox (-1,"title",$window)
WinActivate ($window )
FileClose($file)

I did this so everyone can try it and see the results im getting

muppet hands are so soft :)

Link to comment
Share on other sites

I've combined the two scripts and added a few consolewrites, which might clarify things.

;file create script
Global $tempdir = @ScriptDir
Global $savefile = "notepadhandle.txt"
$createhandles = FileOpen($tempdir & Chr(92) & $savefile, 10)
AutoItSetOption("WinTitleMatchMode", 4); switch WinGet to handle instead of class
Run("notepad.exe")
WinWait("[Notepad]")
Sleep (1000)
$NotepadH = WinGetHandle("[Active]"); get handle of active window
FileWrite($createhandles, $NotepadH & @LF)
;end file create script
Sleep(500)

;file read script
$file = FileOpen("notepadhandle.txt", 0)
$window = FileRead ($file, 100)
WinActivate ($window )
FileClose($file)
;end file read script

;handle comparison
ConsoleWrite('The retrieved handle, stored in $NotepadH = "' & $NotepadH & '"' & @CRLF)
ConsoleWrite('The length of $NotepadH   = ' & StringLen($NotepadH) & ' characters' & @CRLF)
ConsoleWrite('The variable type of$NotepadH     = ' & VarGetType($NotepadH) & @CRLF & @CRLF)

ConsoleWrite('The retrieved handle, stored in $window = "' & $window & '"' & @CRLF)
ConsoleWrite('The length of $window     = ' & StringLen($window) & ' characters' & @CRLF)
ConsoleWrite('The variable type $window     = ' & VarGetType($window) & @CRLF)
;end handle comparison
Link to comment
Share on other sites

Didn't want to combine because the purpose is to pass between 6 scripts. Each knowing the window and identifying its handle. Thats why i presented it in two scripts. When i combine all scripts it seem not have trouble because i dont even need to read from a file. I can reference a variable declared earlier but when script are split i have to declare the variable (handle ) in each script. So i use a txt todo that.

I've combined the two scripts and added a few consolewrites, which might clarify things.

;file create script
Global $tempdir = @ScriptDir
Global $savefile = "notepadhandle.txt"
$createhandles = FileOpen($tempdir & Chr(92) & $savefile, 10)
AutoItSetOption("WinTitleMatchMode", 4); switch WinGet to handle instead of class
Run("notepad.exe")
WinWait("[Notepad]")
Sleep (1000)
$NotepadH = WinGetHandle("[Active]"); get handle of active window
FileWrite($createhandles, $NotepadH & @LF)
;end file create script
Sleep(500)

;file read script
$file = FileOpen("notepadhandle.txt", 0)
$window = FileRead ($file, 100)
WinActivate ($window )
FileClose($file)
;end file read script

;handle comparison
ConsoleWrite('The retrieved handle, stored in $NotepadH = "' & $NotepadH & '"' & @CRLF)
ConsoleWrite('The length of $NotepadH   = ' & StringLen($NotepadH) & ' characters' & @CRLF)
ConsoleWrite('The variable type of$NotepadH     = ' & VarGetType($NotepadH) & @CRLF & @CRLF)

ConsoleWrite('The retrieved handle, stored in $window = "' & $window & '"' & @CRLF)
ConsoleWrite('The length of $window     = ' & StringLen($window) & ' characters' & @CRLF)
ConsoleWrite('The variable type $window     = ' & VarGetType($window) & @CRLF)
;end handle comparison

muppet hands are so soft :)

Link to comment
Share on other sites

Hmm i see, one is ptr format and only 10 characters and the other is 11 and string. Ok it would seem Tvern has found what could be causing the problem. But how is it corrected I tired different combinations of WHnd and (String) but no luck

Edited by russell

muppet hands are so soft :)

Link to comment
Share on other sites

... How is it corrected I tired different combinations of WHnd and (String) but no luck

The trick is that HWnd() needs a properly formatted expression and your string is not properly formatted. From the consolewrite you can see that there is a vertical whitespace character in the string read from the text file. Because of that the interpeter can't convert it properly. So you need to trim that first.

Run this example once as is, to see what happens when you feed the original string to HWnd.

Then uncomment the StringTrimRight, which will remove the vertical whitespace and run it again to see how it should work.

;file create script
Global $tempdir = @ScriptDir
Global $savefile = "notepadhandle.txt"
$createhandles = FileOpen($tempdir & Chr(92) & $savefile, 10)
AutoItSetOption("WinTitleMatchMode", 4); switch WinGet to handle instead of class
Run("notepad.exe")
WinWait("[Notepad]")
Sleep (1000)
$NotepadH = WinGetHandle("[Active]"); get handle of active window
FileWrite($createhandles, $NotepadH & @LF)
;end file create script
Sleep(500)

;file read script
$file = FileOpen("notepadhandle.txt", 0)
$window = FileRead ($file, 100)
WinActivate ($window )
FileClose($file)
;end file read script

;handle comparison
ConsoleWrite('The retrieved handle, stored in $NotepadH = "' & $NotepadH & '"' & @CRLF)
ConsoleWrite('The length of $NotepadH = ' & StringLen($NotepadH) & ' characters' & @CRLF)
ConsoleWrite('The variable type of$NotepadH     = ' & VarGetType($NotepadH) & @CRLF & @CRLF)

;~ $window = StringTrimRight($window,1) ;first run without this line, then uncomment
$window = HWnd($window)

ConsoleWrite('The retrieved handle, stored in $window = "' & $window & '"' & @CRLF)
ConsoleWrite('The length of $window     = ' & StringLen($window) & ' characters' & @CRLF)
ConsoleWrite('The variable type $window     = ' & VarGetType($window) & @CRLF)
;end handle comparison
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...