Skip to content

Commit 1195ddc

Browse files
committed
Log Windows lock/unlock events
Log a dummy entry in the active window list whenever the system in locked/unlocked. Details of dummy entry are as follows : PID = -1 ProcessName = "Microsoft" MainWindowTitle = "Windows" WindowTitle = "Locked" / "Unlocked" TimeStamp = when system locked / unlocked fixes #2
1 parent 0e69c9a commit 1195ddc

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

ActiveWindowLogger/frmMain.vb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ Public Class frmMain
99
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
1010
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer
1111

12+
Private Declare Function WTSRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr, ByVal THISSESS As Long) As Long
13+
Private Declare Function WTSUnRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr) As Long
14+
15+
Private Const NOTIFY_FOR_ALL_SESSIONS As Integer = 1
16+
Private Const NOTIFY_FOR_THIS_SESSION As Integer = 0
17+
Private Const WM_WTSSESSION_CHANGE As Integer = &H2B1
18+
19+
Private Enum WTS
20+
CONSOLE_CONNECT = 1
21+
CONSOLE_DISCONNECT = 2
22+
REMOTE_CONNECT = 3
23+
REMOTE_DISCONNECT = 4
24+
SESSION_LOGON = 5
25+
SESSION_LOGOFF = 6
26+
SESSION_LOCK = 7
27+
SESSION_UNLOCK = 8
28+
SESSION_REMOTE_CONTROL = 9
29+
End Enum
30+
1231
' Set during app launch.
1332
' Used to track how long the app has been runnning.
1433
Dim AppLaunchTime As New Date
@@ -86,10 +105,58 @@ Public Class frmMain
86105
End Sub
87106

88107
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
108+
WTSRegisterSessionNotification(Me.Handle, NOTIFY_FOR_ALL_SESSIONS)
89109
chkStatus.Checked = True
90110
AppLaunchTime = DateTime.UtcNow
91111
End Sub
92112

113+
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
114+
Dim newEntry As ListViewItem
115+
116+
Select Case m.Msg
117+
Case WM_WTSSESSION_CHANGE
118+
Select Case m.WParam.ToInt32
119+
Case WTS.CONSOLE_CONNECT
120+
'Debug.Print("A session was connected to the console session.")
121+
Case WTS.CONSOLE_DISCONNECT
122+
'Debug.Print("A session was disconnected from the console session.")
123+
Case WTS.REMOTE_CONNECT
124+
'Debug.Print("A session was connected to the remote session.")
125+
Case WTS.REMOTE_DISCONNECT
126+
'Debug.Print("A session was disconnected from the remote session.")
127+
Case WTS.SESSION_LOGON
128+
'Debug.Print("A user has logged on to the session.")
129+
Case WTS.SESSION_LOGOFF
130+
'Debug.Print("A user has logged off the session.")
131+
Case WTS.SESSION_LOCK
132+
'Debug.Print("A session has been locked.")
133+
' Populate the listview with Windows locked entry
134+
newEntry = lvEntries.Items.Insert(0, "-1")
135+
newEntry.SubItems.Add("Microsoft")
136+
newEntry.SubItems.Add("Windows")
137+
newEntry.SubItems.Add("Locked")
138+
newEntry.SubItems.Add(Format(Now, "yyyy/MM/dd HH:mm:ss"))
139+
Case WTS.SESSION_UNLOCK
140+
'Debug.Print("A session has been unlocked.")
141+
' Populate the listview with Windows locked entry
142+
newEntry = lvEntries.Items.Insert(0, "-1")
143+
newEntry.SubItems.Add("Microsoft")
144+
newEntry.SubItems.Add("Windows")
145+
newEntry.SubItems.Add("Unlocked")
146+
newEntry.SubItems.Add(Format(Now, "yyyy/MM/dd HH:mm:ss"))
147+
Case WTS.SESSION_REMOTE_CONTROL
148+
'Debug.Print("A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric.")
149+
End Select
150+
End Select
151+
152+
MyBase.WndProc(m)
153+
154+
End Sub
155+
156+
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
157+
WTSUnRegisterSessionNotification(Me.Handle)
158+
End Sub
159+
93160
Private Sub lvEntries_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles lvEntries.ItemChecked
94161
If (lvEntries.Items.Count <> lvEntries.CheckedItems.Count) And (lvEntries.CheckedItems.Count > 0) Then
95162
chkSelect.CheckState = CheckState.Indeterminate

0 commit comments

Comments
 (0)