Jump to content
Sign in to follow this  
Chainik

WinActive always returns 0 when running script under Windows Service Manager

Recommended Posts

Chainik

Hello,

I am running the following script on a server using an application service wrapper (tried both FireDaemon and Windows Task Scheduler). When there is no one logged onto the server console, the Win* functions never seem to return success. When someone is logged on to the console, even if it is not under the same username as the service I've created, WinActive returns success. Running the script at the command prompt also functions as expected.

I am using the following simple script as a test, which opens notepad, waits for the window to exist, closes it and exists.

Can anyone confirm that this is a limitation with running AutoIt scripts as services, or am I missing something?

Dim objAutoIt

Set objAutoIt = WScript.CreateObject("AutoItX3.Control")

objAutoIt.Opt "WinDetectHiddenText", 1

objAutoIt.Opt "WinSearchChildren", 1

objAutoIt.Run "C:\WINDOWS\SYSTEM32\NOTEPAD.EXE", ""

objAutoIt.Sleep 1000

If Not objAutoIt.WinActive("Untitled - Notepad") Then

objAutoIt.WinActivate "Untitled - Notepad", ""

While objAutoIt.WinActive("Untitled - Notepad") <> 1

WScript.Echo "Waiting for " & "Untitled - Notepad" & " to become active (" & objAutoIt.WinActive("Untitled - Notepad") & ") ..."

objAutoIt.WinWaitActive "Untitled - Notepad", "", 1

WEnd

End If

' --- exit LANSurveyor application

objAutoIt.Sleep 1000

objAutoIt.WinClose "Untitled - Notepad", ""

If objAutoIt.WinExists("Untitled - Notepad") Then

While objAutoIt.WinExists("Untitled - Notepad") = 1

WScript.Echo "Waiting for " & "Untitled - Notepad" & " to close (" & objAutoIt.WinExists("Untitled - Notepad") & ") ..."

objAutoIt.WinWaitClose "Untitled - Notepad", "", 1

WEnd

End If

Set objAutoIt = Nothing

WScript.Quit

Share this post


Link to post
Share on other sites
Wooltown

I tried the same thing long ago, and discovered that you can't find windows when not logged on, or is locked by screen saver.

Share this post


Link to post
Share on other sites
MHz

If no one is logged on, the system prevents a window to being active so you cannot activate a window. Use non active functions like WinWait, ControlSend and all the other Control* functions.

Untested code. Typed as an example only. It should work though.

Dim objAutoIt

Set objAutoIt = WScript.CreateObject("AutoItX3.Control")

objAutoIt.Opt "WinDetectHiddenText", 1
objAutoIt.Opt "WinSearchChildren", 1

objAutoIt.Run "C:\WINDOWS\SYSTEM32\NOTEPAD.EXE", ""
objAutoIt.WinWait"Untitled - Notepad"
objAutoIt.ControlSend "Untitled - Notepad", "", "Some text"
Set objAutoIt = Nothing

WScript.Quit

Task Scheduler requires the username and password for the current logon user to allow interaction with the desktop.

These limitations would be similar to any automation process.

:)

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
Sign in to follow this  

×