监视进程以查看它们是否在 vb6 中崩溃 [英] Monitoring processes to see if they've crashed in vb6

查看:19
本文介绍了监视进程以查看它们是否在 vb6 中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序在我睡着时经常崩溃,我需要让它继续运行.所以我想我可能会写一个监控进程列表的 vb6 应用程序,如果有东西消失,它会重新启动它.有人知道一个简单的方法吗?

I've got a program that tends to crash quite often while I'm asleep and I need to keep it running. So I thought I might writeup a vb6 application that monitors the process list, if something disappears it will relaunch it. Anyone know of an easy way?

推荐答案

您可以使用 EnumProcesses 列出您运行时系统中的每个进程,您可以使用此声明来使用它

You could use EnumProcesses to list every process in the system at the moment you're running you could use this declaration to use it

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 的声明是这样的

Prior using it you should define an array of Long to pass as an argument to EnumProcesses with enough space to read all processes ids. You could call EnumProcesses twice to discover how large that array should be. After the second call you could start looping through that array and opening the processes obtaining that way a handle which used appropriately can tell you the name of the process executable and comparing that data with the name of the executable you're searching you are done. Otherwise if what you're looking for is a DLL for example you could EnumProcessModules for that process handle searching for each running process for the dll you're looking for. the declaration of EnumProcessModules is this

    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

你可能需要的代码是这样的

and the probable code you'd need would be something like this

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

函数代码有点长但调用它是一个小函数,如果你想测试一下可以使用它:)

function code is a little long but calling it is a little function, you may use it if you want to test it a little :)

这篇关于监视进程以查看它们是否在 vb6 中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆