Create child process in VB6 that get automatically terminated when the program is terminated

StackOverflow https://stackoverflow.com/questions/15901836

  •  02-04-2022
  •  | 
  •  

質問

I wants to create a program that will run two child processes. Now, I want when my app get terminated by Task Manager or get crash, its two child process should get automatically terminated. How can I do this?

役に立ちましたか?

解決

In Windows you can use Job Objects -- the closest thing to process groups in Linux. Just research the API, it's compatible w/ XP (probably SP3) and above.

You have to assign your VB6 process to a job, then every other process you spawn is implicitly part of this job.

Take a look at Performing equivalent of “Kill Process Tree” in c++ on windows.

他のヒント

let the client check if the master is still running using the FindWindow API

for example: start calculator, run the following sample project, and close the calculator .. upon closing the calculator this sample project will close as well

'1 form with:
'  1 timer: name=Timer1
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
  With Timer1
    .Interval = 100
    .Enabled = True
  End With 'Timer1
End Sub

Private Sub Timer1_Timer()
  Dim hwnd As Long
  hwnd = FindWindow(vbNullString, "Rekenmachine")
  If hwnd = 0 Then Unload Me
End Sub

(Rekenmachine is the dutch name of the calculator in windows, use your own program name instead)

Correct me if I am wrong : you have an application which starts 2 other processes (of which you don't have access to the source code), and you want those 2 processes to be killed when your application ends ?

This can be done when your application starts an extra process which monitors the state of your application and takes care of closing the other 2 processes

for example a rough monitoring application could exist of 1 module :

Option Explicit

Public pstrArg() As String

Private Sub Main()
  pstrArg = Split(Command, " ")
  Load frmMonitor
End Sub

This module loads a form called frmMonitor with a timer control on it :

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
  With Timer1
    .Interval = 100
    .Enabled = True
  End With 'Timer1
End Sub

Private Sub Timer1_Timer()
  Dim intIndex As Integer
  If FindWindow(vbNullString, pstrArg(0)) = 0 Then
    For intIndex = 1 To UBound(pstrArg)
      TerminateProcess pstrArg(intIndex)
    Next intIndex
    End
  End If
End Sub

Private Sub TerminateProcess(strName As String)
  Dim Process As Object
  For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & strName & "'")
    Process.Terminate
  Next
End Sub

Make sure the startup function of the monitoring application is sub Main so you can call it with command line arguments

You can test this as follows : compile the monitoring application into MonitorApp.exe and place it in the same folder as a test project existing of 1 form named frmTest :

'1 form with:
'  1 timer control: name=Timer1
'  1 command button : name=Command1

Option Explicit

Private Sub Command1_Click()
  Shell "calc.exe"
End Sub

Private Sub Form_Load()
  Shell App.Path & "\MonitorApp.exe frmTest calc.exe"
End Sub

This will start a calculator each time you click on the command button When you close the form, the monitor application will close all calculators, and then finish itself

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top