Skip to content

Commit d72e59c

Browse files
committed
More refactoring and cleanup
Refactor code for AppLaunchtime and lock/unlock registration out of frmMain. Also comment out several logical blocks in frmMain.
1 parent d7239be commit d72e59c

File tree

2 files changed

+40
-76
lines changed

2 files changed

+40
-76
lines changed

ActiveWindowLogger/frmMain.vb

Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
11
Option Explicit On
2-
Imports System.Data
3-
Imports System.Data.OleDb
42

53
Public Class frmMain
64

7-
Private Declare Function WTSRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr, ByVal THISSESS As Long) As Long
8-
Private Declare Function WTSUnRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr) As Long
9-
10-
Private Const NOTIFY_FOR_ALL_SESSIONS As Integer = 1
11-
Private Const NOTIFY_FOR_THIS_SESSION As Integer = 0
12-
Private Const WM_WTSSESSION_CHANGE As Integer = &H2B1
13-
14-
Private Enum WTS
15-
CONSOLE_CONNECT = 1
16-
CONSOLE_DISCONNECT = 2
17-
REMOTE_CONNECT = 3
18-
REMOTE_DISCONNECT = 4
19-
SESSION_LOGON = 5
20-
SESSION_LOGOFF = 6
21-
SESSION_LOCK = 7
22-
SESSION_UNLOCK = 8
23-
SESSION_REMOTE_CONTROL = 9
24-
End Enum
25-
265
Private Sub tmrMinute_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMinute.Tick
276
logStatus()
287
End Sub
@@ -50,50 +29,8 @@ Public Class frmMain
5029
initAppLaunchTime()
5130
End Sub
5231

53-
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
54-
Dim newEntry As ListViewItem
55-
56-
Select Case m.Msg
57-
Case WM_WTSSESSION_CHANGE
58-
Select Case m.WParam.ToInt32
59-
Case WTS.CONSOLE_CONNECT
60-
'Debug.Print("A session was connected to the console session.")
61-
Case WTS.CONSOLE_DISCONNECT
62-
'Debug.Print("A session was disconnected from the console session.")
63-
Case WTS.REMOTE_CONNECT
64-
'Debug.Print("A session was connected to the remote session.")
65-
Case WTS.REMOTE_DISCONNECT
66-
'Debug.Print("A session was disconnected from the remote session.")
67-
Case WTS.SESSION_LOGON
68-
'Debug.Print("A user has logged on to the session.")
69-
Case WTS.SESSION_LOGOFF
70-
'Debug.Print("A user has logged off the session.")
71-
Case WTS.SESSION_LOCK
72-
'Debug.Print("A session has been locked.")
73-
' Populate the listview with Windows locked entry
74-
newEntry = lvEntries.Items.Insert(0, "-1")
75-
newEntry.SubItems.Add("Microsoft")
76-
newEntry.SubItems.Add("Windows")
77-
newEntry.SubItems.Add("Locked")
78-
newEntry.SubItems.Add(Format(Now, "yyyy/MM/dd HH:mm:ss"))
79-
Case WTS.SESSION_UNLOCK
80-
'Debug.Print("A session has been unlocked.")
81-
' Populate the listview with Windows locked entry
82-
newEntry = lvEntries.Items.Insert(0, "-1")
83-
newEntry.SubItems.Add("Microsoft")
84-
newEntry.SubItems.Add("Windows")
85-
newEntry.SubItems.Add("Unlocked")
86-
newEntry.SubItems.Add(Format(Now, "yyyy/MM/dd HH:mm:ss"))
87-
Case WTS.SESSION_REMOTE_CONTROL
88-
'Debug.Print("A session has changed its remote controlled status. To determine the status, call GetSystemMetrics and check the SM_REMOTECONTROL metric.")
89-
End Select
90-
End Select
91-
92-
MyBase.WndProc(m)
93-
94-
End Sub
9532

96-
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
33+
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
9734
WTSUnRegisterSessionNotification(Me.Handle)
9835
End Sub
9936

@@ -139,8 +76,9 @@ Public Class frmMain
13976
.FilterIndex = 0, _
14077
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}
14178

142-
'show the dialog + display the results in a msgbox unless cancelled
143-
79+
'****************************************************************
80+
' show the dialog + export window log unless cancelled
81+
'****************************************************************
14482
If sfd.ShowDialog = DialogResult.OK Then
14583

14684
Dim headers = (From ch In lvEntries.Columns _
@@ -160,7 +98,7 @@ Public Class frmMain
16098

16199
table = table.TrimEnd(CChar(vbCr), CChar(vbLf))
162100

163-
' Add retry/abort here in case file write fails (eg. if file open in other app)
101+
' TODO: Issue#11: Add retry/abort here in case file write fails (eg. if file open in other app)
164102
IO.File.WriteAllText(sfd.FileName, table)
165103

166104
End If
@@ -175,4 +113,5 @@ Public Class frmMain
175113
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
176114
frmAbout.ShowDialog()
177115
End Sub
116+
178117
End Class

ActiveWindowLogger/loggingModule.vb

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
Module loggingModule
22

3+
'****************************************************************
4+
' window monitoring
5+
'****************************************************************
36
Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
47
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpdwProcessID As Integer) As Integer
58
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As IntPtr, ByVal WinTitle As String, ByVal MaxLength As Integer) As Integer
69
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer
710

8-
9-
' Set during app launch.
10-
' Used to track how long the app has been runnning.
11-
Dim AppLaunchTime As New Date
12-
13-
Public Sub initAppLaunchTime()
14-
AppLaunchTime = DateTime.UtcNow
15-
End Sub
16-
1711
Public Sub logStatus()
1812
frmMain.lblStatus.Text = frmMain.lvEntries.Items.Count.ToString + " windows" + vbCrLf + "since last " + DateDiff(DateInterval.Minute, AppLaunchTime, DateTime.UtcNow).ToString + " minutes"
1913
End Sub
@@ -87,4 +81,35 @@
8781

8882
End Sub
8983

84+
'****************************************************************
85+
' system lock/unlock monitoring
86+
'****************************************************************
87+
Public Declare Function WTSRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr, ByVal THISSESS As Long) As Long
88+
Public Declare Function WTSUnRegisterSessionNotification Lib "Wtsapi32" (ByVal hWnd As IntPtr) As Long
89+
90+
Public Const NOTIFY_FOR_ALL_SESSIONS As Integer = 1
91+
Public Const NOTIFY_FOR_THIS_SESSION As Integer = 0
92+
Public Const WM_WTSSESSION_CHANGE As Integer = &H2B1
93+
94+
Public Enum WTS
95+
CONSOLE_CONNECT = 1
96+
CONSOLE_DISCONNECT = 2
97+
REMOTE_CONNECT = 3
98+
REMOTE_DISCONNECT = 4
99+
SESSION_LOGON = 5
100+
SESSION_LOGOFF = 6
101+
SESSION_LOCK = 7
102+
SESSION_UNLOCK = 8
103+
SESSION_REMOTE_CONTROL = 9
104+
End Enum
105+
106+
'****************************************************************
107+
' Used to track how long the app has been runnning.
108+
'****************************************************************
109+
Dim AppLaunchTime As New Date
110+
111+
Public Sub initAppLaunchTime()
112+
AppLaunchTime = DateTime.UtcNow
113+
End Sub
114+
90115
End Module

0 commit comments

Comments
 (0)