Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tell when Computer is locked and unlocked 1

Status
Not open for further replies.

BrianAtWork

Programmer
Apr 12, 2004
148
US
Good afternoon,

I am trying to write some code in VB.NET that starts a timer when a user locks their windows desktop (either their screen saver is set to lock the computer after nn minutes, or they manually lock their computer), and then stop when the computer is unlocked, and subtract to get the total time the computer was "idle".

I've tried looking through System.Microsoft.Win32 and a few other ones, and I've looked around on MSDN, but I can't find what I should use to let my code know when the user locks or unlocks the computer...

So, has anyone used this, or know how I would do this? I would really appreciate a step in the right direction. It's gotta be there, I'm just not seeing it.

Thanks in advance!

Brian
 
I found something that looks a little simlar to what I want, but not close enough:

In System.Microsoft.Win32 there is a class call SystemEvents. It has 2 events called SessionEnding and SessionEnded. These are pretty much what I want, except these wait for the user to logoff or shutdown the computer. I want to use an event when the user just locks his computer and will be unlocking it shortly after.

Anyone have any ideas?

Thanks!

Brian
 
Hello Brian,

Do these help? I got them from a Buddy so I haven't had a chance to test them. Please repost if they work:

SystemEvents.SessionSwitch – SessionSwitchEventArgs.Reason indicates why the session was “switched”, and includes SessionLock and SessionUnlock. Good Luck!


Have a great day!

j2consulting@yahoo.com
 
Yes, that helps a lot! Thank you so much!

So I tried testing this by writing the following bit of code:
Code:
Imports Microsoft.Win32

Public Class LWatcher

    Private Sub LWatcher_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler SystemEvents.SessionSwitch, AddressOf checkLockUnlock
    End Sub

    Public Sub checkLockUnlock(ByVal sender As Object, ByVal e As SessionSwitchEventArgs)
        ListBox1.Items.Insert(0, "Lock/Unlock")
    End Sub

End Class

As soon as I try to run it, I get this exception:
Code:
System.EntryPointNotFoundException was unhandled
  Message="Unable to find an entry point named 'WTSRegisterSessionNotification' in DLL 'wtsapi32.dll'."
  Source="System"

I also tried setting up a variable like this:
Code:
Public WithEvents SysEvents As SystemEvents
'and then adding
handles SysEvents.SessionSwitch
'after my "checkLockUnlock" sub

But that event doesn't fire at all.

Does anyone know if I'm doing something wrong with my event handling?

Thanks!

Brian
 
Hmmm.. This helps and confuses me at the same time :)

I read the page that you linked to, very interesting. And yes, I see the part where it says it is new to 2.0.

So I went to the Microsoft page to upgrade to 2.0, and on the page it said "You are already running 2.0."

I am running Windows 2000 SP4, which should be supported.

Then I looked at my error message regarding "WTSRegisterSessionNotification". I searched for this in Microsoft, and found a page that said that WTSRegisterSessionNotification requires windows XP - so maybe that is where I am getting stuck. I have a machine with Windows XP home - maybe I will try my program on that machine and see if that works.
 
Sure enough, my first code example works on my XP machine.. Must be a win2k issue. Oh well, Thanks so much for your help buckeye!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top