C ++ - 当初始进程被杀死时,从另一个进程启动的进程不会被终止

时间:2018-04-18 11:50:34

标签: c++ windows winapi process createprocess

我遇到了一个问题,即当一个进程被“kill task”之类的东西杀死时,剩下的进程就会被创建。更准确地说,应用程序启动general_service.exe。在此服务内部,我启动了一个新的logger_runner.exe。当主应用程序停止时,它只会杀死general_service.exe,但在任务管理器中我看到了我的logger_runner.exe。当我在VS中停止general_service.exe时,它也会被保留。 如何制作我的logger_runner取决于general_service?

感谢您的帮助!

此功能启动我的.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);

}

此函数应结束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);
}

所有这些函数都在包装器中调用

class LoggerWrapper {
public:
    LoggerWrapper() {
        startLogger(m_processInformation);
    };

    ~LoggerWrapper() {
        closeLogger(m_processInformation);
    }
private:
    PROCESS_INFORMATION m_processInformation;
};

在我的general_service解决方案的wmain函数中,我只是调用

LoggerWrapper logger

0 个答案:

没有答案