8

How to detect Windows is locked (Win+L or locked by screensaver)? Is it possible?

1

2 Answers 2

14

There is no documented way to check if the session is locked. There is however a way to detect that the session is getting locked. Use WTSRegisterSessionNotification(), you'll get a WM_WTSSESSION_CHANGE message with the WTS_SESSION_LOCK value when the workstation is being locked.

If you are planning to do this from a service then be sure to google "session 0 isolation" to find out why that doesn't work.

2
  • Thanks! Now I'm reading this link support.microsoft.com/kb/310153. I will try the code later. Dec 22, 2011 at 16:15
  • 4
    Using WTSRegisterSessionNotification() is the only option. MSDN says as much (see msdn.microsoft.com/en-us/library/windows/desktop/aa376875.aspx): "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." Services that use a HandlerEx SCM callback, as opposed to a Handler SCM callback, can receive SERVICE_CONTROL_SESSIONCHANGE notifications from WTSRegisterSessionNotification(). Dec 22, 2011 at 18:47
1

WTSRegisterSessionNotification() will require the elevated privileges on Windows 10/11.

Call WTSQuerySessionInformation with WTS_INFO_CLASS set to WTSSessionInfoEx.

struct _WTSINFOEX_LEVEL1_A has a member, LONG SessionFlags, which indicates whether the calling session is locked or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.