Sign in to follow this  
Followers 0
TheMightyPope

Running AutoIt as process on hidden desktop

4 posts in this topic

Hi,

I need to launch an installer, and I am using AutoIt to simulate keypresses and handling all the automatic selections for the installer. This works great on my regular desktop.

However, I wish to do this silently, i.e. hidden for the user. So I've written a small C++ program, which launches my AutoIt executable script in a secondary desktop (which is hidden).

I can see in my process monitor that both AutoIt and the installer starts just fine, but debugging reveals that:

a: The windows on the hidden desktop exists, but never become ac

  • The windows on the hidden desktop exists, but never become active
  • WinWait never finds the windows/dialogs

To create the hidden desktop, I'm using this C++ call:

CreateDesktop(L"OpenCandyDesktop", NULL, NULL, 0,
DESKTOP_CREATEWINDOW    |   GENERIC_EXECUTE | GENERIC_WRITE |GENERIC_READ |
DESKTOP_WRITEOBJECTS    | DESKTOP_SWITCHDESKTOP  |
STANDARD_RIGHTS_WRITE   | 
STANDARD_RIGHTS_EXECUTE |
DESKTOP_READOBJECTS, NULL);

(I realize I'm overkilling with the flags, but it doesn't hurt any. The GENERIC flags includes the others)

I then create the AutoIt tasks like this:

CreateProcess((LPWSTR)fullExePath.c_str(), 
(LPWSTR)cmdParameters.c_str(), NULL,
NULL, TRUE, CREATE_SUSPENDED, 
NULL, NULL,
&si, piOut))

With the si.lpDesktop set to the new desktop.

(Basically it works, the threads runs fine, the programs starts on the hidden desktop, and autoit launches).

Now, I've played with this for a while and here's the thing: The AutoIt script will sit and wait for WinWait, until I perform a SwitchDesktop(..) call to the new desktop. As soon as it becomes visible, the AutoIt scripts leaps into action.

Here's the script:

; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
; Author:         Jonathan Bennett (jon@hiddensoft.com)
;
; Script Function:
;   Plays with the calculator.
;


; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
;$answer = MsgBox(4, "AutoIt Example (English Only)", "This script will run the calculator and type in 2 x 4 x 8 x 16 and then quit.  Run?")


; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
;If $answer = 7 Then
    ;MsgBox(0, "AutoIt", "OK.  Bye!")
    ;Exit
;EndIf

$file = FileOpen("C:\test.txt", 2);2 = Write mode (erase previous contents)
FileWriteLine($file, "Starting Calculator Script !")

; Run the calculator
If Run("calc.exe") = 0 Then
    FileWriteLine($file, "Run Error")
Else
    FileWriteLine($file, "Cool Runnings")
EndIf

Sleep(1000)

FileWriteLine($file, "----- Visible Windows List -----")

$calhandle = 0
$winlist = WinList()
if IsArray($winlist) Then
    For $i = 0 to UBound($winlist) -1
        If IsVisible($winlist[$i][1]) Then
            FileWriteLine($file, $winlist[$i][0])
        EndIf
        If $winlist[$i][0] = "Calculator" Then
            $calhandle = $winlist[$i][1]
        EndIf
    Next
EndIf

FileWriteLine($file, "-----")

;WinClose("MSCTFIME UI")

if WinActivate("Calculator") = 0 Then
    FileWriteLine($file, "Could not activate window")
EndIf

FileWriteLine($file, WinGetState("Calculator"))


; Wait for the calulator become active - it is titled "Calculator" on English systems
if WinWaitActive("Calculator","", 10) = 0 Then
    FileWriteLine($file, "Could not find Calculator Window or not active")
;   Exit(1)

Else
    FileWriteLine($file, "active")
EndIf

; Now that the calc window is active type 2 x 4 x 8 x 16
; Use AutoItSetOption to slow down the typing speed so we can see it :)
AutoItSetOption("SendKeyDelay", 400)
Send("2*4*8*16=")
Sleep(2000)
FileWriteLine($file, "calculating")


; Now quit by sending a "close" request to the calc
WinClose("Calculator")


; Now wait for calc to close before continuing
WinWaitClose("Calculator")

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then
    Return 1
  Else
    Return 0
  EndIf

EndFunc
; Finished!

So, my question is: Can AutoIt work on a hidden desktop at all, using the Win... functions?

Share this post


Link to post
Share on other sites



Win* functions require a windows to work.

Windows are not created for non-logged in users.

Although this isn't a limitation of AutoIt, it's by-design for Windows OSs'

Share this post


Link to post
Share on other sites

Windows are not created for non-logged in users.

That's not quite true. Windows can be created, but as the OP said, can never be active. You can for example run an installer on your desktop while the desktop is locked. Windows will come and go, and your script can interact with them in any way that does not require the window to be active (i.e. use ControlSend() vice Send()).

:idea:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

That's not quite true. Windows can be created, but as the OP said, can never be active. You can for example run an installer on your desktop while the desktop is locked. Windows will come and go, and your script can interact with them in any way that does not require the window to be active (i.e. use ControlSend() vice Send()).

:idea:

Thanks you. I actually managed to get my script running using ControlSend, and finding the control ID's, instead of relying on the Win... functions. I was hoping I could easily record my script, but the recorder generate win.. events, instead of control.. events.

Does anyone know if there is a way to change the recorder to use the control.. events instead?

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  
Followers 0