import sys p = r"C:/Users/locpham/SonicForgeStudio/native_bridge/src/main.cpp" data = open(p, "rb").read() def rep(old, new, count=1): global data old = old.encode(); new = new.encode() assert data.count(old) >= count, "pattern not found: %r" % old data = data.replace(old, new, count) # 1. vectored handler ghi dump cho first-chance; cai gan CrashDumpHandler vectored = r""" // Vectored handler: catches first-chance exceptions INCLUDING heap-corruption // raises that never reach SetUnhandledExceptionFilter (0xC0000374 observed to // bypass it on this machine). Writes the same minidump then continues. #ifdef _WIN32 static LONG WINAPI VectoredCrashHandler(PEXCEPTION_POINTERS ep) { PEXCEPTION_RECORD er = ep ? ep->ExceptionRecord : nullptr; DWORD code = er ? er->ExceptionCode : 0; if (code != 0xC0000374 && code != 0xC0000005 && code != 0xC0000409) { return EXCEPTION_CONTINUE_SEARCH; // ignore benign first-chance noise } std::cerr << "[VCRASH] first-chance code=0x" << std::hex << code << " addr=0x" << (er ? (void*)er->ExceptionAddress : nullptr) << std::dec << " tid=" << GetCurrentThreadId() << std::endl; // route into the same dump writer return CrashDumpHandler(ep) == EXCEPTION_CONTINUE_SEARCH ? EXCEPTION_CONTINUE_SEARCH : EXCEPTION_CONTINUE_SEARCH; } #endif """ rep("int main(int argc, char* argv[]) {\n SetUnhandledExceptionFilter(CrashDumpHandler);\n", vectored + "\nint main(int argc, char* argv[]) {\n SetUnhandledExceptionFilter(CrashDumpHandler);\n AddVectoredExceptionHandler(1, VectoredCrashHandler);\n") open(p, "wb").write(data) print("patched OK, size=", len(data))