What is the best way to determine if the desktop is locked for the currently logged in user? I looked in MSDN and couldn't find any API calls to detect this. Did I miss something, or is there no simple call I can use?
Duplicate of http://stackoverflow.com/questions/44980/how-can-i-programmatically-determine-if-my-workstation-is-locked
You can use WTSRegisterSessionNotification with WTS_SESSION_LOCK
Former answers are wrong (status changes)
Use the WTS api (islocked)
It's used internally by Windows. Always See on Win32 group for Windows Internal (MS)
The answer depends on whether you want to know if the desktop is locked now, or if you want to be notified when the desktop gets locked (and, presumably, unlocked). It also depends on how you're planning to receive said notifications.
If you really want a one-off test, then the answer here uses
OpenDesktop()
andSwitchDesktop()
to open a handle to the default desktop and activate it - if this fails then it's a good sign that the desktop is locked right now.If you want notification on lock/unlock, and you have a user-mode application with a window and a message pump, then you need to call
WTSRegisterSessionNotification()
and catch theWM_WTSSESSION_CHANGE
message.If you want notifications, and you're running as a Windows service, then you can register for session change events by calling
SetServiceStatus()
and addingSERVICE_ACCEPT_SESSIONCHANGE
todwControlsAccepted
in your status structure. You will then receive callbacks to your own service controlHandlerEx()
function withdwControl
set toSERVICE_CONTROL_SESSIONCHANGE
.