我有一个程序在我睡着的时候经常会崩溃,我需要让它保持运行。所以我想我可能会编写一个监视进程列表的vb6应用程序,如果某些内容消失,它将重新启动它。有人知道一个简单的方法吗?
答案 0 :(得分:3)
您可以使用EnumProcesses列出您运行时系统中的每个进程,您可以使用此声明来使用它
Public Declare Function EnumProcesses Lib "psapi.dll" ( _
ByRef idProcess As Long, ByVal cb As Long, _
ByRef cbNeeded As Long) As Long
在使用它之前,你应该定义一个Long数组作为参数传递给EnumProcesses,并有足够的空间来读取所有进程ID。您可以调用EnumProcesses两次以发现该数组应该有多大。 在第二次调用之后,您可以开始循环遍历该数组并打开进程,从而获得正确使用的句柄可以告诉您进程可执行文件的名称,并将该数据与您正在搜索的可执行文件的名称进行比较。否则,如果您要查找的是DLL,例如,您可以使用EnumProcessModules为该进程处理搜索您正在寻找的dll的每个正在运行的进程。 EnumProcessModules的声明就是这个
Public Declare Function EnumProcessModules Lib "psapi.dll" ( _
ByVal hProcess As Long, ByRef lphModule As Long, _
ByVal cb As Long, ByRef cbNeeded As Long) As Long
你需要的可能代码就是这样的
Option Explicit
Private Declare Function OpenProcess Lib "Kernel32.dll" ( _
ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" ( _
ByRef lpidProcess As Long, ByVal cb As Long, _
ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" ( _
ByVal hProcess As Long, ByVal hmodule As Long, _
ByVal moduleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" ( _
ByVal hProcess As Long, ByRef lphModule As Long, _
ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
Public Function IsModuleRunning(ByVal theModuleName As String) As Boolean
Dim aProcessess(1 To 1024) As Long ' up to 1024 processess?'
Dim bytesNeeded As Long
Dim i As Long
Dim nProcesses As Long
Dim hProcess As Long
Dim found As Boolean
EnumProcesses aProcessess(1), UBound(aProcessess), bytesNeeded
nProcesses = bytesNeeded / 4
For i = 1 To nProcesses
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, aProcessess(i))
If (hProcess) Then
Dim hmodule(1 To 1024) As Long ' no more than 1024 modules per process?'
bytesNeeded = 0
If EnumProcessModules(hProcess, hmodule(1), 1024 * 4, bytesNeeded) Then
Dim nModules As Long
Dim j As Long
Dim moduleName As String
moduleName = Space(1024) ' module name should have less than 1024 bytes'
nModules = bytesNeeded / 4
For j = 1 To nModules
Dim fileNameLen As Long
fileNameLen = GetModuleFileNameExA(hProcess, hmodule(j), moduleName, 1024)
moduleName = Left(moduleName, fileNameLen)
If Right(LCase(moduleName), Len(theModuleName)) = LCase(theModuleName) Then
found = True
Exit For
End If
Next
End If
End If
CloseHandle hProcess
If found Then Exit For
Next
IsModuleRunning = found
End Function
Private Sub Form_Load()
MsgBox IsModuleRunning("explorer.exe")
End Sub
函数代码有点长,但调用它是一个小函数,如果你想稍微测试它可以使用它:)
答案 1 :(得分:2)
使用WMI。
如果您遇到VB6,请在网上搜索WMI + VB6
否则,接口c#和WMI要容易得多。
答案 2 :(得分:0)
我使用了计划任务(每10分钟运行一次),启动带有下一个内容的cmd文件:
任务列表|查找“myapp.exe”> nul || C:\ mypath中\ MyApp.exe的
您可以从VB6 Shell执行此类命令文件,或只使用任务计划程序:)
答案 3 :(得分:0)
我使用运行其他程序的程序。这样,您可以轮询进程句柄以查看应用程序是否仍在运行。如果没有,你可以再次启动它。它确实需要API编程。