views:

365

answers:

3

I have an application which runs every hour on a Windows XP Machine. To run properly, this application requires the current session to be unlocked. So I was wondering if there a way to know if the current Windows session is locked or not with C# and .NET 3.5.

UPDATE: The application cannot listen to the SessionNotification events. The application can be kicked off anytime and terminates when completed.

Thanks!

+1  A: 

These are untested by me, but look interesting. I'd give them a test, but my Visual Studio appears to be less than happy with me at the moment.

Dan F
A: 

I once used the GetForegroundWindow() function to determine whether a screen saver was running. If the return value was NULL, then the screen saver (or, presumably, a locked workstation) was active. Note that this was in the early days of Windows NT, is not documented to do this, and there's no guarantee that it will still do the same thing today.

Looking at the Win32 API reference, you may be able to use something like OpenInputDesktop() to get the HDESK of the currently active desktop. If you can't get a handle to the desktop or if it's different from your thread desktop, then your app is running in a desktop that is not active (and the workstation is probably either in a screensaver state or is locked). No guarantees that this method will work, but it might be worth investigating.

Greg Hewgill
A: 

I answered a similar question here. I think that methods 2 & 3 can be enhanced to detect a locked state.

On Freund