gpt4 book ai didi

c++ - 机器异常后重新执行程序

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

我有一个预编译的 exe( native C++11),它在迭代过程中的某个时刻崩溃(访问冲突错误)。我暂时无法调试它并重新编译它。

我想到了一个肮脏的解决方案。我将制作另一个程序来负责执行该 exe,当它停止工作时,我只需再次重新执行它。

这可能吗?我怎么知道程序已停止?

注意:我在 Windows 上使用 MSVS 进行开发。

最佳答案

我在@Richard Hodges 的帮助下找到了解决方案。

用这段代码制作一个新程序:

#include <Windows.h>
#include <string>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int main(int argc, const char**argv) {
while (true) {
TCHAR ProcessName[256];
STARTUPINFO si;
PROCESS_INFORMATION pi;
wcscpy(ProcessName, L"FaultyProgram.exe");
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
ProcessName, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NEW_CONSOLE, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 0;
}

// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);

// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
return 0;
}

最重要的部分是通过更改注册表中的此值来禁用程序崩溃时的 UI 错误消息:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Windows Error Reporting
"DontShowUI"=dword:00000001

代替:

"DontShowUI"=dword:00000000

关于c++ - 机器异常后重新执行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41079083/

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