Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Or this solution written by Guinness:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@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
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...