Handle the Windows Lock Desktop event/message?

B

brandon

Anybody out there know how I could go about handling, in a systray'd
app, the windows message that the desktop is going to be locked? This
way the application can react whenever the Win+L or Ctrl+Alt+Del+L is
used to lock the computer. I realize this will probably involve some
Windows API work, and am aware of the LockWorkStation inside the
User32.dll which causes the locking of the computer, but don't know
where to start to find out how to get that message from the API when my
form does NOT have focus. I imagine it will involve a Hook to the OS
somehow, but don't want to just hook the Win+L combo or just the
Ctrl+Alt+Del+L or any other way it locks, so would rather grab the
event.

Thanks!
 
M

Mattias Sjögren

Anybody out there know how I could go about handling, in a systray'd
app, the windows message that the desktop is going to be locked?

Check out the WTSRegisterSessionNotification API.


Mattias
 
W

Willy Denoyette [MVP]

| Anybody out there know how I could go about handling, in a systray'd
| app, the windows message that the desktop is going to be locked? This
| way the application can react whenever the Win+L or Ctrl+Alt+Del+L is
| used to lock the computer. I realize this will probably involve some
| Windows API work, and am aware of the LockWorkStation inside the
| User32.dll which causes the locking of the computer, but don't know
| where to start to find out how to get that message from the API when my
| form does NOT have focus. I imagine it will involve a Hook to the OS
| somehow, but don't want to just hook the Win+L combo or just the
| Ctrl+Alt+Del+L or any other way it locks, so would rather grab the
| event.
|
| Thanks!
|

When running on W2K or higher using framework v2.0, you could set-up a
listener for SystemEvents notifications.
Please check the Microsoft.Win32.SystemEvents SessionSwitchEventHandler for
details.

....
SystemEvents.SessionSwitch += new
SessionSwitchEventHandler (SystemEvents_SessionSwitch);
....
// unhook static eventhandler when application terminates!!!
....
//Handle event
static void SystemEvents_SessionSwitch(object sender,
SessionSwitchEventArgs e)
{
Console.WriteLine("The session is switched. Reason={0}", e.Reason);
}

Willy.
 
B

BC3Tech

Awesome solution Willy! I had no idea that .net 2.0 had this
built-in... definitely a bit easier than working with APIs. I
implemented this solution in about 4.6 seconds ;)

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top