最近看到了一個日文版的監控電腦活動記錄的軟件,又在win 32 APi中看到了GetForegroundWindow函數,于是決定動動小手用vbs寫個監控電腦活動記錄的小程序。
主要函數
函數名 | 參數 | 返回值 |
---|---|---|
GetForegroundWindow(void) | 無 | 當前窗口的句柄 |
GetWindowText(HWND hWnd,LPTSTR lpString,Int nMaxCount) |
hWnd:窗口句柄 lpString:接收窗口標題文本的緩沖區的指針 nMaxCount:指定緩沖區中的最大字符數 |
如果成功則返回標題字符串的字符個數。如果窗口無標題欄或文本,或標題欄為空,或窗口或控制的句柄無效,則返回值為零。 |
實現
循環獲取當前焦點所在窗口的標題,然后寫入到日志文件中。最后設置開啟自啟動,隱藏命令行窗口。
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 ' 保存標題文本 Dim stext As String ' 保存上一個窗口句柄 Dim hwnd As Long ' 保存當前窗口句柄 Dim curHwnd As Long ' 書寫流寫入日志文件 Dim sw As StreamWriter ' 日志文件保存路徑 Dim path As String = "c:\log.txt" ' 如果存在日志文件則跳過,否則創建一個日志文件 If Not File.Exists(path) Then File.Create(path) End If sleep(3000) ' 這里是個死循環 While bt stext = Space(255) ' 獲取當前窗口句柄 hwnd = GetForegroundWindow ' 如果當前是新窗口則寫入新窗口標題 If hwnd <> curHwnd Then curHwnd = hwnd ' 獲取窗口標題 GetWindowText(hwnd,stext,255) sw = System.IO.File.AppendText(path) ' 寫入新窗口標題,格式 yyyy年mm月dd日 hh:hh:ss + 標題 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 |
開啟自啟動
新建一個listener.vbs文件(其中C:\listener.exe是vb編譯后的文件路徑,Run參數0表示隱藏命令行窗口):
1
2
3
|
Dim ws set ws = WScript.createObject( "WScript.shell" ) ws.Run "C:\listener.exe" , 0, TRUE |
1. 運行 -> shell:startup
2. 開始菜單 -> 程序 -> 啟動
3. 運行 -> gpedit.msc
4. 啟動 -> 開機中添加listener.vbs腳本
運行
重啟電腦后我們可以再任務管理器中看到運行的腳本
然后查看日志文件C:\log.txt
需要關閉結束進程即可
以上就是如何使用vbs 監控電腦活動記錄的詳細內容,更多關于vbs 監控電腦活動記錄的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/chenjy1225/p/13255953.html