由 错误号 得到 错误字符串

来源:互联网 发布:深圳易佰网络怎么样 编辑:程序博客网 时间:2024/05/21 08:41
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 throughASSERT(!bOK);// Note: DELETE_EXCEPTION_(e) not required}AND_CATCH_ALL(e){// validation failed due to OOM or other resource failuree->ReportError(MB_ICONEXCLAMATION, AFX_IDP_INTERNAL_FAILURE);ASSERT(!bOK);DELETE_EXCEPTION(e);}END_CATCH_ALL


原创粉丝点击