Jump to content

finding a window belonging to specific process


Recommended Posts

Hi all,

I need to launch to instances of the same application, each one in a separate process.

I need a reliable way to find the main window of the application given the process identifier of the required instance.

I have tried with the following code:

Func system_WaitForWindow($windowTitle, $timeout = Default)
    Local $success = WinWait($windowTitle, $system_EMPTY_STRING, $timeout)

    system_AssertTrue($success, _
        system_CreateMessage("WaitForWindow", $windowTitle, $timeout))
    Return $success
EndFunc   ;==>system_WaitForWindow

Func system_WindowFor($processId, $windowTitle, $timeout = Default)
    system_WaitForWindow($windowTitle, $timeout)

    Local $windowId, $i
    Local $windows = WinList($windowTitle)

    For $i = 1 to $windows[0][0]
        $windowId = $windows[$i][1]
        If $processId = system_ProcessOf($windowId) Then
            ExitLoop
        EndIf
    Next

    system_AssertTrue($windowId, system_CreateMessage( _
                "WindowFor", $processId, $windowTitle, $timeout))
    Return $windowId
EndFunc   ;==>system_WindowFor

The problem with this code is that it is not reliable. It just takes some unpredictable time until the window of the required process becomes available. Just asking to wait for some window by its title is not enough. I need it to filter the results by process also.

Any way to achieve this with AutoIt?

Please help,

Adrian.

Link to comment
Share on other sites

Welcome to the forum, adrianherscu!

Maybe this gives you some ideas:

#include <Process.au3>
$PID1 = Run("notepad.exe")
$PID2 = Run("notepad.exe")

If ProcessExists($PID1) Then
    ;do something
    ConsoleWrite("Ooops! There is! Instance no.1:" & $PID1 & "; Executable: " & _ProcessGetName($PID1) & @CRLF)
EndIf

If ProcessExists($PID2) Then
    ;do something
    ConsoleWrite("Ooops! There is! Instance no.2:" & $PID2 & "; Executable: " & _ProcessGetName($PID2) & @CRLF)
EndIf
Edited by taietel
Link to comment
Share on other sites

Hmmm... Seems that a better explanation is required.

I need to test some App that updates a database.

My plan is to launch two instances of App, App1 and App2, each one running in its own process.

Then, I would be able to create/read/update/delete a record in App1 and test that this change can be retrieved in App2.

When AppX process begins to run it creates the main App window, AppWindowX, titled "App".

Since AppX needs to initialize various internal things this behavior is time dependent, hence the test needs to block until a window titled "App" appears.

Then I have:

App1 --> creates --> AppWindow1 (titled "App") <-- the test must block until this window appears

App2 --> creates --> AppWindow2 (titled "App") <-- the test must block until this window appears too

Using the WinWait function is not enough since both windows have the same title.

Any way to do it with AutoIt built-in functions or do I need to call Win32 APIs?

Link to comment
Share on other sites

Hi,

what about WinSetTitle? After that you can distinguish between the AppWindows by name.

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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