Results 1 to 3 of 3

Thread: Service app not getting SystemEvents.SessionSwitch

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2011
    Posts
    246

    Service app not getting SystemEvents.SessionSwitch

    Hi guys,
    My Service app is almost ready, but I have a little problem...
    As everyone knows, a Service can't interact with a GUI (for security reasons), but it also can't interact with SystemEvents.SessionSwitch
    That is the problem...
    I need my service to detect a login or logoff event.
    I found a post (don't remember where) that said you can do it with a Hidden Form, but I can't get it to work...
    Here is my Service Onstart:
    Code:
            Protected Overrides Sub OnStart(args() As String)
                ' Add code here to start your service. This method should set things
                ' in motion so your service can do its work.
    
                'Connect to Server
                Connect()
    
    
                EventLog.WriteEntry("PCMonitor", "Starting PCMonitor")
                Dim t As New Thread(AddressOf RunMessagePump)
                t.Start()
    
            End Sub
    
            Sub RunMessagePump()
            EventLog.WriteEntry("PCMonitor.MessagePump", "Starting PCMonitor Message Pump")
            Application.Run(New HiddenForm())
            End Sub
    Here is my "Hidden Form":
    Code:
    Imports Microsoft.Win32
    Imports System.ComponentModel
    Imports System.Configuration.Install
    Imports System.ServiceProcess
    Imports System.Threading
    Imports System.Windows.Forms
    
    Partial Class HiddenForm
        Inherits Form
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub HiddenForm_Load(sender As Object, e As EventArgs)
            AddHandler SystemEvents.TimeChanged, AddressOf SystemEvents_TimeChanged
            AddHandler SystemEvents.UserPreferenceChanged, AddressOf SystemEvents_UPCChanged
            AddHandler SystemEvents.SessionSwitch, AddressOf SystemEvents_SessionSwitch
            'AddHandler SystemEvents.SessionEnded, AddressOf SystemEvents_SessionEnded
        End Sub
    
        Private Sub HiddenForm_FormClosing(sender As Object, e As FormClosingEventArgs)
            RemoveHandler SystemEvents.TimeChanged, New EventHandler(AddressOf SystemEvents_TimeChanged)
            RemoveHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf SystemEvents_UPCChanged)
            RemoveHandler SystemEvents.SessionSwitch, New SessionSwitchEventHandler(AddressOf SystemEvents_SessionSwitch)
        End Sub
        Private Sub SystemEvents_SessionSwitch(sender As Object, e As SessionSwitchEventArgs)
            EventLog.WriteEntry("PCMonitor.SessionSwitch", e.Reason.ToString)
            'Dim FILE_NAME As String = "C:\Program Files\PCMonitor-Client\Notify\cmd.txt"
    
            'If File.Exists(FILE_NAME) = True Then
    
            '    Dim objWriter As New StreamWriter(FILE_NAME)
    
            '    objWriter.Write(e.Reason.ToString)
            '    objWriter.Close()
            'End If
    
            Select Case e.Reason
                Case SessionSwitchReason.SessionLogoff
                    Thread.Sleep(1000)
                    PCMonitor.SendInfo(PCMonitor.GetString())
                Case SessionSwitchReason.SessionLogon
                    Thread.Sleep(1000)
                    PCMonitor.SendInfo(PCMonitor.GetString())
                Case SessionSwitchReason.SessionUnlock
                    Thread.Sleep(1000)
                    PCMonitor.SendInfo(PCMonitor.GetString())
                Case SessionSwitchReason.SessionLock
                    Thread.Sleep(1000)
                    PCMonitor.SendInfo(PCMonitor.GetString())
            End Select
        End Sub
    
        Private Sub SystemEvents_TimeChanged(sender As Object, e As EventArgs)
            EventLog.WriteEntry("PCMonitor.TimeChanged",
                "Time changed; it is now " & DateTime.Now.ToLongTimeString())
        End Sub
    
        Private Sub SystemEvents_UPCChanged(sender As Object, e As UserPreferenceChangedEventArgs)
            EventLog.WriteEntry("PCMonitor.UserPreferenceChanged", e.Category.ToString())
        End Sub
    End Class
    
    Partial Class HiddenForm
    
        Private components As IContainer = Nothing
    
        Protected Overrides Sub Dispose(disposing As Boolean)
            If disposing AndAlso Not (components Is Nothing) Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        Private Sub InitializeComponent()
            SuspendLayout()
            AutoScaleDimensions = New Drawing.SizeF(6.0F, 13.0F)
            AutoScaleMode = AutoScaleMode.Font
            ClientSize = New Drawing.Size(0, 0)
            FormBorderStyle = FormBorderStyle.None
            Name = "HiddenForm"
            Text = "HiddenForm"
            WindowState = FormWindowState.Minimized
            AddHandler Load, AddressOf HiddenForm_Load
            AddHandler FormClosing, AddressOf HiddenForm_FormClosing
            ResumeLayout(False)
        End Sub
    End Class
    <RunInstaller(False)>
    Public Class SimpleInstaller
        Inherits Installer
    
        Private serviceInstaller As ServiceInstaller
        Private processInstaller As ServiceProcessInstaller
    
        Public Sub New()
            processInstaller = New ServiceProcessInstaller()
            serviceInstaller = New ServiceInstaller()
    
            ' Service will run under system account
            processInstaller.Account = ServiceAccount.LocalSystem
    
            ' Service will have Start Type of Manual
            serviceInstaller.StartType = ServiceStartMode.Automatic
    
            serviceInstaller.ServiceName = "PCMonitor"
    
            Installers.Add(serviceInstaller)
            Installers.Add(processInstaller)
        End Sub
    End Class
    Thanks

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,066

    Re: Service app not getting SystemEvents.SessionSwitch

    I could be wrong about this, but Services run under a special service account... not the user ... so there's no context switching happening. I could be wrong though... probably am... But if not, that would make sense why it's not getting picked up.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2011
    Posts
    246

    Re: Service app not getting SystemEvents.SessionSwitch

    Quote Originally Posted by techgnome View Post
    I could be wrong about this, but Services run under a special service account... not the user ... so there's no context switching happening. I could be wrong though... probably am... But if not, that would make sense why it's not getting picked up.


    -tg
    I read something about it, so I think you are not wrong...
    Do you think there is a work around?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width