/xbmc/win32/WMIInterface.cpp

http://github.com/xbmc/xbmc · C++ · 242 lines · 175 code · 39 blank · 28 comment · 26 complexity · 380c33729eef86f8029278e2b69eed1d MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "stdafx.h"
  21. #include "WMIInterface.h"
  22. #include "../Util.h"
  23. using namespace std;
  24. CWIN32Wmi::CWIN32Wmi(void)
  25. {
  26. bconnected = false;
  27. pclsObj = NULL;
  28. Connect();
  29. }
  30. CWIN32Wmi::~CWIN32Wmi(void)
  31. {
  32. Release();
  33. }
  34. bool CWIN32Wmi::Connect()
  35. {
  36. // Initialize COM. ------------------------------------------
  37. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  38. if (FAILED(hres))
  39. {
  40. return false; // Program has failed.
  41. }
  42. hres = CoInitializeSecurity(
  43. NULL,
  44. -1, // COM authentication
  45. NULL, // Authentication services
  46. NULL, // Reserved
  47. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  48. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  49. NULL, // Authentication info
  50. EOAC_NONE, // Additional capabilities
  51. NULL // Reserved
  52. );
  53. if (FAILED(hres))
  54. {
  55. return false; // Program has failed.
  56. }
  57. pLoc = NULL;
  58. hres = CoCreateInstance(
  59. CLSID_WbemLocator,
  60. 0,
  61. CLSCTX_INPROC_SERVER,
  62. IID_IWbemLocator, (LPVOID *) &pLoc);
  63. if (FAILED(hres))
  64. {
  65. return false; // Program has failed.
  66. }
  67. pSvc = NULL;
  68. // Connect to the root\cimv2 namespace with
  69. // the current user and obtain pointer pSvc
  70. // to make IWbemServices calls.
  71. hres = pLoc->ConnectServer(
  72. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  73. NULL, // User name. NULL = current user
  74. NULL, // User password. NULL = current
  75. 0, // Locale. NULL indicates current
  76. NULL, // Security flags.
  77. 0, // Authority (e.g. Kerberos)
  78. 0, // Context object
  79. &pSvc // pointer to IWbemServices proxy
  80. );
  81. if (FAILED(hres))
  82. {
  83. pLoc->Release();
  84. CoUninitialize();
  85. return false; // Program has failed.
  86. }
  87. hres = CoSetProxyBlanket(
  88. pSvc, // Indicates the proxy to set
  89. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  90. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  91. NULL, // Server principal name
  92. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  93. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  94. NULL, // client identity
  95. EOAC_NONE // proxy capabilities
  96. );
  97. if (FAILED(hres))
  98. {
  99. pSvc->Release();
  100. pLoc->Release();
  101. CoUninitialize();
  102. return false; // Program has failed.
  103. }
  104. pEnumerator = NULL;
  105. bconnected = true;
  106. return true;
  107. }
  108. void CWIN32Wmi::Release()
  109. {
  110. if(pSvc != NULL)
  111. pSvc->Release();
  112. if(pLoc != NULL)
  113. pLoc->Release();
  114. if(pEnumerator != NULL)
  115. pEnumerator->Release();
  116. if(pclsObj != NULL)
  117. pclsObj->Release();
  118. CoUninitialize();
  119. bconnected = false;
  120. pSvc = NULL;
  121. pLoc = NULL;
  122. pEnumerator = NULL;
  123. pclsObj = NULL;
  124. }
  125. void CWIN32Wmi::testquery()
  126. {
  127. hres = pSvc->ExecQuery(
  128. bstr_t("WQL"),
  129. //bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE"),
  130. //bstr_t("SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter=TRUE"),
  131. bstr_t("SELECT * FROM Win32_NetworkAdapter WHERE Description='Atheros AR5008X Wireless Network Adapter'"),
  132. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  133. NULL,
  134. &pEnumerator);
  135. if (FAILED(hres))
  136. {
  137. return; // Program has failed.
  138. }
  139. ULONG uReturn = 0;
  140. while (pEnumerator)
  141. {
  142. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  143. &pclsObj, &uReturn);
  144. if(0 == uReturn)
  145. {
  146. break;
  147. }
  148. VARIANT vtProp;
  149. VariantInit(&vtProp);
  150. // Get the value of the Name property
  151. //hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
  152. vtProp.bstrVal = bstr_t("192.168.1.209");
  153. hr = pclsObj->Put(L"IPAddress",0,&vtProp,0);
  154. VariantClear(&vtProp);
  155. //iCpu++;
  156. }
  157. pclsObj->Release();
  158. pclsObj = NULL;
  159. }
  160. std::vector<CStdString> CWIN32Wmi::GetWMIStrVector(CStdString& strQuery, CStdStringW& strProperty)
  161. {
  162. std::vector<CStdString> strResult;
  163. pEnumerator = NULL;
  164. pclsObj = NULL;
  165. if(!bconnected)
  166. return strResult;
  167. hres = pSvc->ExecQuery(
  168. bstr_t("WQL"),
  169. bstr_t(strQuery.c_str()),
  170. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  171. NULL,
  172. &pEnumerator);
  173. if (FAILED(hres))
  174. {
  175. return strResult; // Program has failed.
  176. }
  177. ULONG uReturn = 0;
  178. while (pEnumerator)
  179. {
  180. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  181. &pclsObj, &uReturn);
  182. if(0 == uReturn)
  183. {
  184. break;
  185. }
  186. VARIANT vtProp;
  187. VariantInit(&vtProp);
  188. hr = pclsObj->Get(strProperty.c_str(), 0, &vtProp, 0, 0);
  189. strResult.push_back(vtProp.bstrVal);
  190. VariantClear(&vtProp);
  191. }
  192. if(pEnumerator != NULL)
  193. pEnumerator->Release();
  194. pEnumerator = NULL;
  195. if(pclsObj != NULL)
  196. pclsObj->Release();
  197. pclsObj = NULL;
  198. return strResult;
  199. }
  200. CStdString CWIN32Wmi::GetWMIString(CStdString& strQuery, CStdStringW& strProperty)
  201. {
  202. return GetWMIStrVector(strQuery, strProperty)[0];
  203. }