Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
2.8 KiB

  1. // Error.cpp: implementation of the CError class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Error.h"
  6. #include "debug.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CError::CError()
  11. {
  12. }
  13. CError::~CError()
  14. {
  15. }
  16. void CError::ErrorMsgBox(HRESULT hr)
  17. {
  18. LPTSTR lpMsgBuf;
  19. FormatMessage(
  20. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  21. FORMAT_MESSAGE_FROM_SYSTEM |
  22. FORMAT_MESSAGE_IGNORE_INSERTS,
  23. NULL,
  24. hr,
  25. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  26. (LPTSTR) &lpMsgBuf,
  27. 0,
  28. NULL
  29. );
  30. // Display the string.
  31. MessageBox( NULL, lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION );
  32. // Free the buffer.
  33. LocalFree( lpMsgBuf );
  34. }
  35. void CError::ErrorTrace(HRESULT hr,LPCSTR szStr,LPCSTR szFile,int iLine)
  36. {
  37. CHAR tmp[2048];
  38. LPSTR lpMsgBuf;
  39. DWORD iMsgBuf = FormatMessageA(
  40. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  41. FORMAT_MESSAGE_FROM_SYSTEM |
  42. FORMAT_MESSAGE_IGNORE_INSERTS,
  43. NULL,
  44. hr,
  45. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  46. (LPSTR) &lpMsgBuf,
  47. 0,
  48. NULL
  49. );
  50. if(iMsgBuf == 0)
  51. {
  52. _ASSERTE(!lpMsgBuf);
  53. lpMsgBuf = "\n";
  54. }
  55. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  56. if(hStdOut != INVALID_HANDLE_VALUE)
  57. {
  58. DWORD i = wsprintfA(tmp,"%s: Error [%x] %s",szStr,hr,lpMsgBuf);
  59. if(i)
  60. {
  61. Trace(tmp);
  62. }
  63. if(HRESULT_FACILITY(hr) == FACILITY_ITF)
  64. {
  65. USES_CONVERSION;
  66. HRESULT hres = S_OK;
  67. IErrorInfo *pIErrInfo = NULL;
  68. hres = GetErrorInfo(NULL,&pIErrInfo);
  69. if(hres == S_OK)
  70. {
  71. BSTR bstr = NULL;
  72. BSTR bstrHelpFile = NULL;
  73. hres = pIErrInfo->GetDescription(&bstr);
  74. hres = pIErrInfo->GetHelpFile(&bstrHelpFile);
  75. i = wsprintfA(tmp,"IErrInf: %s ",OLE2A(bstr));
  76. Trace(tmp);
  77. SysFreeString(bstr);
  78. SysFreeString(bstrHelpFile);
  79. pIErrInfo->Release();
  80. }
  81. }
  82. i = wsprintfA(tmp,"in %s line %d.",szFile,iLine);
  83. if(i)
  84. {
  85. Trace(tmp);
  86. }
  87. Trace(tmp);
  88. }
  89. // Free the buffer.
  90. if(iMsgBuf > 0)
  91. LocalFree( lpMsgBuf );
  92. }
  93. void CError::Trace(LPCTSTR szStr)
  94. {
  95. #ifdef TRACE
  96. ::TRACE(szStr);
  97. #else
  98. USES_CONVERSION;
  99. LPCSTR szStrA = T2A(szStr);
  100. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  101. if(hStdOut != INVALID_HANDLE_VALUE)
  102. {
  103. DWORD i = strlen(szStrA);
  104. DWORD j;
  105. WriteFile(hStdOut,szStrA,i,&j,NULL);
  106. }
  107. #endif
  108. }
  109. void CError::Trace(LPCSTR szStr)
  110. {
  111. #ifdef TRACE
  112. USES_CONVERSION;
  113. LPCTSTR szStrW = A2T(szStr);
  114. ::TRACE(szStrW);
  115. #else
  116. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  117. if(hStdOut != INVALID_HANDLE_VALUE)
  118. {
  119. DWORD i = strlen(szStr);
  120. DWORD j;
  121. WriteFile(hStdOut,szStr,i,&j,NULL);
  122. }
  123. #endif
  124. }