0

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
Bill Lumbert
  • 4,633
  • 3
  • 20
  • 30
  • child process independent from parent. it not died when killed parent. – RbMm Apr 18 '18 at 11:52
  • you of course can exec process inside job object and kill all processes in job, but not think this is good solution - kill processes. every process must correct exit – RbMm Apr 18 '18 at 11:54
  • 2
    possibly related: https://stackoverflow.com/questions/53208/how-do-i-automatically-destroy-child-processes-in-windows?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Richard Hodges Apr 18 '18 at 12:07
  • Agree. @BillLumbert: Please indicate if there's any difference between that question and this one. – MSalters Apr 18 '18 at 12:12
  • really need call `TerminateJobObject ` instead set `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE` limit, if we want terminate all process tree. anyway this not good idea. or what sense call `GetExitCodeProcess` before `TerminateProcess` ? – RbMm Apr 18 '18 at 12:23
  • This sounds like you are trying to implement a service rather than a regular application. In that case, the service control manager already implements dependency management for you. – IInspectable Apr 18 '18 at 12:29
  • @IInspectable - nobody (including scm) will be kill child process. dependency management here unrelated – RbMm Apr 18 '18 at 12:50

0 Answers0