MTVERIFY.H 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #ifndef _MTVERIFY_HEADER_
  15. #define _MTVERIFY_HEADER_
  16. #pragma comment( lib, "USER32" )
  17. #include <strsafe.h>
  18. #include <crtdbg.h>
  19. #define MTASSERT(a) _ASSERTE(a)
  20. #define MTVERIFY(a) if (!(a)) PrintError(#a,__FILE__,__LINE__,GetLastError())
  21. __inline void PrintError(LPTSTR linedesc, LPTSTR filename, int lineno, DWORD errnum)
  22. {
  23. LPTSTR lpBuffer;
  24. TCHAR errbuf[MAX_PATH];
  25. #ifdef _WINDOWS
  26. TCHAR modulename[MAX_PATH];
  27. #else // _WINDOWS
  28. DWORD numread;
  29. #endif // _WINDOWS
  30. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM,
  31. NULL,
  32. errnum,
  33. LANG_NEUTRAL,
  34. (LPTSTR)&lpBuffer,
  35. 0,
  36. NULL );
  37. // _stprintf(errbuf, _T("\nThe following call failed at line %d in %s:\n\n%s\n\nReason: %s\n"), lineno, filename, linedesc, lpBuffer);
  38. StringCchPrintf(errbuf, MAX_PATH, _T("\nThe following call failed at line %d in %s:\n\n%s\n\nReason: %s\n"), lineno, filename, linedesc, lpBuffer);
  39. #ifndef _WINDOWS
  40. WriteFile(GetStdHandle(STD_ERROR_HANDLE), errbuf, _tcslen(errbuf), &numread, FALSE );
  41. Sleep(3000);
  42. #else
  43. GetModuleFileName(NULL, modulename, MAX_PATH);
  44. //MessageBox(NULL, errbuf, modulename, MB_ICONWARNING|MB_OK|MB_TASKMODAL|MB_SETFOREGROUND);
  45. #endif
  46. exit(EXIT_FAILURE);
  47. }
  48. #endif