gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:56:27 25 4
gpt4 key购买 nike

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com