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