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.

295 lines
11 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <netsh.h>
  6. #include "objbase.h"
  7. #include "Wbemidl.h"
  8. extern "C"
  9. {
  10. UINT g_CIMOSType = 0;
  11. UINT g_CIMOSProductSuite = 0;
  12. UINT g_CIMProcessorArchitecture = 0;
  13. WCHAR g_CIMOSVersion[MAX_PATH];
  14. WCHAR g_CIMOSBuildNumber[MAX_PATH];
  15. WCHAR g_CIMServicePackMajorVersion[MAX_PATH];
  16. WCHAR g_CIMServicePackMinorVersion[MAX_PATH];
  17. BOOL g_CIMAttempted = FALSE;
  18. BOOL g_CIMSucceeded = FALSE;
  19. HRESULT WINAPI
  20. UpdateVersionInfoGlobals(LPCWSTR pwszMachine);
  21. }
  22. HRESULT WINAPI
  23. UpdateVersionInfoGlobals(LPCWSTR pwszMachine)
  24. {
  25. HRESULT hr = S_OK;
  26. g_CIMAttempted = TRUE;
  27. g_CIMSucceeded = FALSE;
  28. hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  29. if (FAILED(hr) && (RPC_E_CHANGED_MODE != hr))
  30. {
  31. return hr;
  32. }
  33. // Create an instance of the WbemLocator interface.
  34. IWbemLocator *pIWbemLocator = NULL;
  35. hr = CoCreateInstance(CLSID_WbemLocator,
  36. NULL,
  37. CLSCTX_INPROC_SERVER,
  38. IID_IWbemLocator,
  39. (LPVOID *) &pIWbemLocator);
  40. if (FAILED(hr))
  41. {
  42. return hr;
  43. }
  44. IWbemServices *pIWbemServices;
  45. // If already connected, release m_pIWbemServices.
  46. // Using the locator, connect to CIMOM in the given namespace.
  47. BSTR pNamespace;
  48. WCHAR szPath[MAX_PATH];
  49. wsprintf(szPath, L"\\\\%s\\root\\cimv2", !pwszMachine ? L"." : pwszMachine);
  50. pNamespace = SysAllocString(szPath);
  51. hr = pIWbemLocator->ConnectServer(pNamespace,
  52. NULL, //using current account for simplicity
  53. NULL, //using current password for simplicity
  54. 0L, // locale
  55. 0L, // securityFlags
  56. NULL, // authority (domain for NTLM)
  57. NULL, // context
  58. &pIWbemServices);
  59. if (SUCCEEDED(hr))
  60. {
  61. hr = CoSetProxyBlanket(pIWbemServices,
  62. RPC_C_AUTHN_WINNT,
  63. RPC_C_AUTHZ_NONE,
  64. NULL,
  65. RPC_C_AUTHN_LEVEL_CALL,
  66. RPC_C_IMP_LEVEL_IMPERSONATE,
  67. NULL,
  68. EOAC_DEFAULT);
  69. if (SUCCEEDED(hr))
  70. {
  71. IEnumWbemClassObject *pEnum = NULL;
  72. BSTR bstrWQL = SysAllocString(L"WQL");
  73. BSTR bstrPath = SysAllocString(L"select * from Win32_OperatingSystem");
  74. VARIANT varOSType;
  75. VARIANT varOSVersion;
  76. VARIANT varOSProductSuite;
  77. VARIANT varOSBuildNumber;
  78. VARIANT varServicePackMajorVersion;
  79. VARIANT varServicePackMinorVersion;
  80. VARIANT varArchitecture;
  81. hr = pIWbemServices->ExecQuery(bstrWQL, bstrPath, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
  82. if (SUCCEEDED(hr))
  83. {
  84. IWbemClassObject *pNSClass;
  85. ULONG uReturned;
  86. hr = pEnum->Next(WBEM_INFINITE, 1, &pNSClass, &uReturned );
  87. if (SUCCEEDED(hr))
  88. {
  89. if (uReturned)
  90. {
  91. do
  92. {
  93. g_CIMSucceeded = TRUE;
  94. CIMTYPE ctpeType;
  95. hr = pNSClass->Get(L"OSType", NULL, &varOSType, &ctpeType, NULL);
  96. if (SUCCEEDED(hr))
  97. {
  98. hr = VariantChangeType(&varOSType, &varOSType, 0, VT_UINT);
  99. if (SUCCEEDED(hr))
  100. {
  101. g_CIMOSType = varOSType.uintVal;
  102. }
  103. }
  104. if (FAILED(hr))
  105. {
  106. g_CIMSucceeded = FALSE;
  107. break;
  108. }
  109. hr = pNSClass->Get(L"Version", NULL, &varOSVersion, &ctpeType, NULL);
  110. if (SUCCEEDED(hr))
  111. {
  112. hr = VariantChangeType(&varOSVersion, &varOSVersion, 0, VT_BSTR);
  113. if (SUCCEEDED(hr))
  114. {
  115. wcscpy(g_CIMOSVersion, varOSVersion.bstrVal);
  116. }
  117. }
  118. if (FAILED(hr))
  119. {
  120. g_CIMSucceeded = FALSE;
  121. break;
  122. }
  123. hr = pNSClass->Get(L"OSProductSuite", NULL, &varOSProductSuite, &ctpeType, NULL);
  124. if (SUCCEEDED(hr))
  125. {
  126. //
  127. // if the return type is VT_NULL, leave g_CIMOSProductSuite value alone (0)
  128. if (VT_NULL != varOSProductSuite.vt)
  129. {
  130. hr = VariantChangeType(&varOSProductSuite, &varOSProductSuite, 0, VT_UINT);
  131. if (SUCCEEDED(hr))
  132. {
  133. g_CIMOSProductSuite = varOSProductSuite.uintVal;
  134. }
  135. }
  136. }
  137. if (FAILED(hr))
  138. {
  139. g_CIMSucceeded = FALSE;
  140. break;
  141. }
  142. hr = pNSClass->Get(L"BuildNumber", NULL, &varOSBuildNumber, &ctpeType, NULL);
  143. if (SUCCEEDED(hr))
  144. {
  145. hr = VariantChangeType(&varOSBuildNumber, &varOSBuildNumber, 0, VT_BSTR);
  146. if (SUCCEEDED(hr))
  147. {
  148. wcscpy(g_CIMOSBuildNumber, varOSBuildNumber.bstrVal);
  149. }
  150. }
  151. if (FAILED(hr))
  152. {
  153. g_CIMSucceeded = FALSE;
  154. break;
  155. }
  156. hr = pNSClass->Get(L"ServicePackMajorVersion", NULL, &varServicePackMajorVersion, &ctpeType, NULL);
  157. if (SUCCEEDED(hr))
  158. {
  159. hr = VariantChangeType(&varServicePackMajorVersion, &varServicePackMajorVersion, 0, VT_BSTR);
  160. if (SUCCEEDED(hr))
  161. {
  162. wcscpy(g_CIMServicePackMajorVersion, varServicePackMajorVersion.bstrVal);
  163. }
  164. }
  165. if (FAILED(hr))
  166. {
  167. g_CIMSucceeded = FALSE;
  168. break;
  169. }
  170. hr = pNSClass->Get(L"ServicePackMinorVersion", NULL, &varServicePackMinorVersion, &ctpeType, NULL);
  171. if (SUCCEEDED(hr))
  172. {
  173. hr = VariantChangeType(&varServicePackMinorVersion, &varServicePackMinorVersion, 0, VT_BSTR);
  174. if (SUCCEEDED(hr))
  175. {
  176. wcscpy(g_CIMServicePackMinorVersion, varServicePackMinorVersion.bstrVal);
  177. }
  178. }
  179. if (FAILED(hr))
  180. {
  181. g_CIMSucceeded = FALSE;
  182. break;
  183. }
  184. }
  185. while (FALSE);
  186. }
  187. else
  188. {
  189. hr = E_UNEXPECTED;
  190. }
  191. pNSClass->Release();
  192. }
  193. pEnum->Release();
  194. }
  195. SysFreeString(bstrPath);
  196. if (SUCCEEDED(hr))
  197. {
  198. bstrPath = SysAllocString(L"select * from Win32_Processor");
  199. hr = pIWbemServices->ExecQuery(bstrWQL, bstrPath, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
  200. if (SUCCEEDED(hr))
  201. {
  202. IWbemClassObject *pNSClass;
  203. ULONG uReturned;
  204. hr = pEnum->Next(WBEM_INFINITE, 1, &pNSClass, &uReturned );
  205. if (SUCCEEDED(hr))
  206. {
  207. if (uReturned)
  208. {
  209. CIMTYPE ctpeType;
  210. hr = pNSClass->Get(L"Architecture", NULL, &varArchitecture, &ctpeType, NULL);
  211. if (SUCCEEDED(hr))
  212. {
  213. VariantChangeType(&varArchitecture, &varArchitecture, 0, VT_UINT);
  214. g_CIMProcessorArchitecture = varArchitecture.uintVal;
  215. }
  216. else
  217. {
  218. g_CIMSucceeded = FALSE;
  219. }
  220. }
  221. else
  222. {
  223. hr = E_UNEXPECTED;
  224. }
  225. pNSClass->Release();
  226. }
  227. pEnum->Release();
  228. }
  229. SysFreeString(bstrPath);
  230. }
  231. SysFreeString(bstrWQL);
  232. pIWbemServices->Release();
  233. } //hr = CoSetProxyBlanket(pIWbemServices.., if (SUCCEEDED(hr))
  234. } //hr = pIWbemLocator->ConnectServer.., if (SUCCEEDED(hr))
  235. SysFreeString(pNamespace);
  236. CoUninitialize();
  237. // Translate any WMI errors into Win32 errors:
  238. switch (hr)
  239. {
  240. case WBEM_E_NOT_FOUND:
  241. hr = HRESULT_FROM_WIN32(ERROR_HOST_UNREACHABLE);
  242. break;
  243. case WBEM_E_ACCESS_DENIED:
  244. hr = E_ACCESSDENIED;
  245. break;
  246. case WBEM_E_PROVIDER_FAILURE:
  247. hr = E_FAIL;
  248. break;
  249. case WBEM_E_TYPE_MISMATCH:
  250. case WBEM_E_INVALID_CONTEXT:
  251. case WBEM_E_INVALID_PARAMETER:
  252. hr = E_INVALIDARG;
  253. break;
  254. case WBEM_E_OUT_OF_MEMORY:
  255. hr = E_OUTOFMEMORY;
  256. break;
  257. }
  258. if ( (hr == S_OK) && (!g_CIMSucceeded) )
  259. {
  260. return E_FAIL;
  261. }
  262. else
  263. {
  264. return hr;
  265. }
  266. }