您的位置:首页 > 其它

由 错误号 得到 错误字符串

2011-09-22 15:38 148 查看
FormatMessageA(//FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_FROM_SYSTEM|
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0,//Default language
lpErrMsgBuf,
100,
NULL);
printf("Fail Starting the Service. Error %ld: %s",GetLastError(),lpErrMsgBuf);
LocalFree(lpErrMsgBuf);

更可以编成宏:

#define PRINT_ERR(x, err) {\
char lpErrMsgBuf_75367[101]={0};\
int errNum_25456 = err;\
FormatMessageA(\
FORMAT_MESSAGE_FROM_SYSTEM|\
FORMAT_MESSAGE_IGNORE_INSERTS,\
NULL,\
errNum_25456,\
0,/*Default language*/\
lpErrMsgBuf_75367,\
100,\
NULL);\
printf(x, errNum_25456, lpErrMsgBuf_75367);}

调用时使用 PRINT_ERR("xxxxxxxx failed! Error %d : %s\n", GetLastError());

现在发现,为何不用CException类呢

TRY
{
DoDataExchange(&dx);
bOK = TRUE;         // it worked
}
CATCH(CUserException, e)
{
// validation failed - user already alerted, fall through
ASSERT(!bOK);
// Note: DELETE_EXCEPTION_(e) not required
}
AND_CATCH_ALL(e)
{
// validation failed due to OOM or other resource failure
e->ReportError(MB_ICONEXCLAMATION, AFX_IDP_INTERNAL_FAILURE);
ASSERT(!bOK);
DELETE_EXCEPTION(e);
}
END_CATCH_ALL
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: