Connecting Tech Pros Worldwide Help | Site Map

Windows lock/unlock (with code)

Newbie
 
Join Date: Nov 2008
Location: Germany
Posts: 11
#1: Mar 5 '09
Hi everyone,

i'm trying to Handle the Micosoft.Win32.SessionSwitch Event, so when Windows is going to be locked by the user, i will simply write a line to the console.When it's going to be unlocked, the same.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1.     Sub Main()
  2.         AddHandler Microsoft.Win32.SystemEvents.SessionSwitch,
  3. _ SystemEvents_Sessionswitch()
  4.         Console.ReadLine()
  5.         RemoveHandler Microsoft.Win32.SystemEvents,
  6. _ SystemEvents_Sessionswitch()
  7.     End Sub
  8.  
  9.     Private Sub SystemEvents_Sessionswitch(ByVal sender As Object, 
  10. _ ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
  11.         If e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLock Then
  12.             Console.WriteLine("locked at: " & DateTime.Now)
  13.         End If
  14.         If e.Reason = Microsoft.Win32.SessionSwitchReason.SessionUnlock Then
  15.             Console.WriteLine("unlocked at: " & DateTime.Now)
  16.         End If
  17.     End Sub
  18.  
My Problem is now, that the Add/Remove Handler wants to have the 2 arguments given.

Here's the code in C# again, here it works flawless, without the arguments given.

Expand|Select|Wrap|Line Numbers
  1. using Microsoft.Win32;
  2. public class App
  3. {
  4. static void Main()
  5. {
  6. SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
  7. Console.ReadLine(); 
  8. SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
  9. }
  10.  
  11. static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
  12. {
  13. if(e.Reason == SessionSwitchReason.SessionLock)
  14. {
  15.     Console.WriteLine("locked at {0}", DateTime.Now);
  16. }
  17. if(e.Reason == SessionSwitchReason.SessionUnlock)
  18. {
  19.     Console.WriteLine("unlocked at {0}", DateTime.Now);
  20. }
  21. }
  22. }
Thank you and sorry for my bad englisch.

Chris
Newbie
 
Join Date: Nov 2008
Location: Germany
Posts: 11
#2: Mar 5 '09

re: Windows lock/unlock (with code)


Ok, i'm an idiot. :)

Here's the solution:

Expand|Select|Wrap|Line Numbers
  1.         AddHandler Microsoft.Win32.SystemEvents.SessionSwitch, _
  2.  AddressOf SystemEvents_Sessionswitch
  3.         Console.ReadLine()
  4.         RemoveHandler Microsoft.Win32.SystemEvents.SessionSwitch, _
  5.  AddressOf SystemEvents_Sessionswitch
  6.  
The AddressOf was missing. :)
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: Oct 6 '09

re: Windows lock/unlock (with code)


i am using the "chrisli" code which is given in C# i made a dot net service i will work fine when i login the system and then start it will capture all the event .

The problem is that when i restart the system and login (it will not capture the logon event,lock,unlock) . if i restart the service it will work fine and capture all event like (logoff,logon,lock , unlock)
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Oct 6 '09

re: Windows lock/unlock (with code)


The C# code is a bit flawed.
Eventhandlers should be added like
Expand|Select|Wrap|Line Numbers
  1. SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
  2.  
And removed with the -=
Expand|Select|Wrap|Line Numbers
  1. SystemEvents.SessionSwitch -= new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
  2.  
(There are other ways, but simply using += SystemEvents_SessionSwitch should be avoided)


Are you removing the eventhandler when you shouldn't be?
Newbie
 
Join Date: Oct 2009
Posts: 3
#5: Oct 7 '09

re: Windows lock/unlock (with code)


i have done this but the code is running condition and work in service when i login the system and restart the service in this scenario it will work fine

but the problem is that when i restart the computer and logon,logof,lock,unlock it will not capture these event
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: Oct 7 '09

re: Windows lock/unlock (with code)


And you are sure your service is running and not paused or busy?
Hmmm
Newbie
 
Join Date: Oct 2009
Posts: 3
#7: Oct 8 '09

re: Windows lock/unlock (with code)


i have find the solution.the SystemEvent depend on TermService(Terminal Service) i set my service dependence on TermService now it will work fine
Reply

Tags
event, lock, sessionswitch, unlock, vb.net