MTVERIFY.H 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * MtVerify.h
  3. *
  4. * Error handling for applications in
  5. * "Multitheading Applications in Win32"
  6. *
  7. * The function PrintError() is marked as __inline so that it can be
  8. * included from one or more C or C++ files without multiple definition
  9. * errors. For the examples in this book, this works fine.
  10. * To use the PrintError() in an application, it should be taken out,
  11. * placed in its own source file, and the "__inline" declaration removed
  12. * so the function will be globally available.
  13. */
  14. #pragma comment( lib, "USER32" )
  15. #include <crtdbg.h>
  16. #define MTASSERT(a) _ASSERTE(a)
  17. #define MTVERIFY(a) if (!(a)) PrintError(#a,__FILE__,__LINE__,GetLastError())
  18. __inline void PrintError(LPSTR linedesc, LPSTR filename, int lineno, DWORD errnum)
  19. {
  20. LPSTR lpBuffer;
  21. char errbuf[256];
  22. #ifdef _WINDOWS
  23. char modulename[MAX_PATH];
  24. #else // _WINDOWS
  25. DWORD numread;
  26. #endif // _WINDOWS
  27. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
  28. | FORMAT_MESSAGE_FROM_SYSTEM,
  29. NULL,
  30. errnum,
  31. LANG_NEUTRAL,
  32. (LPTSTR)&lpBuffer,
  33. 0,
  34. NULL );
  35. wsprintf(errbuf, "\nThe following call failed at line %d in %s:\n\n"
  36. " %s\n\nReason: %s\n", lineno, filename, linedesc, lpBuffer);
  37. #ifndef _WINDOWS
  38. WriteFile(GetStdHandle(STD_ERROR_HANDLE), errbuf, strlen(errbuf), &numread, FALSE );
  39. Sleep(3000);
  40. #else
  41. GetModuleFileName(NULL, modulename, MAX_PATH);
  42. MessageBox(NULL, errbuf, modulename, MB_ICONWARNING|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
  43. #endif
  44. exit(EXIT_FAILURE);
  45. }