您可以使用以下代码片段强制 Windows 不忽略异常(来自 Microsoft 的Exceptions that are throw from an application that running in a 64-bit version of Windows are ignored),您将在您的进程代码中放入:
// my SDK is v6.0A and the two APIs are not available in the .h files, so I need to get them at runtime
#define PROCESS_CALLBACK_FILTER_ENABLED 0x1
typedef BOOL (WINAPI *GETPROCESSUSERMODEEXCEPTIONPOLICY)(__out LPDWORD lpFlags);
typedef BOOL (WINAPI *SETPROCESSUSERMODEEXCEPTIONPOLICY)(__in DWORD dwFlags );
HINSTANCE h = ::LoadLibrary(L"kernel32.dll");
if ( h ) {
GETPROCESSUSERMODEEXCEPTIONPOLICY GetProcessUserModeExceptionPolicy = reinterpret_cast< GETPROCESSUSERMODEEXCEPTIONPOLICY >( ::GetProcAddress(h, "GetProcessUserModeExceptionPolicy") );
SETPROCESSUSERMODEEXCEPTIONPOLICY SetProcessUserModeExceptionPolicy = reinterpret_cast< SETPROCESSUSERMODEEXCEPTIONPOLICY >( ::GetProcAddress(h, "SetProcessUserModeExceptionPolicy") );
if ( GetProcessUserModeExceptionPolicy == 0 || SetProcessUserModeExceptionPolicy == 0 ) {
return;
}
DWORD dwFlags;
if (GetProcessUserModeExceptionPolicy(&dwFlags)) {
SetProcessUserModeExceptionPolicy(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED);
}
}
可能您还必须添加一个未处理的异常过滤器:过滤器就像一个“顶级异常处理程序”,就像一个最顶层的catch
块。要从 _EXCEPTION_POINTERS 中提取程序员友好的字符串,您可以看到Is there a function to convert EXCEPTION_POINTERS struct to a string?
LONG WINAPI my_filter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo)
{
::OutputDebugStringA("an exception occured!");
return EXCEPTION_EXECUTE_HANDLER;
}
您添加过滤器:
::SetUnhandledExceptionFilter(my_filter);
您必须在进程的每个线程中执行此操作:虽然前面的代码片段是每个进程的,但过滤器是每个线程的。