您的位置:好站长外链 > 站长新闻 > 软件 >

如何使用vb 监控电脑活动记录

文章来源:好站长外链

作者:互联网

人气:19

2021-06-27

最近看到了一个日文版的监控电脑活动记录的软件,又在win 32 APi中看到了GetForegroundWindow函数,于是决定动动小手用vb写个监控电脑活动记录的小程序。

主要函数

函数名 参数 返回值
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

需要关闭结束进程即可

相关文章

在线客服

外链咨询

扫码加我微信

微信:841909564

返回顶部