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.

225 lines
6.0 KiB

  1. /*++
  2. // Copyright (c) 1998-2001 Microsoft Corporation, All Rights Reserved
  3. Module Name:
  4. WBEMERROR.CPP
  5. Abstract:
  6. Implements string table based, error msgs for all of wbem.
  7. History:
  8. a-khint 5-mar-98 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "WbemError.h"
  12. #include "commain.h"
  13. #include "resource.h"
  14. #include "wbemcli.h"
  15. #include <stdio.h>
  16. extern HINSTANCE g_hInstance;
  17. //-------------------------------------------------
  18. bool LoadMyString(UINT ID,
  19. LPTSTR str, UINT size,
  20. LPCTSTR def)
  21. {
  22. bool retval = true;
  23. if(str)
  24. {
  25. // TCHAR *szStr = new TCHAR[size];
  26. if (LoadString(g_hInstance, ID, str, size) == 0)
  27. {
  28. if(def)
  29. lstrcpyn(str, def, size);
  30. }
  31. /* else
  32. {
  33. mbstowcs(str, szStr, size);
  34. retval = true;
  35. }
  36. */
  37. // delete [] szStr;
  38. }
  39. return retval;
  40. }
  41. DWORD MyFormatMessage(DWORD dwFlags,
  42. LPCVOID lpSource,
  43. DWORD dwMessageId,
  44. DWORD dwLanguageId,
  45. PVOID* lpBuffer,
  46. DWORD nSize,
  47. va_list *Arguments)
  48. {
  49. DWORD dwRet = 0;
  50. PVOID p;
  51. if(lpBuffer)
  52. {
  53. dwRet = FormatMessage(dwFlags|FORMAT_MESSAGE_ALLOCATE_BUFFER,lpSource,dwMessageId,dwLanguageId,(LPTSTR)lpBuffer,
  54. nSize,Arguments);
  55. }
  56. return dwRet;
  57. }
  58. //-------------------------------------------------
  59. DWORD WbemErrorString(SCODE sc,
  60. PVOID* errMsg, UINT errSize, DWORD dwFlags=FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_HMODULE)
  61. {
  62. return MyFormatMessage(dwFlags,
  63. g_hInstance, sc,
  64. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  65. errMsg, errSize, NULL);
  66. }
  67. HRESULT CWbemError::GetErrorCodeText(HRESULT hRes, LCID LocaleId, long lFlags, BSTR * MessageText)
  68. {
  69. PVOID errMsg = 0;
  70. int errSize = 256;
  71. DWORD dwMsgSize = 0;
  72. *MessageText = NULL;
  73. DWORD dwFlags = FORMAT_MESSAGE_IGNORE_INSERTS;
  74. if(LocaleId != 0 )
  75. return WBEM_E_INVALID_PARAMETER;
  76. // If WBEMSTATUS_FORMAT_NO_NEWLINE is specified update FormatMessage mask
  77. if(lFlags==WBEMSTATUS_FORMAT_NO_NEWLINE)
  78. dwFlags|=FORMAT_MESSAGE_MAX_WIDTH_MASK; // No newline mask for FormatMessage
  79. else if (lFlags!=WBEMSTATUS_FORMAT_NEWLINE)
  80. return WBEM_E_INVALID_PARAMETER;
  81. // If the facility code is wbem, try loading the error from the wbem strings
  82. if(SCODE_FACILITY(hRes) == FACILITY_ITF)
  83. dwMsgSize = WbemErrorString(hRes, &errMsg, errSize, dwFlags|FORMAT_MESSAGE_FROM_HMODULE);
  84. if(dwMsgSize == 0)
  85. dwMsgSize = MyFormatMessage(dwFlags|FORMAT_MESSAGE_FROM_SYSTEM,
  86. NULL, hRes,
  87. MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  88. &errMsg, errSize, NULL);
  89. if(dwMsgSize > 0)
  90. {
  91. *MessageText = SysAllocString((LPCTSTR)errMsg);
  92. LocalFree(errMsg);
  93. if(*MessageText)
  94. return S_OK;
  95. }
  96. return WBEM_E_FAILED;
  97. }
  98. HRESULT CWbemError::GetFacilityCodeText(HRESULT sc, LCID LocaleId, long lFlags, BSTR * MessageText)
  99. {
  100. TCHAR facility[50];
  101. int facSize = 50;
  102. PVOID wTemp = 0;
  103. bool bLoaded = false;
  104. *MessageText = NULL;
  105. if(LocaleId != 0 || lFlags != 0)
  106. return WBEM_E_INVALID_PARAMETER;
  107. switch(SCODE_FACILITY(sc))
  108. {
  109. case FACILITY_ITF:
  110. if(WbemErrorString(sc, &wTemp, 256))
  111. {
  112. LocalFree(wTemp);
  113. bLoaded = LoadMyString(IDS_FAC_WBEM,
  114. facility, facSize,
  115. __TEXT("WMI"));
  116. break;
  117. }
  118. else
  119. bLoaded = LoadMyString(IDS_FAC_ITF,
  120. facility, facSize,
  121. __TEXT("Interface"));
  122. break;
  123. case FACILITY_NULL:
  124. bLoaded = LoadMyString(IDS_FAC_NULL,
  125. facility, facSize,
  126. __TEXT("<Null>"));
  127. break;
  128. case FACILITY_RPC:
  129. bLoaded = LoadMyString(IDS_FAC_RPC,
  130. facility, facSize,
  131. __TEXT("RPC"));
  132. break;
  133. case FACILITY_STORAGE:
  134. bLoaded = LoadMyString(IDS_FAC_STORAGE,
  135. facility, facSize,
  136. __TEXT("Storage"));
  137. break;
  138. case FACILITY_DISPATCH:
  139. bLoaded = LoadMyString(IDS_FAC_DISPATCH,
  140. facility, facSize,
  141. __TEXT("Dispatch"));
  142. break;
  143. case FACILITY_WIN32:
  144. bLoaded = LoadMyString(IDS_FAC_WIN32,
  145. facility, facSize,
  146. __TEXT("Win32"));
  147. break;
  148. case FACILITY_WINDOWS:
  149. bLoaded = LoadMyString(IDS_FAC_WINDOWS,
  150. facility, facSize,
  151. __TEXT("Windows"));
  152. break;
  153. case FACILITY_SSPI:
  154. bLoaded = LoadMyString(IDS_FAC_SSPI,
  155. facility, facSize,
  156. __TEXT("SSPI"));
  157. break;
  158. case FACILITY_CONTROL:
  159. bLoaded = LoadMyString(IDS_FAC_CONTROL,
  160. facility, facSize,
  161. __TEXT("Control"));
  162. // get error msg from the system.
  163. break;
  164. case FACILITY_CERT:
  165. bLoaded = LoadMyString(IDS_FAC_CERT,
  166. facility, facSize,
  167. __TEXT("Cert"));
  168. break;
  169. case FACILITY_INTERNET:
  170. bLoaded = LoadMyString(IDS_FAC_INET,
  171. facility, facSize,
  172. __TEXT("Internet"));
  173. break;
  174. default :
  175. bLoaded = LoadMyString(IDS_FAC_UNKNOWN,
  176. facility, facSize,
  177. __TEXT("General"));
  178. break;
  179. } //endswitch
  180. if(bLoaded)
  181. {
  182. *MessageText = SysAllocString(facility);
  183. if(*MessageText)
  184. return S_OK;
  185. }
  186. return WBEM_E_FAILED;
  187. }