This project is read-only.

Check if the desktop is locked...


Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e) 
    if (e.Reason == SessionSwitchReason.SessionLock) 
        //I left my desk 
    else if (e.Reason == SessionSwitchReason.SessionUnlock) 
        //I returned to my desk 
There is no reliable API for detected it from MSDN.
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation. To unlock the workstation, the user must log in. There is no function you can call to determine whether the workstation is locked. To receive notification when the user logs in, use the WTSRegisterSessionNotification function to receive WM_WTSSESSION_CHANGE messages. You can use session notifications to track the desktop state so you know whether it is possible to interact with the user.
If your process is running under user mode, you may call OpenInputDesktop() to detect secure desktops if it fails.
Note this may not what you wanted because it fails for UAC popup/Locked desktop. There is no API for differentiate between Locked Desktop and UAC popup.


FAIZ99 wrote Nov 2, 2014 at 10:57 AM

I think , registering such process as a scheduled task may allow it to run even when user is not logged in and it will also take administrative privileges skipping the UAC popup. Check if it solves your problem. There is also a Logon Trigger in Scheduled Tasks. Give it a try.