A different approach
Not useful in all situations, however I had one particular scenario where an application was controlling a child process completely. For communication & API interception, a DLL injection was required so a DLL is running in the actual child processes to report back to the parent.
Note: Now, this is not for the creativity award. Background here is: An application that needed to be wrapped, however that was a legacy application and could neither be modified, nor rewritten in a satisfying manner. We needed to keep this system running, while still intercepting outputs from it.
Also, the child process was poorly written, launching itself again immediately and then terminating, so the parent process is actually not our main application.
If you control the child processes and have an injected DLL anyway, you can extend this DLL by monitoring the parent process to check if it is running. If not, ExitProcess
.
If you don't need a DLL in the child process, the other solutions are most likely far better.
You can transfer the parentProcessID
through, for instance, a registry key that you use by your application already. Or you can pass a commandline, if this doesn't break the child process.
This has to run in its own thread. My DLL had two threads: This code here and the controlling & communication code.
DLL
bool IsProcessRunning(HANDLE hProcess)
{
DWORD exitCode;
GetExitCodeProcess(hProcess, &exitCode);
return exitCode == STILL_ACTIVE;
}
bool WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
int parentProcessID = [...]
HANDLE parentProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, TRUE, parentProcessID);
while (IsProcessRunning(parentHandle)) Sleep(100);
ExitProcess(0);
}
return true;
}