How to use VBS to monitor computer activity records

Time:2021-12-29
catalogue
  • Main function
  • realization
  • Turn on self start
  • function

Recently, I saw a Japanese version of the software for monitoring computer activity records, and saw the GetForegroundWindow function in the win 32 API, so I decided to use my little hand to write a small program for monitoring computer activity records with VBS.

Main function

Function name parameter Return value
GetForegroundWindow(void) nothing Handle to the current window
GetWindowText(HWND hWnd,LPTSTR lpString,Int nMaxCount) HWnd: window handle
LpString: pointer to the buffer that receives the window title text
Nmaxcount: Specifies the maximum number of characters in the buffer
If successful, the number of characters of the title string is returned. If the window has no title bar or text, or the title bar is empty, or the handle to the window or control is invalid, the return value is zero.

realization

Loop to get the title of the window where the current focus is located, and then write it to the log file. Finally, set to enable self startup and hide the command line window.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Imports System
Imports System.io
 
Module Module1
 private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
 'Win32 Api
 Private Declare Function GetForegroundWindow Lib "user32" () As Long
 Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
 
 Sub Main()
     
  Dim bt As Boolean = True
     'save title text
  Dim stext As String
     'save previous window handle
     Dim hwnd As Long
     'save current window handle
     Dim curHwnd As Long
     'write stream write log file
     Dim sw As StreamWriter
     'log file save path
     Dim path As String = "c:\log.txt"
     
     'skip if log file exists, otherwise create a log file
     If Not File.Exists(path) Then
         File.Create(path)
     End If
     
     sleep(3000)
     
     'This is an endless cycle
     While bt
     
      stext = Space(255)
         'get current window handle
         hwnd = GetForegroundWindow
         
         'if it is currently a new window, write the new window title
         If hwnd <> curHwnd Then
          curHwnd = hwnd
                'get window title
                GetWindowText(hwnd,stext,255)
                
                sw = System.IO.File.AppendText(path)
                
                'write new window title in the format yyyy MM DD HH: HH: SS + title
                Using sw
                    sw.WriteLine(String.Format("{0:F}", DateTime.Now) +" "+ stext)
                    sw.Flush()
                End Using
                
        
         End If
   sleep(2000)
         
  End While
     
 End Sub
End Module

Turn on self start

Create a new listener VBS file (where C: \ listener.exe is the file path after VB compilation, and run parameter 0 means to hide the command line window):

?
1
2
3
Dim ws
set ws = WScript.createObject("WScript.shell")
ws.Run "C:\listener.exe", 0, TRUE

1. Run – > shell: startup

How to use VBS to monitor computer activity records

2. Start menu – > program – > start

How to use VBS to monitor computer activity records

3. Run – > gpedit msc

How to use VBS to monitor computer activity records

4. Add listener in startup – > startup Vbs script

How to use VBS to monitor computer activity records

function

After restarting the computer, we can see the running script in the task manager

How to use VBS to monitor computer activity records

Then check the log file C: \ log txt

How to use VBS to monitor computer activity records

You need to close the end process

The above is the details of how to use VBS to monitor computer activity records. For more information about VBS monitoring computer activity records, please pay attention to other relevant articles of developeppaper!