# How can I link two separate .exe files such that ending one process kills both?

I have two executable files that I would like to run together: main.exe & side.exe

The main.exe file is proprietary source code that I cannot modify, but it is utilizing a modification to compliment it, side.exe, that I can modify. When main.exe is killed, side.exe remains running. I would like to create a method for linking the two executable files such that running main.exe also runs side.exe and closing main.exe closes side.exe.

Currently, I've tried using a simple batch script to run the two executable files. This works to start both executable files, but it doesn't have the latter behavior of closing both files on exiting main.exe:

script.bat:

@echo off
c:\path\to\first\exe\main.exe
c:\path\to\second\exe\side.exe


How can I link the two within batch, or side.exe whose source is in cpp?

On

As per user4581301's comment. Here's the C++ code I generated.

Using CreateProcess:

The following code is compiled within Side.exe and starts Main.exe if it's not already running.

if (!FindProcessId(L"Main.exe")) {
LPTSTR szCmdline = _tcsdup(TEXT("\"C:\\Program Files (x86)\\Path\\To\\Main\""));
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si)); //Use default startup info
ZeroMemory(&pi, sizeof(pi));
CreateProcess(NULL,
szCmdline,
NULL,
NULL,
FALSE,
CREATE_BREAKAWAY_FROM_JOB,
NULL,
NULL,
&si,
&pi
);
}


Since my particular project involves injecting DLL's and is a Qt GUI state machine monitoring Main.exe, upon state change I would run this:

if (!dllInjector.GetProcessID(MAIN_PROCESS_NAME))
{
// Code ommitted for brevity
OnExitClick();
// Simulates exit click "QApplication::exit();"
}


which would close Side.exe if Main.exe is not detected.

Thus, the behavior is as follows: Running Side.exe runs Main.exe; closing Main.exe closes Side.exe; however, this doesn't run Side.exe on running Main.exe. Other approaches commented in my question seem viable.