Monitor / Process Manager in VB6

I need to write a small application in VB6 to run instances of another VB6 application and keep track of running processes, but I don't know how to get process information in VB6. I can see some of what I need with the utility tasklist, but I really do not know how to create processes (indicating the name of the process or application, if possible) and get information about processes from the operating system.

This application runs on a computer running Windows XP.

Does anyone know of a startup tutorial or a useful web page for this kind of thing?

+5
source share
4 answers
+6

, ** VB6 **, ActiveX exe. . COM .

+3

spelunking , , . VB6 Shell() , OpenProcess . CreateProcess .

, VB6 . 3- ( ). , , , . . TerminateProcess().

, . exit 1234 somesuch, .

, VB6 . TextBox Text1 Timer1 ( ). :

Option Explicit

Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_QUERY_INFORMATION = &H400&
Private Const PROCESS_TERMINATE = &H1&
Private Const WAIT_OBJECT_0 = 0
Private Const INVALID_HANDLE = -1
Private Const DEAD_HANDLE = -2

Private Declare Function CloseHandle Lib "kernel32" ( _
    ByVal hObject As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" ( _
    ByVal hProcess As Long, _
    ByRef lpExitCode As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" ( _
    ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long

Private Tasks() As String
Private Handles() As Long

Private Sub Form_Load()
    Dim I As Integer

    'We'll run 3 copies of the command shell as an example.
    ReDim Tasks(2)
    ReDim Handles(2)
    For I = 0 To 2
        Tasks(I) = Environ$("COMSPEC") & " /k ""@ECHO I am #" & CStr(I) & """"
        Handles(I) = INVALID_HANDLE
    Next
    Timer1.Interval = 100
    Timer1.Enabled = True
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    Dim I As Integer

    Timer1.Enabled = False
    DoEvents
    For I = 0 To UBound(Tasks)
        If Handles(I) <> INVALID_HANDLE And Handles(I) <> DEAD_HANDLE Then
            TerminateProcess Handles(I), 666
            CloseHandle Handles(I)
            Handles(I) = DEAD_HANDLE
        End If
    Next
End Sub

Private Sub Timer1_Timer()
    Dim I As Integer
    Dim ExitCode As Long
    Dim Pid As Long

    Timer1.Enabled = False
    For I = 0 To UBound(Tasks)
        If Handles(I) <> INVALID_HANDLE Then
            If WaitForSingleObject(Handles(I), 0) = WAIT_OBJECT_0 Then
                If GetExitCodeProcess(Handles(I), ExitCode) <> 0 Then
                    Text1.SelText = "Task " & CStr(I) & " terminated, " _
                                  & "exit code: " & CStr(ExitCode) _
                                  & ", restarting task." _
                                  & vbNewLine
                Else
                    Text1.SelText = "Task " & CStr(I) & " terminated, " _
                                  & "failed to retrieve exit code, error " _
                                  & CStr(Err.LastDllError) _
                                  & ", restarting task." _
                                  & vbNewLine
                End If
                CloseHandle Handles(I)
                Handles(I) = INVALID_HANDLE
            End If
        End If
        If Handles(I) = INVALID_HANDLE Then
            Pid = Shell(Tasks(I), vbNormalFocus)
            If Pid <> 0 Then
                Handles(I) = OpenProcess(SYNCHRONIZE _
                                      Or PROCESS_QUERY_INFORMATION _
                                      Or PROCESS_TERMINATE, 0, Pid)
                If Handles(I) <> 0 Then
                    Text1.SelText = "Task " & CStr(I) & " started." _
                                  & vbNewLine
                Else
                    Text1.SelText = "Task " & CStr(I) _
                                  & ", failed to open child process." _
                                  & vbNewLine
                    Handles(I) = DEAD_HANDLE
                End If
            Else
                Text1.SelText = "Task " & CStr(I) _
                              & ", failed to Shell child process." _
                              & vbNewLine
                Handles(I) = DEAD_HANDLE
            End If
        End If
    Next
    Timer1.Enabled = True
End Sub

Hope this helps answer the question.

+2
source

something simpler will use sockets.

Launch the application server and your client will communicate with your server. In doing so, you will provide a connection.

Well, I say. because i'm not what you are trying to do

Sorry that this only applies if your customers are in house i. You have the ability to add changes.

0
source

All Articles