Jump to content
birbird

If multiple window match WinGetHandle, what's the expected result

Recommended Posts

birbird

My sinario is to run multiple instances of an application. I think window handle is for me.

I write some test code,  

#include <MsgBoxConstants.au3>

$hWnd1 = Example()
$hWnd2 = Example()
$hWnd3 = Example()

WinClose($hWnd1)
WinClose($hWnd2)
WinClose($hWnd3)

Func Example()
    ; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    WinWait("[CLASS:Notepad]", "", 10)

    ; Retrieve the handle of the Notepad window using the classname of Notepad.
    Local $hWnd = WinGetHandle("[CLASS:Notepad]")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when trying to retrieve the window handle of Notepad.")
        Exit
    EndIf

    ; Display the handle of the Notepad window.
    MsgBox($MB_SYSTEMMODAL, "", $hWnd)

    ; Close the Notepad window using the handle returned by WinGetHandle.
    ; WinClose($hWnd)
    Return $hWnd
EndFunc   ;==>Example

It works well. WinGetHandle returns the desired handle (the one opened last) every time.

But there is no explicit description of the expected behavior of WinGetHandle when multiple windows match.

Is the hehavior stable? And what is the detailed rule?

I guess the result of WinGetHandle should be the same with the 1st element of WinList, is that true? What is the sort rule of WinList?

 

Furthermore, I think a more natural way could be get the window handle from Run function which returns a process id.

Then the question is to get window handle by process id, but I don't see any API can do that.

Is it possible?

 

So many questions... Any help will be highly appreciated :)

Thanks in advance.

 

Edited by birbird

Share this post


Link to post
Share on other sites
birbird

I do some more test with another application.

I get the conclusion the behavior is not stable. It is not always return the handle of last opened window.

So I have to find a way to get the handle from run function, not depending on the WinGetHandle.

Please help.

Share this post


Link to post
Share on other sites
water

Run returns the process id.
There are examples on the forum how to get a list of windows related to this PID. This is one of them:


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Or this solution written by Guinness:


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
birbird

@water, thanks a lot for your reply and nice solution.

I decided to use another simpler strategy. Run a number of instances, use WinList get all of them, then map handle of each one to a logical concept of mine. Later, use the logical concept to get the handle needed.

This solution is suitable for my problem, maybe it can help others.

Edited by birbird

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

×