前两天看了Delphi版面精华区中的“进程死亡的自动复活“一文,觉得作者的思路很不错,利用api来监视进程的活动,当被销毁时就自动再创建进程。仔细推敲之后,发觉其实用
vb也是可以做到的。于是花了半天的时间写了以下的
程序,实现了使用WaitForSingleObject API来监视被创建的进程的活动,一旦返回除time out 之外的消息就自动创建新的进程。以下为其实现代码。在win2000 server + vb 6.0下通过。
Option Explicit
Private RunFile$
Private Const NORMAL_PRIORITY_CLASS = &H20 ''如果进程位于前台,则基本值是9;如果在后台,则优先值为7
Private Const INFINITE = &HFFFFFFFF
Private Const WAIT_TIMEOUT = &H102& ''对象保持未发出信号的状态,但等待超时时间已经超过
Private Flag As Boolean ‘进程活动监视标志
''说明∶PROCESS_INFORMATION结构由CreateProcess函数将关于新建立的进程和
''主要线索的信息写入其中成员变量
Private Type PROCESS_INFORMATION ''
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
''说明∶STARTUPINFO结构用在CreateProcess函数中指定为新进程建立的新窗口的主要属性。这一
''一信息影响由CreateWindows函数建立的第一个窗口
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAt