#include<Windows.h>
#include<tchar.h>
#include<stdio.h>
void ReportError(LPCTSTR userMessage, DWORD exitCode, BOOL printErrorMessage) {
DWORD eMsgLen, errNum = GetLastError();
LPTSTR lpvSysMsg;
_ftprintf(stderr, _T("%s\n"), userMessage);
if (printErrorMessage) {
eMsgLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL, errNum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpvSysMsg, 0, NULL);
if (eMsgLen > 0) {
_ftprintf(stderr, "%s\n", lpvSysMsg);
}
else {
_ftprintf(stderr, _T("Last Error Number;%d.\n"), errNum);
}
if (lpvSysMsg != NULL)LocalFree(lpvSysMsg);//注意释放内存,否则会造成内存泄露
}
if (exitCode > 0)ExitProcess(exitCode);
return;
}
错误信息输出
于 2022-10-13 13:59:31 首次发布