MTVERIFY 用以捕捉错误并协助找出其原因(针对返回值是BOOL的Win32API表达式)
例如: MTVERIFY( CloseHandle(handle) );
如果调用失败,输出信息格式如下:
The following call failed at line 50 in Demo.c:
CloseHandle(hThread)
Reason: The handle is invalid
#include <crtdbg.h> // 为了引入_ASSERTE
#pragma comment( lib, "USER32" )
#define MTASSERT(a) _ASSERTE(a)
#define MTVERIFY(a) if (!(a))
PrintError(#a,__FILE__,__LINE__,GetLastError())
void PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum);
static void PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum)
{
LPSTR lpBuffer;
char errbuf[256];
#ifdef _WINDOWS
char modulename[MAX_PATH];
#else // _WINDOWS
DWORD numread;
#endif // _WINDOWS
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errnum,
LANG_NEUTRAL,
(LPTSTR)&lpBuffer,
0,
NULL );
wsprintf(errbuf, "\nThe following call failed at line %d in %s:\n\n"
" %s\n\nReason: %s\n", lineno, filename, linedesc, lpBuffer);
#ifndef _WINDOWS
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
errbuf, strlen(errbuf), &numread, FALSE );
Sleep(3000);
#else
GetModuleFileName(NULL, modulename, MAX_PATH);
MessageBox(NULL, errbuf, modulename,
MB_ICONWARNING|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
#endif
exit(EXIT_FAILURE);
}