在 vb6 中监视进程以查看它们是否崩溃

发布于 2024-07-09 13:26:54 字数 90 浏览 7 评论 0原文

我有一个程序在我睡觉时经常崩溃,但我需要保持它运行。 所以我想我可以写一个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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如日中天 2024-07-16 13:26:55

我使用了计划任务(每 10 分钟运行一次),用下一个内容启动 cmd 文件:

tasklist |find "myapp.exe" >nul || c:\mypath\myapp.exe

您可以从 VB6 Shell 执行此类命令文件或仅使用任务计划程序:)

I've used scheduled task (running at each 10 min), starting cmd file with next content:

tasklist |find "myapp.exe" >nul || c:\mypath\myapp.exe

You can execute such command file from VB6 Shell or just use Task Scheduler :)

她说她爱他 2024-07-16 13:26:54

您可以使用 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

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

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

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 :)

泛泛之交 2024-07-16 13:26:54

使用WMI

如果您无法使用 VB6,请在网上搜索 WMI+VB6。
否则,连接 C# 和 WMI 会容易得多。

Use WMI.

If you're stuck with VB6, search the web for WMI+VB6.
Otherwise, interfacing c# and WMI is much easier.

余生共白头 2024-07-16 13:26:54

我使用一个运行其他程序的程序。 这样您就可以轮询进程句柄以查看应用程序是否仍在运行。 如果没有,您可以再次启动它。 它确实需要 API 编程。

I use a program that runs other programs. That way you can poll the process handle to see if the application is still running. If not you can launch it again. It does require API programming.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文