How to detect Windows is locked (Win+L or locked by screensaver)? Is it possible?
            Asked
            
        
        
            Active
            
        
            Viewed 4,114 times
        
    8
            
            
        - 
                    This is an old question, but there are new answer: [For windows 7 and abowe WTS API can be used](https://stackoverflow.com/a/32115698/2148229) – Oleg Korzhukov Aug 21 '19 at 13:51
 
2 Answers
13
            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.
        Hans Passant
        
- 922,412
 - 146
 - 1,693
 - 2,536
 
- 
                    Thanks! Now I'm reading this link http://support.microsoft.com/kb/310153. I will try the code later. – Charles Sungkono Dec 22 '11 at 16:15
 - 
                    4Using `WTSRegisterSessionNotification()` is the only option. MSDN says as much (see http://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()`. – Remy Lebeau Dec 22 '11 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.
        Morikawa
        
- 11
 - 1