Saturday 27 November 2021

CreateJobObject.exe and CreateJobObjectTimeout starts a program (such as a batch file). That program and any programs started by that program will be terminated as a group.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
REM CreateJobObject.bat
REM This file compiles CreateJobObject.vb to CreateJobObject.exe
REM CreateJobObject.exe starts a program (such as a batch file). That program and any programs started by that program will be terminated as a groupREM CreateJobObject.exe starts a program (such as a batch file). That program and any programs started by that program will be terminated as a groupwhen you click Ok. 
REM CreateJobObjectTimeout.exe starts a program (such as a batch file). That program and any programs started by that program will be terminated as a group after the specified number of seconds.
REM To use 
REM     CreateJobObject Program.exe
REM To use 
REM     CreateJobObjectTimeout  Program.exe
REM EG
REM CreateJobObject cmd /k "start notepad & start mspaint"
REM CreateJobObjectTimeout 4 notepad
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\CreateJobObject.exe" "%~dp0\CreateJobObject.vb" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\CreateJobObjectTimeOut.exe" "%~dp0\CreateJobObjectTimeOut.vb" 
pause

'CreateJobObject.vb
Imports System.Runtime.InteropServices
Imports System.Diagnostics.Process

Public Module Editor
	Private Declare UNICODE Function CreateJobObjectW Lib "Kernel32" (lpJobAttributes as IntPtr, ByVal lpName As String) As IntPtr
	Private Declare Function AssignProcessToJobObject Lib "Kernel32" (ByVal hJob As IntPtr, hProcess As IntPtr) As Boolean
	Private Declare Function TerminateJobObject Lib "Kernel32" (ByVal HJob as IntPtr, ExitCode As Integer) As Boolean

	Public Sub Main()
		Dim hJob as IntPtr
		Dim hProcess as Integer
		Dim wshshell as Object
		wshshell = CreateObject("Wscript.Shell")
		hJob = CreateJobObjectW(0, "MyJobObject")
		hProcess = -1
		AssignProcessToJobObject(hJob, hProcess)
		wshshell.run(Command(),, vbfalse)
		If Msgbox("Press Ok to terminate this program and any child programs") = 1 then TerminateJobObject(hJob, 0)
		msgbox(err.lastdllerror)
	End Sub
End Module

'CreateJobObjectTimeout.vb
Imports System.Runtime.InteropServices
Imports System.Diagnostics.Process

Public Module Editor
	Private Declare UNICODE Function CreateJobObjectW Lib "Kernel32" (lpJobAttributes as IntPtr, ByVal lpName As String) As IntPtr
	Private Declare Function AssignProcessToJobObject Lib "Kernel32" (ByVal hJob As IntPtr, hProcess As IntPtr) As Boolean
	Private Declare Function TerminateJobObject Lib "Kernel32" (ByVal HJob as IntPtr, ExitCode As Integer) As Boolean
	Private Declare Sub Sleep Lib "Kernel32" (ByVal TimeOut As Integer) 

	Public Sub Main()
		Dim hJob as IntPtr
		Dim hProcess as Integer
		Dim wshshell as Object
		wshshell = CreateObject("Wscript.Shell")
		hJob = CreateJobObjectW(0, "MyJobObject")
		hProcess = -1
		AssignProcessToJobObject(hJob, hProcess)
		wshshell.run(Split(Command()," ", 3)(1),, vbfalse)
		Sleep(CInt(Split(Command(), " ", 2)(0)) * 1000)
		TerminateJobObject(hJob, 0)
		msgbox(err.lastdllerror)
	End Sub
End Module