在C#中记录登录、注销、系统锁定/解锁事件

3

我计划开发一个应用程序,它可以随Windows启动(可能是一个服务),并静默记录用户的登录、注销和系统锁定/解锁事件。

如果有人能给我一个提示来记录他们的总闲置时间(即用户没有进行任何系统活动的时间),那就太棒了。

我正在寻找一个预先开发的库或其他东西,可以帮助我顺利完成这个项目。

2个回答

5

请尝试运行以下代码...

private enum WTS
{
        CONSOLE_CONNECT = 1,
        CONSOLE_DISCONNECT = 2,
        REMOTE_CONNECT = 3,
        REMOTE_DISCONNECT = 4,
        SESSION_LOGON = 5,
        SESSION_LOGOFF = 6,
        SESSION_LOCK = 7,
        SESSION_UNLOCK = 8,
        SESSION_REMOTE_CONTROL = 9
 }

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    switch (m.Msg) {
        case WM_WTSSESSION_CHANGE:
            switch (m.WParam.ToInt32) {
                case WTS.CONSOLE_CONNECT:
                    //MessageBox.Show("A session was connected to the console session.");

                    break;
                case WTS.CONSOLE_DISCONNECT:
                    //MessageBox.Show("A session was disconnected from the console session.");

                    break;
                case WTS.REMOTE_CONNECT:
                    break;
                //MessageBox.Show("A session was connected to the remote session.");
                case WTS.REMOTE_DISCONNECT:
                    break;
                //MessageBox.Show("A session was disconnected from the remote session.");
                case WTS.SESSION_LOGON:

                    break;
                //MessageBox.Show("A user has logged on to the session.")
                case WTS.SESSION_LOGOFF:
                    //MessageBox.Show("A user has logged off the session.");

                    break;
                case WTS.SESSION_LOCK:
                    //MessageBox.Show("A session has been locked.")

                    break;
                case WTS.SESSION_UNLOCK:
                    //MessageBox.Show("A session has been unlocked.")

                    break;
                case WTS.SESSION_REMOTE_CONTROL:
                    MessageBox.Show("A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric.");
                    break;
            }

            break;
    }

    base.WndProc(m);
}

2
我在构建时遇到了一些错误:当前上下文中不存在名称“WM_WTSSESSION_CHANGE”。无法将类型“WindowsApplication2.Form1.WTS”隐式转换为“方法组”。请帮忙!!! - M Kamran Asif
声明变量为私有整数 WM_WTSSESSION_CHANGE = &H2B1。 - Nitin Vijay
请看我下面的帖子。我有VB.NET中的代码。如果您正在使用C#,请转换以下代码。我在上面的帖子中只展示了概念。现在我复制了我的工作代码... - Nitin Vijay
你能否将整个VS解决方案压缩并附上呢? 或者发送邮件至kamisheikh@gmail.com 非常感激...谢谢 - M Kamran Asif
好的..我已经把代码转换成C#了,但是仍然出现同样的错误,即“无法隐式转换类型'Test_LoginLogout_Check.Form1.WTS'为'method group'”,这个错误发生了9次。 - M Kamran Asif
显示剩余4条评论

0
Private Declare Function WTSRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr, ByVal THISSESS As Integer) As Integer
    Private Declare Function WTSUnRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr) As Integer

    Private Const NOTIFY_FOR_ALL_SESSIONS As Integer = 1
    Private Const NOTIFY_FOR_THIS_SESSION As Integer = 0
    Private Const WM_WTSSESSION_CHANGE As Integer = &H2B1



    Private Enum WTS
        CONSOLE_CONNECT = 1
        CONSOLE_DISCONNECT = 2
        REMOTE_CONNECT = 3
        REMOTE_DISCONNECT = 4
        SESSION_LOGON = 5
        SESSION_LOGOFF = 6
        SESSION_LOCK = 7
        SESSION_UNLOCK = 8
        SESSION_REMOTE_CONTROL = 9
    End Enum
    Shared FileName As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        WTSRegisterSessionNotification(Me.Handle, NOTIFY_FOR_ALL_SESSIONS)
        Timer1.Start()
        Me.Hide()
        FileName = Application.StartupPath + "/Data/TimeSheet_" + DateTime.Today.ToString("dd-MMM-yyyy") + ".txt"
        System.IO.File.Create(FileName)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_WTSSESSION_CHANGE
                Select Case m.WParam.ToInt32
                    Case WTS.CONSOLE_CONNECT
                        'MessageBox.Show("A session was connected to the console session.")
                        Timer1.Start()
                        System.IO.File.AppendAllText(FileName, "IN - " + lblTime.Text)
                    Case WTS.CONSOLE_DISCONNECT
                        'MessageBox.Show("A session was disconnected from the console session.")
                        System.IO.File.AppendAllText(FileName, "OUT - " + lblTime.Text + vbCrLf)
                        Timer1.Stop()
                    Case WTS.REMOTE_CONNECT
                        'MessageBox.Show("A session was connected to the remote session.")
                    Case WTS.REMOTE_DISCONNECT
                        'MessageBox.Show("A session was disconnected from the remote session.")
                    Case WTS.SESSION_LOGON
                        Timer1.Start()
                        System.IO.File.AppendAllText(FileName, "IN - " + lblTime.Text)
                        'MessageBox.Show("A user has logged on to the session.")
                    Case WTS.SESSION_LOGOFF
                        'MessageBox.Show("A user has logged off the session.")
                        System.IO.File.AppendAllText(FileName, "OUT - " + lblTime.Text + vbCrLf)
                        Timer1.Stop()
                    Case WTS.SESSION_LOCK
                        'MessageBox.Show("A session has been locked.")
                        System.IO.File.AppendAllText(FileName, "OUT - " + lblTime.Text + vbCrLf)
                        Timer1.Stop()
                    Case WTS.SESSION_UNLOCK
                        'MessageBox.Show("A session has been unlocked.")
                        Timer1.Start()
                        System.IO.File.AppendAllText(FileName, "IN - " + lblTime.Text)
                        If m_clsBalloonEvents Is Nothing Then
                            'allow our ballon class to create the event class for us, hooking into the notify icon that will 
                            'be associated with the balloons...
                            m_clsBalloonEvents = Balloon.HookBalloonEvents(niMain)
                            'add handlers for the events that we are concerned with in this application...
                            AddHandler m_clsBalloonEvents.BallonClicked, AddressOf BalloonClicked
                            AddHandler m_clsBalloonEvents.BallonHidden, AddressOf BalloonHidden
                        End If
                        Balloon.DisplayBalloon(niMain, "Today's Time", lblTime.Text, CType(IIf(False, Balloon.BalloonMessageTypes.Error, Balloon.BalloonMessageTypes.Info), Balloon.BalloonMessageTypes))
                    Case WTS.SESSION_REMOTE_CONTROL
                        MessageBox.Show("A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric.")
                End Select

        End Select

        MyBase.WndProc(m)
    End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接