【问题标题】:Handle AppCrash of created process处理已创建进程的 AppCrash
【发布时间】:2016-03-09 04:43:16
【问题描述】:

在我的项目中,我必须运行一个外部应用程序来为我做一些工作。

不幸的是,这个应用程序不是很稳定,当给定的输入不是这个应用程序所期望的时它会崩溃。

我正在尽最大努力确保所有输入都有效,但我也想处理未预知的情况,而不是 Windows AppCrash 消息显示我自己的消息。

所以我的问题是:有没有办法处理流程 AppCrash 事件?

这是我用来启动应用程序的代码:

bool runExternalCalibrationConsole(const std::wstring &wsCalibFolderPath)
{
    wchar_t AppName[512];
    wchar_t CmdLine[2 * MAX_PATH];

    std::wstring app = L"D:\\ExternalCalibrationConsole.exe";
    std::wstring cmd = L"D:\\ExternalCalibrationConsole.exe";
    cmd += L" ";
    cmd += wsCalibFolderPath;
    swprintf_s(AppName, 512, app.c_str());
    swprintf_s(CmdLine, 512, cmd.c_str());
    PROCESS_INFORMATION processInformation;
    STARTUPINFO startupInfo;
    memset(&processInformation, 0, sizeof(processInformation));
    memset(&startupInfo, 0, sizeof(startupInfo));
    startupInfo.cb = sizeof(startupInfo);

    BOOL result;
    char tempCmdLine[MAX_PATH * 2];  //Needed since CreateProcessW may change the contents of CmdLine
    if (CmdLine != NULL)
    {
        //_tcscpy_s(tempCmdLine, MAX_PATH * 2, CmdLine);
        result = ::CreateProcess(AppName, CmdLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInformation);
        if (result){
            printf("Process created\n");
        }
        else
        {
            printf("Error creating process\n");
            return -10;
        }
        WaitForSingleObject(processInformation.hProcess, INFINITE);
        DWORD exitCode;
        result = GetExitCodeProcess(processInformation.hProcess, &exitCode);

        CloseHandle(processInformation.hProcess);

        return exitCode;
    }

    return 0;
}

【问题讨论】:

    标签: c++ windows exception process


    【解决方案1】:

    一种选择是将子进程创建为可调试的(例如,将DEBUG_ONLY_THIS_PROCESS 传递给CreateProcess()),然后在WaitForDebugEvent()ContinueDebugEvent() 的帮助下处理来自它的调试事件。

    MSDN: WaitForDebugEventMSDN: Writing the Debugger's Main LoopAn example of setting that up

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2012-11-02
      • 1970-01-01
      相关资源
      最近更新 更多