在下面的代码中,我已经使用了所有记录的方法来检测异常并生成诊断。它使用C++的try/catch关键字,用__try/__catch扩展关键字捕获SEH异常,使用Windows' AddVectoredExceptionHandler()和SetUnhandledExceptionFilter() winapi函数来安装VEH/SEH过滤器。
在Visual C++ 2003上运行: /GS: 输出"hello,world!"并以退出代码0终止。 /GS-: 输出"hello,world!"并以退出代码0终止。
在Visual C++ 2013上运行: /GS: 没有输出,以退出代码-1073740791终止。 /GS-: 输出"hello,world!"并以退出代码0终止。
如何在启用/GS的VS2013编译程序中生成诊断?
在Visual C++ 2003上运行: /GS: 输出"hello,world!"并以退出代码0终止。 /GS-: 输出"hello,world!"并以退出代码0终止。
在Visual C++ 2013上运行: /GS: 没有输出,以退出代码-1073740791终止。 /GS-: 输出"hello,world!"并以退出代码0终止。
如何在启用/GS的VS2013编译程序中生成诊断?
#include "stdafx.h"
#include <Windows.h>
#define CALL_FIRST 1
#define CALL_LAST 0
LONG WINAPI MyVectoredHandler(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
UNREFERENCED_PARAMETER(ExceptionInfo);
printf("MyVectoredHandler\n");
return EXCEPTION_CONTINUE_SEARCH;
}
LONG WINAPI MyUnhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo)
{
printf("SetUnhandledExceptionFilter\n");
return EXCEPTION_CONTINUE_SEARCH;
}
void f()
{
__try
{
char p[20] = "hello,world!";
p[24] = '!';
printf("%s\n", p);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
printf("f() exception\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
AddVectoredExceptionHandler(CALL_FIRST, MyVectoredHandler);
SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
try{
f();
}
catch (...){
printf("catched f exception\n");
}
return 0;
}
std::string
来表示字符串,并使用at
成员函数进行索引。 - user1084944