怎样使用VB编写监视指定进程的程序
来源:优易学  2011-12-3 11:08:48   【优易学:中国教育考试门户网】   资料下载   IT书店

  Public Declare Function GetCurrentProcess Lib "kernel32" () As Long’获取当前进程句柄
  Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long;获取当前进程ID
  Public Const TH32CS_SNAPPROCESS = As LongH2
  Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
  Public Declare Function RegisterServiceProcess Lib "kernel32" (ByVal dwProcessId As Long, ByVal dwType As Long) As Long
  Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
  Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
  Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
  Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
  Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
  Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
  Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
  Public Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
  Public Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
  Public Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
  Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
  Public Const PROCESS_TERMINATE =&H1
  Public Const PROCESS_QUERY_INFORMATION =&H400
  Public Const EWX_FORCE = 4
  Public Const EWX_REBOOT = 2
  Public Const GW_CHILD = 5
  Public Const GW_HWNDFIRST = 0
  Public Const GW_HWNDNEXT = 2
  Public Const GW_MAX = 5
  Public Const GW_OWNER = 4
  Public Const HKEY_LOCAL_MACHINE =&H80000002
  Public Const REG_SZ = 1
  Public Const RSP_SIMPLE_SERVICE = 1
  Public Const RSP_UNREGISTER_SERVICE = 0
  Public Const CREATE_SUSPENDED = &H4
  Public Const MF_BYPOSITION = &H400
  Public Const BM_CLICK = &HF5
  Public pe As PROCESSENTRY32, hSnapshot As Long
  Public StartNum As Long, AppName As String, Section As String, sKey As String, appValue As String, sKeyFile As String, sKeyNum As String
  Public NumTerminate As Long, hThread As Long, ThreadID As Long, sFileName As String
  Public Function StartMonitor(lParam As Long) As Long’线程函数
  WaitForTheProcess GetProcessHandle(sFileName), sFileName’开始监控
  StartMonitor = 1
  End Function
  Public Function SendEnter As Long()’搜寻系统登陆对话框,找到就发送回车键
  Dim Currwnd As Long, Length As Long, ListItem As String
  Currwnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)’这里用窗口标题查找的原因是系统重启时基本上不会加载多少进程,这样窗口的标题通常是不会被改变的。
  While Currwnd <> 0
  Length = GetWindowTextLength(Currwnd)’获取窗口标题字符串的长度。
  If Length <> 0 Then
  ListItem As String = Space As String(Length)
  Length = GetWindowText(Currwnd, ListItem As String, Length + 2)’获取窗口标题
  If InStr(ListItem, "输入网络密码") <> 0 Then
  EnumChildWindows Currwnd, AddressOf GetOkButton, 0
  SendEnter = 1
  Exit Function
  End If
  End If
  Currwnd = GetWindow(Currwnd, GW_HWNDNEXT)
  Wend
  SendEnter = 0
  End Function
  Public Sub WaitForTheProcess(ByVal hProcess As Long, ByVal sPath As String)’开始监控进程
  Dim Pro_Info As PROCESS_INFORMATION, StartInfo As STARTUPINFO
  StartInfo.cb = Len(StartInfo)
  If hProcess > 0 Then’如果已经运行了被监控进程则开始监控
  Dim WaitResult As Long
  WaitResult = WaitForSingleObject(hProcess, (-1))
  CloseHandle hProcess
  If StartNum >= NumTerminate Then’如果重启次数超过设置的次数就重新启动系统
  SaveSetting AppName, Section, sKey, "1"
  ExitWindowsEx EWX_REBOOT Or EWX_FORCE, 0’强制退出,这样可以顺利退出
  Exit Sub
  End If
  StartNum = StartNum + 1
  Form1.Label6 = StartNum
  End If
  CreateProcess vbNullString, sPath, 0, 0, True, 32, ByVal 0 As Long, vbNullString, StartInfo, Pro_Info’ 否则用被监控进程的全路径文件名来创建被监控进程
  WaitForTheProcess Pro_Info.hProcess, sPath
  End Sub
  Public Function GetProcessHandle As Long(ByVal sPath As String)’获取被监控进程的进程句柄
  sPath = LCase(sPath)
  hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)’创建一个snapshot对象
  pe.dwSize = Len(pe)
  bValue = Process32First(hSnapshot, pe)’开始遍历系统进程池
  While bValue <> 0
  If InStr(LCase(pe.szExeFile), sPath) <> 0 Then’如果找到了,则…
  Dim hProcess As Long
  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pe.th32ProcessID)
  GetProcessHandle = hProcess
  CloseHandle hSnapshot
  Exit Function
  End If
  bValue = Process32Next(hSnapshot, pe)
  Wend
  CloseHandle hSnapshot
  GetProcessHandle = 0’否则返回0
  End Function

上一页  [1] [2] [3] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训