-1

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?

Ilya Kurnosov
  • 3,049
  • 3
  • 20
  • 35
  • How do you create and launch child processes? Using Shell function, CreateProcess API, DCOM or whatever other methods? – Arvo Apr 09 '13 at 13:39
  • @Arvo Actually my programming is starting a service which needs to be get stopped when my program is terminated. In the case, it is only possible with Shell function or CreateProcess, I am ready to use these. – user2245523 Apr 09 '13 at 15:27
  • By "service" do you mean Windows service? You framed the question as if it is about child processes spawned by your application. – Ilya Kurnosov Apr 09 '13 at 16:33

3 Answers3

1

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.

Community
  • 1
  • 1
wqw
  • 10,921
  • 1
  • 30
  • 39
0

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)

Hrqls
  • 2,829
  • 4
  • 31
  • 53
  • May be, I haven't wrote it clearly. But, in my question, conditions are opposite. In your example, I want the calculator to automatic get closed when VB6 program is terminated. – user2245523 Apr 09 '13 at 15:24
  • I think he is suggesting using this logic in your child processes, assuming they are also vb code you are in charge of. Then when your main window closes, the children will recognize this and close themselves. – Cemafor Apr 09 '13 at 20:03
  • Use this in the window you want to be closed, and replace "Rekenmachine" with the caption of the window you want to follow the state of. I assumed you have access to the source code of the process you want to be closed automatically. – Hrqls Apr 10 '13 at 05:15
  • 1
    If you only have access to the source code of the main process which starts 2 other process of which you dont have the source code, then you can't do it without creating another monitoring process. As your master crashes it cant do anything anymore, so you need another process which can use the above code to monitor the state of the master. it can then terminate the other processes using one of the answer in this thread : http://stackoverflow.com/q/1378604/1584106 .. and after that has been done, close itself – Hrqls Apr 10 '13 at 05:20
0

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

Hrqls
  • 2,829
  • 4
  • 31
  • 53
  • Isn't there any alternate which don't need creating a new application? – user2245523 Apr 10 '13 at 11:09
  • i dont think so: as you want to monitor crashes of your own application as well ... if your application crashes, then you cant do anything ... or you must know where it is likely to crash, in that case you might do something about it :) – Hrqls Apr 10 '13 at 12:34