Windows lock/unlock (with code)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chrisli
    New Member
    • Nov 2008
    • 11

    Windows lock/unlock (with code)

    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:
    Code:
        Sub Main()
            AddHandler Microsoft.Win32.SystemEvents.SessionSwitch,
    _ SystemEvents_Sessionswitch()
            Console.ReadLine()
            RemoveHandler Microsoft.Win32.SystemEvents,
    _ SystemEvents_Sessionswitch()
        End Sub
    
        Private Sub SystemEvents_Sessionswitch(ByVal sender As Object, 
    _ ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
            If e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLock Then
                Console.WriteLine("locked at: " & DateTime.Now)
            End If
            If e.Reason = Microsoft.Win32.SessionSwitchReason.SessionUnlock Then
                Console.WriteLine("unlocked at: " & DateTime.Now)
            End If
        End Sub
    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.

    Code:
    using Microsoft.Win32;
    public class App
    {
    static void Main()
    {
    SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
    Console.ReadLine(); 
    SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
    }
    
    static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
    if(e.Reason == SessionSwitchReason.SessionLock)
    {
        Console.WriteLine("locked at {0}", DateTime.Now);
    }
    if(e.Reason == SessionSwitchReason.SessionUnlock)
    {
        Console.WriteLine("unlocked at {0}", DateTime.Now);
    }
    }
    }
    Thank you and sorry for my bad englisch.

    Chris
    Last edited by chrisli; Mar 5 '09, 07:49 AM. Reason: solved
  • chrisli
    New Member
    • Nov 2008
    • 11

    #2
    Ok, i'm an idiot. :)

    Here's the solution:

    Code:
            AddHandler Microsoft.Win32.SystemEvents.SessionSwitch, _
     AddressOf SystemEvents_Sessionswitch
            Console.ReadLine()
            RemoveHandler Microsoft.Win32.SystemEvents.SessionSwitch, _
     AddressOf SystemEvents_Sessionswitch
    The AddressOf was missing. :)

    Comment

    • abljalil
      New Member
      • Oct 2009
      • 3

      #3
      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,unlo ck) . if i restart the service it will work fine and capture all event like (logoff,logon,l ock , unlock)

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        The C# code is a bit flawed.
        Eventhandlers should be added like
        [code=c#]
        SystemEvents.Se ssionSwitch += new SessionSwitchEv entHandler(Syst emEvents_Sessio nSwitch);
        [/code]

        And removed with the -=
        [code=c#]
        SystemEvents.Se ssionSwitch -= new SessionSwitchEv entHandler(Syst emEvents_Sessio nSwitch);
        [/code]

        (There are other ways, but simply using += SystemEvents_Se ssionSwitch should be avoided)


        Are you removing the eventhandler when you shouldn't be?

        Comment

        • abljalil
          New Member
          • Oct 2009
          • 3

          #5
          Windows terminal event lock/unlock ,loggon /loggoff

          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,loc k,unlock it will not capture these event

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            And you are sure your service is running and not paused or busy?
            Hmmm

            Comment

            • abljalil
              New Member
              • Oct 2009
              • 3

              #7
              i have find the solution.the SystemEvent depend on TermService(Ter minal Service) i set my service dependence on TermService now it will work fine

              Comment

              Working...