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.

118 lines
2.7 KiB

  1. //=============================================================================
  2. //
  3. // WbemError.cpp
  4. //
  5. // Copyright (c) 1997-1999 Microsoft Corporation
  6. //
  7. // Implements string table based, error msgs for all of wbem.
  8. //
  9. // History:
  10. //
  11. // a-khint 5-mar-98 Created.
  12. //
  13. //=============================================================================
  14. #include "precomp.h"
  15. #include "WbemError.h"
  16. #include "resource.h"
  17. #include <wbemcli.h>
  18. #include <stdio.h>
  19. //--------------PUBLIC-----------------------------
  20. #define TEMP_BUF 512
  21. POLARITY bool ErrorStringEx(HRESULT hr,
  22. TCHAR *errMsg, UINT errSize,
  23. UINT *sevIcon)
  24. {
  25. TCHAR szError[TEMP_BUF] = {0};
  26. TCHAR szFacility[TEMP_BUF] = {0};
  27. TCHAR szFormat[100] = {0};
  28. IWbemStatusCodeText * pStatus = NULL;
  29. // initialize buffers.
  30. errMsg[0] = 0;
  31. szFacility[0] = 0;
  32. szError[0] = 0;
  33. HRESULT hr1 = CoInitialize(NULL);
  34. SCODE sc1 = CoCreateInstance(CLSID_WbemStatusCodeText,
  35. 0, CLSCTX_INPROC_SERVER,
  36. IID_IWbemStatusCodeText,
  37. (LPVOID *) &pStatus);
  38. // loaded OK?
  39. if(sc1 == S_OK)
  40. {
  41. BSTR bstr = 0;
  42. sc1 = pStatus->GetErrorCodeText(hr, 0, 0, &bstr);
  43. if(sc1 == S_OK)
  44. {
  45. #ifdef UNICODE
  46. wcsncpy(szError, bstr, TEMP_BUF-1);
  47. size_t x = wcslen(szError);
  48. #else
  49. size_t x = wcstombs(szError, bstr, TEMP_BUF-1);
  50. #endif
  51. szError[x-2] = _T('\0');
  52. SysFreeString(bstr);
  53. bstr = 0;
  54. }
  55. sc1 = pStatus->GetFacilityCodeText(hr, 0, 0, &bstr);
  56. if(sc1 == S_OK)
  57. {
  58. #ifdef UNICODE
  59. wcsncpy(szFacility, bstr, TEMP_BUF-1);
  60. size_t x = wcslen(szFacility);
  61. #else
  62. wcstombs(szFacility, bstr, TEMP_BUF-1);
  63. #endif
  64. SysFreeString(bstr);
  65. bstr = 0;
  66. }
  67. // RELEASE
  68. pStatus->Release();
  69. pStatus = NULL;
  70. }
  71. else
  72. {
  73. ::MessageBox(NULL, _T("WMI error features not available. Upgrade WMI to a newer build."),
  74. _T("Internal Error"), MB_ICONSTOP|MB_OK);
  75. }
  76. // if not msgs returned....
  77. if(_tcslen(szFacility) == 0 || _tcslen(szError) == 0)
  78. {
  79. // format the error nbr as a reasonable default.
  80. LoadString(_Module.GetModuleInstance(), IDS_ERROR_UNKN_ERROR_FMT, szFormat, 99);
  81. _stprintf(errMsg, szFormat, hr);
  82. }
  83. else
  84. {
  85. // format a readable msg.
  86. LoadString(_Module.GetModuleInstance(), IDS_ERROR_FMT, szFormat, 99);
  87. _stprintf(errMsg, szFormat, szFacility, szError);
  88. }
  89. // want an icon recommendation with that?
  90. if(sevIcon)
  91. {
  92. switch(SCODE_SEVERITY(hr))
  93. {
  94. case 0: // - Success
  95. *sevIcon = MB_ICONINFORMATION;
  96. break;
  97. case 1: //- Failed
  98. *sevIcon = MB_ICONEXCLAMATION;
  99. break;
  100. } //endswitch severity
  101. } //endif sevIcon
  102. if(hr1 == S_OK)
  103. CoUninitialize();
  104. return (SUCCEEDED(sc1) && SUCCEEDED(hr1));
  105. }