views:

1167

answers:

4

DUPLICATE: How can I programmatically determine if my workstation is locked?

How can I detect (during runtime) when a Windows user has locked their screen (Windows+L) and unlocked it again. I know I could globally track keyboard input, but is it possible to check such thing with environment variables?

+8  A: 

A SessionSwitch event may be your best bet for this. Check the SessionSwitchReason passed through the SessionSwitchEventArgs to find out what kind of switch it is and react appropriately.

Ben S
A: 

You can get this notification via a WM_WTSSESSION_CHANGE message. You must notify Windows that you want to receive these messages via WTSRegisterSessionNotification and unregister with WTSUnRegisterSessionNotification.

These posts should be helpful for a C# implementation.

http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification

http://blogs.msdn.com/shawnfa/archive/2005/05/17/418891.aspx

http://bytes.com/groups/net-c/276963-trapping-when-workstation-locked

Andrew Grant
Beat me by a few seconds :)
shahkalpesh
-1: The managed code version of this was posted before the Win32 version was... why is this even marked as the correct answer?
R. Bemrose
Hardly a reason for a downvote tho, eh?
Andrew Grant
IMO, pinvoke is a last case solution when using .NET. It's better to use methods built into the Framework first.
R. Bemrose
I don't disagree, I just wasn't aware there was an alternative .net method. I've no idea why you cry babies see it as a reason to start downvoting though.
Andrew Grant
A: 

This might help.

shahkalpesh
A: 

You absolutely don't need WM_WTSSESSION_CHANGE Just use internal WTTS apis.