Function Reference


Makes the specified desktop visible and activates it

#include <WinAPISys.au3>
_WinAPI_SwitchDesktop ( $hDesktop )


$hDesktop Handle to the desktop. This desktop must be associated with the current window station for the process.

Return Value

Success: True
Failure: False


The _WinAPI_SwitchDesktop() function fails if the desktop belongs to an invisible window station. This function
also fails when called from a process that is associated with a secured desktop such as the WinLogon and ScreenSaver
desktops. Processes that are associated with a secured desktop include custom UserInit processes. Such calls
typically fail with an "access denied" error.

See Also

Search SwitchDesktop in MSDN Library.


#include <APIProcConstants.au3>
#include <APISysConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPI.au3>
#include <WinAPIProc.au3>
#include <WinAPISys.au3>

; Retrieve a handle to the current desktop and create a new desktop named "MyDesktop"
Local $hPrev = _WinAPI_GetThreadDesktop(_WinAPI_GetCurrentThreadId())
Local $hDesktop = _WinAPI_CreateDesktop('MyDesktop', BitOR($DESKTOP_CREATEWINDOW, $DESKTOP_SWITCHDESKTOP))
If Not $hDesktop Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Unable to create desktop.')

; Switch to the newly created desktop

; Run "calc.exe" on "MyDesktop" and wait until a process will not be closed by user
Local $pText = _WinAPI_CreateString('MyDesktop')
Local $tProcess = DllStructCreate($tagPROCESS_INFORMATION)
Local $tStartup = DllStructCreate($tagSTARTUPINFO)
DllStructSetData($tStartup, 'Size', DllStructGetSize($tStartup))
DllStructSetData($tStartup, 'Desktop', $pText)
If _WinAPI_CreateProcess('', @SystemDir & '\calc.exe', 0, 0, 0, $CREATE_NEW_PROCESS_GROUP, 0, 0, $tStartup, $tProcess) Then
    ProcessWaitClose(DllStructGetData($tProcess, 'ProcessID'))

; Switch to previous desktop and close "MyDesktop"

; Free memory allocated for a string