Search code examples
windows-10autoit

Launch camera application (Windows 10) using AutoIt


I am trying to launch my camera application (Windows 10) using ShellExecuteWait() (as I want my script to wait until I close the camera app). But I can't seem to get it to work. I tried following code from the help file:

#include <MsgBoxConstants.au3>

Camera()

Func Camera()
   ;Execute Camera and wait for Camera to close
   Local $iReturn = ShellExecuteWait("notepad.exe")

   ;Display the return value
   MsgBox($MB_SYSTEMMODAL, "", "The return code from Notepad was: " & $iReturn)

EndFunc

This displays Notepad. When you close it, a message box appears with the return value. However, as I change notepad.exe to explorer.exe, ShellExecuteWait() seems to fail (and the message box appears straight away).

You can't open the camera on Windows 10 using Run("camera.exe"); like with explorer.exe this fails and the message box appears straight away.

This is the code that I want to work:

#include <MsgBoxConstants.au3>

Camera()

Func Camera()
   ;Execute Camera and wait for Camera to close
   Local $iReturn = ShellExecuteWait("explorer.exe", "shell:AppsFolder\Microsoft.WindowsCamera_8wekyb3d8bbwe!App")

   ;Display the return value
   MsgBox($MB_SYSTEMMODAL, "", "The return code from Notepad was: " & $iReturn)

EndFunc 

Solution

  • ShellExecuteWait() may not work as expected with server-ish processes like explorer. explorer.exe is always running. Another call to it will only tell the already running explorer.exe to display an additional window (like many browsers will not create another instance of itself but just a new tab). You can examine the current process hierarchy with tools like ProcessExplorer (from the Sysinternals Suite). There you can see, that notepad is actually a sub-process of your autoit script where explorer.exe stays a child of svhost.exe.

    You could still try something like this:

    #include <MsgBoxConstants.au3>
    
    Camera()
    
    Func Camera()
       ;Execute Camera and wait for Camera to close
       local $iPID = ShellExecuteWait("explorer.exe", "shell:AppsFolder\Microsoft.WindowsCamera_8wekyb3d8bbwe!App")
       Sleep(3000)
       WinWaitClose("Camera")
    
       ;Display the return value
       MsgBox($MB_SYSTEMMODAL, "", "The Camera was closed." )
    
    EndFunc
    

    With that, your script waits until no more windows called "Camera" exist. Please mind that the result will depend on the OS language (e.g. for me it's "Kamera"). You may be able to use the tool AutoIt Window Info to find language independent characteristics.