在机器异常之后重新执行程序

Re-execute a program after machine exception

本文关键字:执行程序 之后 机器 异常      更新时间:2023-10-16

我有一个预编译EXE(本机C 11),该EXE在迭代过程的某个时候崩溃(访问违规错误)。我现在无法负担调试并暂时重新编译。

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

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

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

我在@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_USERSOFTWAREMicrosoftWindowsWindows Error Reporting
"DontShowUI"=dword:00000001

而不是:

"DontShowUI"=dword:00000000