tags:

views:

610

answers:

5

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?

A: 

You can hook the WM_WTSSESSION_CHANGE message

Rowland Shaw
+2  A: 

Duplicate of http://stackoverflow.com/questions/44980/how-can-i-programmatically-determine-if-my-workstation-is-locked

Daniel A. White
This only allows you to determine when the lock status changes, not if the current session is actually locked
JaredPar
Not necessarily a duplicate.
snowcrash09
A: 

You can use WTSRegisterSessionNotification with WTS_SESSION_LOCK

Brian R. Bondy
A: 

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)

+2  A: 

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() and SwitchDesktop() 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 the WM_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 adding SERVICE_ACCEPT_SESSIONCHANGE to dwControlsAccepted in your status structure. You will then receive callbacks to your own service control HandlerEx() function with dwControl set to SERVICE_CONTROL_SESSIONCHANGE.

snowcrash09