-1

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?

jsonV
  • 1,650
  • 2
  • 9
  • 23
  • Build a control process (e.g. PowerShell?) instead of a flimsy batch file, or package these up as a Windows Service you can start/stop/suspend. – tadman Apr 19 '19 at 19:55
  • 2
    Have side.exe start main.exe with [`CreateProcess` and friends](https://learn.microsoft.com/en-us/windows/desktop/procthread/process-handles-and-identifiers) and monitor the created process to see if main.exe closes. When it closes, have side.exe the politest way you can. – user4581301 Apr 19 '19 at 19:57
  • @user4581301 This sounds like the beginnings of a good solution; however, with this approach I'll need to check if main.exe is already running. I'll modify side.exe source and see what I can do. Thanks for the link! – jsonV Apr 19 '19 at 20:10
  • You could do this https://stackoverflow.com/a/51319981/2836621 – Mark Setchell Apr 19 '19 at 21:16
  • https://learn.microsoft.com/en-us/windows/desktop/procthread/job-objects `hJob = CreateJobObjectW(0, "EditorJobObject")` all processes started from here are part of the job. Wait for main to exit then to kill all processes `TerminateJobObject(hJob, 0)`. – Noodles Apr 19 '19 at 23:26

1 Answers1

0

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.

jsonV
  • 1,650
  • 2
  • 9
  • 23