c++ - 处理创建进程的 AppCrash

标签 c++ windows exception process

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

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

我正在尽最大努力确保所有输入均有效,但我也想处理未预料到的情况,而不是 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;
}

最佳答案

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

参见 MSDN: WaitForDebugEvent , MSDN: Writing the Debugger's Main Loop , An example of setting that up .

关于c++ - 处理创建进程的 AppCrash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085135/

相关文章:

java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException)

c# - 找出具体异常

c++ - 代码重构后前向声明导致错误

c++ - 在 Windows 上调试时,stderr 去哪里了?

c++ - 异常规范作为函数声明中的注释

mysql - 特殊字符在 MySQL (UTF-8) 中不起作用

Windows 批处理编程——此时出乎意料

C++:使用 x{0} 进行变量初始化

c++ - 在二叉搜索树中插入节点时使用指针时出错

c# - 如何检查小文本文件中的特定字符串