Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Windows is locked?

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

like image 757
Charles Sungkono Avatar asked Dec 22 '11 15:12

Charles Sungkono


People also ask

What to do if Windows is locked?

Press CTRL+ALT+DELETE to unlock the computer. Type the logon information for the last logged on user, and then click OK. When the Unlock Computer dialog box disappears, press CTRL+ALT+DELETE and log on normally.

Does Windows still run when locked?

Yes, when the computer is in lock mode the program and you run any programs it will run, if it is in sleep mode, program will stop running.


2 Answers

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.

like image 54
Hans Passant Avatar answered Nov 11 '22 23:11

Hans Passant


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.

like image 1
Morikawa Avatar answered Nov 11 '22 21:11

Morikawa