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.

106 lines
2.5 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. //--------------PUBLIC-----------------------------
  19. #define TEMP_BUF 512
  20. POLARITY bool ErrorStringEx(HRESULT hr,
  21. TCHAR *errMsg, UINT errSize,
  22. UINT *sevIcon)
  23. {
  24. WCHAR szError[TEMP_BUF];
  25. WCHAR szFacility[TEMP_BUF];
  26. WCHAR szFormat[100];
  27. IWbemStatusCodeText * pStatus = NULL;
  28. // initialize buffers.
  29. errMsg[0] = 0;
  30. szFacility[0] = 0;
  31. szError[0] = 0;
  32. HRESULT hr1 = CoInitialize(NULL);
  33. SCODE sc1 = CoCreateInstance(CLSID_WbemStatusCodeText,
  34. 0, CLSCTX_INPROC_SERVER,
  35. IID_IWbemStatusCodeText,
  36. (LPVOID *) &pStatus);
  37. // loaded OK?
  38. if(sc1 == S_OK)
  39. {
  40. BSTR bstr = 0;
  41. sc1 = pStatus->GetErrorCodeText(hr, 0, 0, &bstr);
  42. if(sc1 == S_OK)
  43. {
  44. _tcsncpy(szError, bstr, TEMP_BUF-1);
  45. SysFreeString(bstr);
  46. bstr = 0;
  47. }
  48. sc1 = pStatus->GetFacilityCodeText(hr, 0, 0, &bstr);
  49. if(sc1 == S_OK)
  50. {
  51. _tcsncpy(szFacility, bstr, TEMP_BUF-1);
  52. SysFreeString(bstr);
  53. bstr = 0;
  54. }
  55. // RELEASE
  56. pStatus->Release();
  57. pStatus = NULL;
  58. }
  59. else
  60. {
  61. ::MessageBox(NULL, _T("WBEM error features not available. Upgrade WMI to a newer build."),
  62. _T("Internal Error"), MB_ICONSTOP|MB_OK);
  63. }
  64. // if not msgs returned....
  65. if(wcslen(szFacility) == 0 || wcslen(szError) == 0)
  66. {
  67. // format the error nbr as a reasonable default.
  68. LoadString(GetModuleHandle(_T("MMFUtil.dll")), IDS_ERROR_UNKN_ERROR_FMT, szFormat, 99);
  69. _stprintf(errMsg, szFormat, hr);
  70. }
  71. else
  72. {
  73. // format a readable msg.
  74. LoadString(GetModuleHandle(_T("MMFUtil.dll")), IDS_ERROR_FMT, szFormat, 99);
  75. _stprintf(errMsg, szFormat, szFacility, szError);
  76. }
  77. // want an icon recommendation with that?
  78. if(sevIcon)
  79. {
  80. switch(SCODE_SEVERITY(hr))
  81. {
  82. case 0: // - Success
  83. *sevIcon = MB_ICONINFORMATION;
  84. break;
  85. case 1: //- Failed
  86. *sevIcon = MB_ICONEXCLAMATION;
  87. break;
  88. } //endswitch severity
  89. } //endif sevIcon
  90. if(hr1 == S_OK)
  91. CoUninitialize();
  92. return (SUCCEEDED(sc1) && SUCCEEDED(hr1));
  93. }