Jump to content

ShellExecute and WinWaitActive


regg
 Share

Recommended Posts

WinWaitActive is failing when my AutoIt script runs a program and then tries to find it.  What the script needs to do is start a program program and then do things like file open.  Notepad is a simple example. 

--------------example start-------------------------
#include <Process.au3>

MsgBox(0,"##Debug##", "script started" )

;RunWait( "Notepad.exe" )        ; un-Comment one of these
;Run( "Notepad.exe" )
;Local $iRc = _RunDos( "Notepad.exe" )
ShellExecute( "Notepad.exe" )

$h = WinWaitActive("[CLASS:Notepad]", "", 3)        ; Wait 3 seconds for the Notepad window to appear
If $h = 0 Then
    MsgBox(0,"##Debug##", "no notepad window" )
Else
    Sleep( 2000 )
    MsgBox(0,"##Debug##", "notepad window found" )
EndIf

MsgBox(0,"##Debug##", "script ended" )

--------------example end--------------------------

What happens (using Win7): The script starts Notepad, and then waits until Notepad exits.  This is expected for RunWait, but the other ways of starting Notepad do the same thing.  WinWaitActive always fails (unless Notepad is running before the script starts). 

It seems that there is no difference between Run and RunWait.  Did I miss something (maybe AutoItSetOption)?  Is there a way for AutoIt to start a program and then interact with it?

 

Link to comment
Share on other sites

Your script works for me on Windows 7.

Please note that WinWaitActive only works if the Notepad window is actually the window which is currently focused.

You might want to use WinWait() instead, which checks all existing windows for the one you're looking for.

Example:

MsgBox(0,"##Debug##", "script started" )

ShellExecute("Notepad.exe")

$h = WinWait("[CLASS:Notepad]", "", 3)        ; Wait 3 seconds for the Notepad window to exist
If $h = 0 Then
    MsgBox(0,"##Debug##", "no notepad window")
Else
    Sleep( 2000 )
    MsgBox(0,"##Debug##", "notepad window found")
EndIf

MsgBox(0,"##Debug##", "script ended")

 

Another one using ShellExecute and the Process ID that is returned:

Global $ProcessID = ShellExecute("notepad.exe")
Global $SecondsToTimeout = 3
Global $ProcessList
Global $Timer = TimerInit()

While 1
    $ProcessList = ProcessList()
    If TimerDiff($Timer) > $SecondsToTimeout * 1000 Then
        MsgBox(0,"##Debug##", "nothing found - timeout")
        Exit
    EndIf
    If Not IsArray($ProcessList) Then ContinueLoop
    For $i = 1 To $ProcessList[0][0]
        If $ProcessID = $ProcessList[$i][1] Then
            MsgBox(0,"##Debug##", "process found")
            Exit
        EndIf
    Next
WEnd

Just to give you some ideas :)

Link to comment
Share on other sites

2 hours ago, regg said:

It seems that there is no difference between Run and RunWait.

There's a great difference:

  • Run is starting the app and continues with script
  • RunWait is starting the app and waits with script continuing until the app is closed.

Your example with Shellexecute works for me. May be the TimeOut-param in

$h = WinWaitActive("[CLASS:Notepad]", "", 3)        ; Wait 3 seconds for the Notepad window to appear

is to low.

Link to comment
Share on other sites

Hi regg, welcome to the forum.

Just in case something else is taking the focus and interfering, you should incorporate WinActivate just prior to WinWaitActive. You could even incorporate a Sleep and or loop if necessary.

Have a look at the Help file example for the WinActivate command.

P.S. Note, that the Editor has a Code button <> which you should use when posting code, as it makes it much easier for helpers to read. :)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

It looks like my problem was other stuff stealing the 'active' state while the script was looking for Notepad to become active. 

The key idea for me is to use something more reliable than WinWaitActive for this.  WinWait works.  Also, WinExists works even better as it returns a logic 1 or 0. 

Of course, Notepad is not what I actually need to run.  If it was, then it is likely that there would be other Notepad instances running.  The ProcessID method handles that by identifying the checking for the correct instance of Notepad.  Great idea!

Thanks.

Link to comment
Share on other sites

  • 1 year later...

Hello folks!

I know it's an old thread, but anyway, I would like to contribute with my painful, but rewarding experience...

I was about to beat my head against the keyboard trying to automate some Firefox tasks using ShellExecute and some window functions like WinWait, WinWaitActive, WinGetHandle, etc.

What I discovered, and I could prove myself that wasn't just an assumption, is that for some strange reason, ShellExecute randomly returns "0x00000000" as a handle for the resulting window, thus making window functions to fail.

Replacing ShellExecute for Run solved the problem without any other changes to the script. You can check for youself if you will, using this little code below:

This code will fail randomly

ShellExecute("C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "google.com")
MsgBox(64, "Window Handle", WinWait("Google - Mozilla Firefox"))

This code will never fail

Run('"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" google.com')
MsgBox(64, "Window Handle", WinWait("Google - Mozilla Firefox"))


I hope it helps to solve similar issues like the one I had.

Link to comment
Share on other sites

Are you sure that there's nothing wrong with your FireFox installation? I just tried opening firefox 100 times and I got a proper handle each time.

 

Spoiler

Row|Col 0
[0]|0x00250862
[1]|0x0061096C
[2]|0x001809D2
[3]|0x008007F0
[4]|0x002406A4
[5]|0x002A0862
[6]|0x005A07F4
[7]|0x00380840
[8]|0x00500954
[9]|0x00A3095E
[10]|0x006B096C
[11]|0x002C06A4
[12]|0x008B07F0
[13]|0x003E083C
[14]|0x002609CA
[15]|0x008E07F0
[16]|0x0041083C
[17]|0x002909CA
[18]|0x003406A4
[19]|0x009207F0
[20]|0x002C09CA
[21]|0x003409CE
[22]|0x00490840
[23]|0x00610954
[24]|0x003F0862
[25]|0x004A083C
[26]|0x00B7095E
[27]|0x00550950
[28]|0x004109BE
[29]|0x00BA095E
[30]|0x004F083C
[31]|0x00520840
[32]|0x006B0954
[33]|0x007907F4
[34]|0x007506D0
[35]|0x003E0972
[36]|0x004706A4
[37]|0x004509CE
[38]|0x004D0862
[39]|0x004D09BE
[40]|0x005A09B4
[41]|0x007C06D0
[42]|0x005F0840
[43]|0x00520862
[44]|0x008707F4
[45]|0x005D09D0
[46]|0x006109B4
[47]|0x005306A4
[48]|0x0063083C
[49]|0x008506D0
[50]|0x00D1095E
[51]|0x006709B4
[52]|0x00710950
[53]|0x0068083C
[54]|0x00860954
[55]|0x00D6095E
[56]|0x009507F4
[57]|0x00A1096C
[58]|0x006D083C
[59]|0x00780950
[60]|0x009006D0
[61]|0x006206A4
[62]|0x006109CE
[63]|0x00CC0990
[64]|0x00900954
[65]|0x009506D0
[66]|0x007609B4
[67]|0x00AD096C
[68]|0x006F09BE
[69]|0x006A06A4
[70]|0x007B09D0
[71]|0x007F0840
[72]|0x006D06A4
[73]|0x007E083C
[74]|0x009A0954
[75]|0x00B5096C
[76]|0x00750862
[77]|0x008C0950
[78]|0x008209B4
[79]|0x00870840
[80]|0x0085083C
[81]|0x00A706D0
[82]|0x008909D0
[83]|0x007A06A4
[84]|0x00760972
[85]|0x00A60954
[86]|0x008A09B4
[87]|0x007C09CA
[88]|0x00B907F4
[89]|0x009009D0
[90]|0x008209CE
[91]|0x00BC07F4
[92]|0x009E0950
[93]|0x00B306D0
[94]|0x008506A4
[95]|0x008609CA
[96]|0x00C107F4
[97]|0x008806A4
[98]|0x009D0840
[99]|0x009909B4
 

 

Link to comment
Share on other sites

23 hours ago, Floops said:

Are you sure that there's nothing wrong with your FireFox installation? I just tried opening firefox 100 times and I got a proper handle each time.

 

  Reveal hidden contents

Row|Col 0
[0]|0x00250862
[1]|0x0061096C
[2]|0x001809D2
[3]|0x008007F0
[4]|0x002406A4
[5]|0x002A0862
[6]|0x005A07F4
[7]|0x00380840
[8]|0x00500954
[9]|0x00A3095E
[10]|0x006B096C
[11]|0x002C06A4
[12]|0x008B07F0
[13]|0x003E083C
[14]|0x002609CA
[15]|0x008E07F0
[16]|0x0041083C
[17]|0x002909CA
[18]|0x003406A4
[19]|0x009207F0
[20]|0x002C09CA
[21]|0x003409CE
[22]|0x00490840
[23]|0x00610954
[24]|0x003F0862
[25]|0x004A083C
[26]|0x00B7095E
[27]|0x00550950
[28]|0x004109BE
[29]|0x00BA095E
[30]|0x004F083C
[31]|0x00520840
[32]|0x006B0954
[33]|0x007907F4
[34]|0x007506D0
[35]|0x003E0972
[36]|0x004706A4
[37]|0x004509CE
[38]|0x004D0862
[39]|0x004D09BE
[40]|0x005A09B4
[41]|0x007C06D0
[42]|0x005F0840
[43]|0x00520862
[44]|0x008707F4
[45]|0x005D09D0
[46]|0x006109B4
[47]|0x005306A4
[48]|0x0063083C
[49]|0x008506D0
[50]|0x00D1095E
[51]|0x006709B4
[52]|0x00710950
[53]|0x0068083C
[54]|0x00860954
[55]|0x00D6095E
[56]|0x009507F4
[57]|0x00A1096C
[58]|0x006D083C
[59]|0x00780950
[60]|0x009006D0
[61]|0x006206A4
[62]|0x006109CE
[63]|0x00CC0990
[64]|0x00900954
[65]|0x009506D0
[66]|0x007609B4
[67]|0x00AD096C
[68]|0x006F09BE
[69]|0x006A06A4
[70]|0x007B09D0
[71]|0x007F0840
[72]|0x006D06A4
[73]|0x007E083C
[74]|0x009A0954
[75]|0x00B5096C
[76]|0x00750862
[77]|0x008C0950
[78]|0x008209B4
[79]|0x00870840
[80]|0x0085083C
[81]|0x00A706D0
[82]|0x008909D0
[83]|0x007A06A4
[84]|0x00760972
[85]|0x00A60954
[86]|0x008A09B4
[87]|0x007C09CA
[88]|0x00B907F4
[89]|0x009009D0
[90]|0x008209CE
[91]|0x00BC07F4
[92]|0x009E0950
[93]|0x00B306D0
[94]|0x008506A4
[95]|0x008609CA
[96]|0x00C107F4
[97]|0x008806A4
[98]|0x009D0840
[99]|0x009909B4
 

 

Floops, that was my first assumption, so I reinstalled Firefox with a blank profile to avoid possible "dirty remains"...
I tried both 32 and 64 bit versions, with the same result, so I'm sure there's nothing to do with Firefox installation.

I made a loop script to start and close Firefox with ShellExecute and WinClose, and pause with MsgBox just when window handle returns "0x00000000", and the number of occurrences were quite high. Another strange thing is that if window handle is not properly found, it's impossible to close Firefox with WinClose, no matter using title, class, whatever.

Using latest versions of both AutoIt and Firefox, running on my vintage Windows 7 64-bit.

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