#pragma comment( lib, "USER32" )

#include <crtdbg.h>
#define MTASSERT(a) _ASSERTE(a)

#define MTVERIFY(a) if (!(a)) PrintError(L#a, __FUNCTIONW__, __LINE__, GetLastError())

__inline void PrintError(LPWSTR linedesc, LPWSTR filename, int lineno, DWORD errnum)
{
	LPWSTR lpBuffer;
	WCHAR errbuf[256];
#ifdef _WINDOWS
	WCHAR 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 );

	swprintf_s(
		errbuf, 
		_T("\nThe following call failed at line %d in %s:\n\n\t%s\n\nReason: %s\n"), 
		lineno, 
		filename, 
		linedesc, 
		lpBuffer);

#ifndef _WINDOWS
	WriteFile(GetStdHandle(STD_ERROR_HANDLE), errbuf, _tcslen(errbuf), &numread, FALSE );
	Sleep(3000);
#else
	GetModuleFileNameW(NULL, modulename, MAX_PATH);
	MessageBoxW(NULL, errbuf, modulename, MB_ICONWARNING|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
#endif
	exit(EXIT_FAILURE);
}
