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.

133 lines
2.9 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. TRACE.CPP
  5. Abstract:
  6. Support of trace output and internationalized strings.
  7. History:
  8. a-davj 13-July-97 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include <wbemcli.h>
  12. #include "trace.h"
  13. #include <autoptr.h>
  14. extern HINSTANCE ghModule;
  15. TCHAR JustInCase = 0;
  16. BSTR GetFromStdErrorFacility(HRESULT hres)
  17. {
  18. HRESULT hTemp = hres;
  19. // Certain strings are obtained from the standard facility rather
  20. // than the local string table.
  21. if(hres == WBEM_E_NOT_FOUND || hres == WBEM_E_TYPE_MISMATCH || hres == WBEM_E_OVERRIDE_NOT_ALLOWED ||
  22. hres == WBEM_E_PROPAGATED_QUALIFIER || hres == WBEM_E_VALUE_OUT_OF_RANGE)
  23. return NULL;
  24. // we are only interested in 0x8004xxxx values.
  25. hTemp &= 0xffff0000;
  26. if(hTemp != 0x80040000)
  27. return NULL;
  28. // attempt to read the string from the usual place
  29. IWbemStatusCodeText * pStatus = NULL;
  30. SCODE sc = CoCreateInstance(CLSID_WbemStatusCodeText, 0, CLSCTX_INPROC_SERVER,
  31. IID_IWbemStatusCodeText, (LPVOID *) &pStatus);
  32. if(FAILED(sc))
  33. return NULL;
  34. BSTR bstrError = 0;
  35. sc = pStatus->GetErrorCodeText(hres, 0, 0, &bstrError);
  36. pStatus->Release();
  37. if(sc == S_OK)
  38. return bstrError;
  39. else
  40. return NULL;
  41. }
  42. IntString::IntString(DWORD dwID)
  43. {
  44. DWORD dwSize, dwRet, dwLen = 0;
  45. // see if the message can be obtained for the standard place
  46. BSTR bstrErrMsg = GetFromStdErrorFacility((HRESULT)dwID);
  47. if(bstrErrMsg)
  48. {
  49. dwLen = lstrlen(bstrErrMsg)+1;
  50. m_pString = new TCHAR[dwLen];
  51. if(m_pString == NULL)
  52. {
  53. SysFreeString(bstrErrMsg);
  54. m_pString = &JustInCase; // should never happen!
  55. return;
  56. }
  57. StringCchCopyW(m_pString, dwLen, bstrErrMsg);
  58. SysFreeString(bstrErrMsg);
  59. return;
  60. }
  61. // Get the message from the string table.
  62. m_pString = &JustInCase;
  63. for(dwSize = 128; dwSize < 4096; dwSize *= 2)
  64. {
  65. wmilib::auto_buffer<TCHAR> p(new TCHAR[dwSize]);
  66. if(NULL == p.get())
  67. {
  68. return;
  69. }
  70. dwRet = LoadString( ghModule, dwID, p.get(), dwSize);
  71. // Check for failure to load
  72. if(dwRet == 0)
  73. {
  74. return;
  75. }
  76. // Check for the case where the buffer was too small
  77. if((dwRet + 1) >= dwSize)
  78. {
  79. //overrun, just loop
  80. }
  81. else
  82. {
  83. m_pString = p.release();
  84. return; // all is well!
  85. }
  86. }
  87. }
  88. IntString::~IntString()
  89. {
  90. if(m_pString != &JustInCase)
  91. delete(m_pString);
  92. }
  93. void CopyOrConvert(TCHAR * pTo, WCHAR * pFrom, int iLen)
  94. {
  95. #ifdef UNICODE
  96. wcsncpy(pTo, pFrom,iLen);
  97. #else
  98. wcstombs(pTo, pFrom, iLen);
  99. #endif
  100. pTo[iLen-1] = 0;
  101. return;
  102. }