I have a problem that a process created inside of another one is left, when the parent one is killed with something like 'kill task'. More precisely, application launches general_service.exe. Inside of this service, I launch a new logger_runner.exe. When main application stops, it just kill general_service.exe, but in task manager I see my logger_runner.exe. When I stop general_service.exe in VS, it is left as well. How to make my logger_runner depends on general_service ?
Thanks for the help!
This function starts my .exe
void startLogger(PROCESS_INFORMATION & processInformation) {
std::wstring commandLine = L"\"" + PathUtils::programFolder() + LR"(\logger_runner.exe)" + L"\"";
STARTUPINFOW startupInfo;
memset(&startupInfo, 0, sizeof(startupInfo));
memset(&processInformation, 0, sizeof(processInformation));
wchar_t* commandLineCopy = _wcsdup(commandLine.c_str());
if (!CreateProcessW(NULL, // No module name (use command line)
commandLineCopy, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startupInfo, // Pointer to STARTUPINFO structure
&processInformation) // Pointer to PROCESS_INFORMATION structure
)
{
free(commandLineCopy);
throw SystemException();
}
free(commandLineCopy);
}
This function should ends logger_runner.exe
void closeLogger(PROCESS_INFORMATION & processInformation) {
// Check exit code
DWORD exitCode = 0;
GetExitCodeProcess(processInformation.hProcess, &exitCode);
if (TerminateProcess(processInformation.hProcess, 0) == 0) {
std::cerr << "Could not terminate logger process\n";
}
// Close process and thread handles.
CloseHandle(processInformation.hProcess);
CloseHandle(processInformation.hThread);
}
All these functions are called inside of the wrapper
class LoggerWrapper {
public:
LoggerWrapper() {
startLogger(m_processInformation);
};
~LoggerWrapper() {
closeLogger(m_processInformation);
}
private:
PROCESS_INFORMATION m_processInformation;
};
In wmain function of my general_service solution, I just call
LoggerWrapper logger