Source code of Windows XP (NT5)
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.

307 lines
8.4 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. util.CPP
  5. Abstract:
  6. File containing utility classes
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 04/27/99
  9. - Created
  10. Jim Martin (a-jammar) 04/30/99
  11. - Changed to use global IWbemServices pointer, and added
  12. GetWbemServices, CopyProperty, and GetCIMDataFile
  13. Ghim-Sim Chua (gschua) 05/01/99
  14. - Modified GetWbemServices, GetCIMDataFile
  15. Kalyani Narlanka (kalyanin) 05/11/99
  16. - Added the function GetCompletePath
  17. ********************************************************************/
  18. #include "pchealth.h"
  19. /////////////////////////////////////////////////////////////////////////////
  20. // tracing stuff
  21. #ifdef THIS_FILE
  22. #undef THIS_FILE
  23. #endif
  24. static char __szTraceSourceFile[] = __FILE__;
  25. #define THIS_FILE __szTraceSourceFile
  26. #define TRACE_ID DCID_UTIL
  27. /////////////////////////////////////////////////////////////////////////////
  28. // utility functions
  29. // ****************************************************************************
  30. HRESULT GetWbemServices(IWbemServices **ppServices)
  31. {
  32. TraceFunctEnter("GetWbemServices");
  33. IWbemLocator *pWbemLocator = NULL;
  34. HRESULT hr = NOERROR;
  35. // If global variable already initialized, use it
  36. if (g_pWbemServices)
  37. {
  38. *ppServices = g_pWbemServices;
  39. (*ppServices)->AddRef();
  40. goto done;
  41. }
  42. // First we have the get the IWbemLocator object with a CoCreateInstance.
  43. hr = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL,
  44. CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
  45. IID_IUnknown, (void **)&pWbemLocator);
  46. if (FAILED(hr))
  47. {
  48. ErrorTrace(TRACE_ID, "CoCreateInstance failed to create IWbemAdministrativeLocator.");
  49. goto done;
  50. }
  51. // Then we connect to the WMI server for the local CIMV2 namespace.
  52. hr = pWbemLocator->ConnectServer(CComBSTR(CIM_NAMESPACE), NULL, NULL, NULL, 0, NULL, NULL, ppServices);
  53. if (FAILED(hr))
  54. {
  55. ErrorTrace(TRACE_ID, "ConnectServer failed to connect to cimv2 namespace.");
  56. goto done;
  57. }
  58. // Store it in the global variable
  59. g_pWbemServices = *ppServices;
  60. // BUGBUG: check out why this stops fault on NET STOP WINMGMT
  61. (*ppServices)->AddRef();
  62. done:
  63. if (pWbemLocator != NULL)
  64. pWbemLocator->Release();
  65. TraceFunctLeave();
  66. return hr;
  67. }
  68. // ****************************************************************************
  69. HRESULT ExecWQLQuery(IEnumWbemClassObject **ppEnumInst, BSTR bstrQuery)
  70. {
  71. TraceFunctEnter("ExecWQLQuery");
  72. IWbemServices *pWbemServices = NULL;
  73. HRESULT hr = NOERROR;
  74. // Get pointer to WbemServices
  75. hr = GetWbemServices(&pWbemServices);
  76. if (FAILED(hr))
  77. goto done;
  78. // execute the query
  79. hr = pWbemServices->ExecQuery(CComBSTR("WQL"), bstrQuery,
  80. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  81. NULL, ppEnumInst);
  82. if (FAILED(hr))
  83. {
  84. ErrorTrace(TRACE_ID, "ExecQuery failed: 0x%08x", hr);
  85. goto done;
  86. }
  87. done:
  88. if (pWbemServices != NULL)
  89. pWbemServices->Release();
  90. TraceFunctLeave();
  91. return hr;
  92. }
  93. // ****************************************************************************
  94. HRESULT CopyProperty(IWbemClassObject *pFrom, LPCWSTR szFrom, CInstance *pTo,
  95. LPCWSTR szTo)
  96. {
  97. TraceFunctEnter("CopyProperty");
  98. _ASSERT(pFrom && szFrom && pTo && szTo);
  99. CComVariant varValue;
  100. CComBSTR bstrFrom;
  101. HRESULT hr = NOERROR;
  102. // First, get the property (as a variant) from the source class object.
  103. bstrFrom = szFrom;
  104. hr = pFrom->Get(bstrFrom, 0, &varValue, NULL, NULL);
  105. if (FAILED(hr))
  106. {
  107. ErrorTrace(TRACE_ID, "GetVariant on %s field failed.", szFrom);
  108. }
  109. else
  110. {
  111. // Then set the variant for the target CInstance object.
  112. if (pTo->SetVariant(szTo, varValue) == FALSE)
  113. {
  114. ErrorTrace(TRACE_ID, "SetVariant on %s field failed.", szTo);
  115. hr = WBEM_E_FAILED;
  116. }
  117. }
  118. TraceFunctLeave();
  119. return hr;
  120. }
  121. // ****************************************************************************
  122. HRESULT GetCIMDataFile(BSTR bstrFile, IWbemClassObject **ppFileObject,
  123. BOOL fHasDoubleSlashes)
  124. {
  125. TraceFunctEnter("GetCIMDataFile");
  126. IWbemServices *pWbemServices = NULL;
  127. HRESULT hr = NOERROR;
  128. CComBSTR bstrObjectPath;
  129. wchar_t *pwch;
  130. UINT uLen;
  131. if (bstrFile == NULL || ppFileObject == NULL)
  132. {
  133. ErrorTrace(TRACE_ID, "Parameter pointer is null.");
  134. hr = WBEM_E_INVALID_PARAMETER;
  135. goto done;
  136. }
  137. hr = GetWbemServices(&pWbemServices);
  138. if (FAILED(hr))
  139. goto done;
  140. // Construct the path for the file we are trying to get. Note, the path needs
  141. // the have double backslashes for the GetObject call to work. We scan through
  142. // the string and do this manually here.
  143. bstrObjectPath = "\\\\.\\root\\cimv2:CIM_DataFile.Name=\"";
  144. pwch = bstrFile;
  145. if (fHasDoubleSlashes)
  146. {
  147. bstrObjectPath.Append(pwch, SysStringLen(bstrFile));
  148. }
  149. else
  150. {
  151. for (uLen = SysStringLen(bstrFile); uLen > 0; uLen--)
  152. {
  153. if (*pwch == L'\\')
  154. bstrObjectPath.Append("\\");
  155. bstrObjectPath.Append(pwch, 1);
  156. pwch++;
  157. }
  158. }
  159. bstrObjectPath.Append("\"");
  160. // Make the call to get the CIM_DataFile object.
  161. hr = pWbemServices->GetObject(bstrObjectPath, 0, NULL, ppFileObject, NULL);
  162. if (FAILED(hr))
  163. ErrorTrace(TRACE_ID, "GetObject on CIM_DataFile failed.");
  164. done:
  165. if (pWbemServices != NULL)
  166. pWbemServices->Release();
  167. TraceFunctLeave();
  168. return hr;
  169. }
  170. // ****************************************************************************
  171. HRESULT GetCIMObj(BSTR bstrPath, IWbemClassObject **ppObj, long lFlags)
  172. {
  173. TraceFunctEnter("GetCIMObj");
  174. IWbemServices *pWbemServices = NULL;
  175. HRESULT hr = NOERROR;
  176. if (bstrPath == NULL || ppObj == NULL)
  177. {
  178. ErrorTrace(TRACE_ID, "bad parameters");
  179. hr = WBEM_E_INVALID_PARAMETER;
  180. goto done;
  181. }
  182. // make sure we have a services object
  183. hr = GetWbemServices(&pWbemServices);
  184. if (FAILED(hr))
  185. goto done;
  186. // Make the call to get the CIM_DataFile object.
  187. hr = pWbemServices->GetObject(bstrPath, lFlags, NULL, ppObj, NULL);
  188. if (FAILED(hr))
  189. ErrorTrace(TRACE_ID, "GetObject failed: 0x%08x", hr);
  190. done:
  191. if (pWbemServices != NULL)
  192. pWbemServices->Release();
  193. TraceFunctLeave();
  194. return hr;
  195. }
  196. // ****************************************************************************
  197. BOOL getCompletePath(CComBSTR bstrFileName, CComBSTR &bstrFileWithPathName)
  198. {
  199. // Return
  200. BOOL bFoundFile = FALSE;
  201. ULONG uiReturn;
  202. TCHAR szDirectory[MAX_PATH];
  203. TCHAR temp[MAX_PATH];
  204. TCHAR lpstrTemp[MAX_PATH];
  205. struct _stat filestat;
  206. CComVariant varValue = NULL;
  207. CComBSTR bstrDirectory;
  208. // Check for the File in the System Directory
  209. uiReturn = GetSystemDirectory(szDirectory, MAX_PATH);
  210. if (uiReturn != 0 && uiReturn < MAX_PATH)
  211. {
  212. bstrDirectory = szDirectory;
  213. bstrDirectory.Append("\\");
  214. bstrDirectory.Append(bstrFileName);
  215. USES_CONVERSION;
  216. int Result = _tstat(W2T(bstrDirectory), &filestat) ;
  217. if (Result == 0)
  218. {
  219. bstrFileWithPathName = bstrDirectory;
  220. bFoundFile = TRUE;
  221. }
  222. }
  223. // If not there, then check in the windows directory.
  224. if (!bFoundFile)
  225. {
  226. uiReturn = GetWindowsDirectory(szDirectory, MAX_PATH);
  227. if (uiReturn != 0 && uiReturn < MAX_PATH)
  228. {
  229. bstrDirectory = szDirectory;
  230. bstrDirectory.Append("\\");
  231. bstrDirectory.Append(bstrFileName);
  232. USES_CONVERSION;
  233. int Result = _tstat(W2T(bstrDirectory), &filestat) ;
  234. if (Result == 0)
  235. {
  236. bstrFileWithPathName = bstrDirectory;
  237. bFoundFile = TRUE;
  238. }
  239. }
  240. }
  241. return(bFoundFile);
  242. }