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.

2060 lines
50 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // SshWbemHelpers.cpp
  3. //
  4. //
  5. //
  6. //
  7. // History:
  8. //
  9. //
  10. //
  11. // Copyright (c) 1997-1999 Microsoft Corporation
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include "precomp.h"
  14. #include <stdio.h>
  15. #include <cominit.h>
  16. #ifdef EXT_DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. #include "SshWbemHelpers.h"
  21. #define REFCOUNT(obj) obj->AddRef()?obj->Release():0xFFFFFFFF
  22. //-----------------------------------------------------------------------------
  23. IErrorInfo * CWbemException::GetErrorObject()
  24. {
  25. IErrorInfo * pEI = NULL;
  26. GetErrorInfo(0, &pEI);
  27. return pEI;
  28. }
  29. //-----------------------------------------------------------------------------
  30. IErrorInfo * CWbemException::MakeErrorObject(_bstr_t sDescription)
  31. {
  32. ICreateErrorInfoPtr pcerrinfo;
  33. HRESULT hr;
  34. hr = CreateErrorInfo(&pcerrinfo);
  35. if(SUCCEEDED(hr))
  36. {
  37. pcerrinfo->SetDescription(sDescription);
  38. pcerrinfo->SetSource(_bstr_t("CWbemException"));
  39. IErrorInfoPtr perrinfo;
  40. hr = pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &perrinfo);
  41. if(SUCCEEDED(hr))
  42. {
  43. SetErrorInfo(0, perrinfo);
  44. }
  45. }
  46. return GetErrorObject();
  47. }
  48. //-----------------------------------------------------------------------------
  49. void CWbemException::GetWbemStatusObject()
  50. {
  51. m_pWbemError = new CWbemClassObject();
  52. if(m_pWbemError)
  53. {
  54. IErrorInfoPtr pEI = ErrorInfo();
  55. if(pEI)
  56. {
  57. pEI->QueryInterface(IID_IWbemClassObject, (void**)&(*m_pWbemError));
  58. }
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. CWbemException::CWbemException(HRESULT hr,_bstr_t sDescription) :
  63. _com_error(hr,GetErrorObject()),
  64. m_sDescription(sDescription),
  65. m_hr(hr)
  66. {
  67. GetWbemStatusObject();
  68. }
  69. //-----------------------------------------------------------------------------
  70. CWbemException::CWbemException(_bstr_t sDescription) :
  71. _com_error(WBEM_E_FAILED,MakeErrorObject(sDescription)),
  72. m_sDescription(sDescription)
  73. {
  74. GetWbemStatusObject();
  75. }
  76. //-----------------------------------------------------------------------------
  77. CWbemClassObject CWbemException::GetWbemError()
  78. {
  79. return *m_pWbemError;
  80. }
  81. //-----------------------------------------------------------------------------
  82. CWbemServices::CWbemServices(IWbemContext * _pContext)
  83. {
  84. m_pCtx = _pContext;
  85. m_authIdent = 0;
  86. m_pService = 0;
  87. m_cloak = 0;
  88. }
  89. //-----------------------------------------------------------------------------
  90. CWbemServices::CWbemServices(const CWbemServices& _p, COAUTHIDENTITY *authIdent)
  91. {
  92. m_authIdent = authIdent;
  93. m_pService = 0;
  94. IWbemServicesPtr pServices;
  95. m_cloak = 0;
  96. m_hr = S_OK;
  97. if(SUCCEEDED(const_cast<CWbemServices&>(_p).GetInterfacePtr(pServices)))
  98. {
  99. m_hr = CommonInit(pServices);
  100. }
  101. m_pCtx = _p.m_pCtx;
  102. }
  103. //-----------------------------------------------------------------------------
  104. CWbemServices::CWbemServices(const IWbemServicesPtr& _in)
  105. {
  106. m_authIdent = 0;
  107. m_hr = S_OK;
  108. m_pService = 0;
  109. m_cloak = 0;
  110. m_hr = CommonInit(const_cast<IWbemServicesPtr&>(_in));
  111. }
  112. //-----------------------------------------------------------------------------
  113. CWbemServices::CWbemServices(IWbemServices *_in,IWbemContext * _pContext)
  114. {
  115. m_pCtx = _pContext;
  116. m_authIdent = 0;
  117. m_hr = S_OK;
  118. m_pService = 0;
  119. m_cloak = 0;
  120. IWbemServicesPtr pServices = _in;
  121. m_hr = CommonInit(pServices);
  122. }
  123. //-----------------------------------------------------------------------------
  124. CWbemServices::CWbemServices(const IUnknownPtr& _in)
  125. {
  126. IWbemServicesPtr pServices = _in;
  127. m_authIdent = 0;
  128. m_hr = S_OK;
  129. m_pService = 0;
  130. m_cloak = 0;
  131. m_hr = CommonInit(pServices);
  132. }
  133. //-----------------------------------------------------------------------------
  134. CWbemServices::CWbemServices(IUnknown * _in)
  135. {
  136. IWbemServicesPtr pServices = _in;
  137. m_authIdent = 0;
  138. m_hr = S_OK;
  139. m_pService = 0;
  140. m_cloak = 0;
  141. m_hr = CommonInit(pServices);
  142. }
  143. //-----------------------------------------------------------------------------
  144. CWbemServices::~CWbemServices()
  145. {
  146. }
  147. //-----------------------------------------------------------------------------
  148. CWbemServices& CWbemServices::operator=(IWbemServices *_p)
  149. {
  150. m_pCtx = NULL;
  151. IWbemServicesPtr pServices = _p;
  152. m_hr = CommonInit(pServices);
  153. return *this;
  154. }
  155. //-----------------------------------------------------------------------------
  156. CWbemServices& CWbemServices::operator=(IUnknown * _p)
  157. {
  158. IWbemServicesPtr pServices = _p;
  159. m_pCtx = NULL;
  160. m_hr = CommonInit(pServices);
  161. return *this;
  162. }
  163. //-----------------------------------------------------------------------------
  164. CWbemServices& CWbemServices::operator=(IUnknownPtr& _p)
  165. {
  166. IWbemServicesPtr pServices = _p;
  167. m_pCtx = NULL;
  168. m_hr = CommonInit(pServices);
  169. return *this;
  170. }
  171. //-----------------------------------------------------------------------------
  172. CWbemServices& CWbemServices::operator=(const CWbemServices& _p)
  173. {
  174. IWbemServicesPtr pServices;
  175. if(SUCCEEDED(const_cast<CWbemServices&>(_p).GetInterfacePtr(pServices)))
  176. {
  177. m_hr = CommonInit(pServices);
  178. }
  179. m_pCtx = _p.m_pCtx;
  180. return *this;
  181. }
  182. //-----------------------------------------------------------------------------
  183. HRESULT CWbemServices::GetInterfacePtr(
  184. IWbemServicesPtr& pServices,
  185. DWORD dwProxyCapabilities) // = EOAC_NONE
  186. {
  187. pServices = m_pService;
  188. SetBlanket(pServices, dwProxyCapabilities);
  189. return S_OK;
  190. }
  191. //-----------------------------------------------------------------------------
  192. HRESULT CWbemServices::CommonInit(IWbemServicesPtr& pServ)
  193. {
  194. HRESULT hr = E_FAIL;
  195. if(pServ)
  196. {
  197. m_pService = pServ;
  198. hr = SetBlanket((IWbemServices *)m_pService);
  199. }
  200. return hr;
  201. }
  202. //---------------------------------------------------------------------
  203. bool CWbemServices::IsClientNT5OrMore(void)
  204. {
  205. OSVERSIONINFO os;
  206. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  207. if(!GetVersionEx(&os))
  208. return FALSE; // should never happen
  209. return ( os.dwPlatformId == VER_PLATFORM_WIN32_NT ) && ( os.dwMajorVersion >= 5 ) ;
  210. }
  211. //---------------------------------------------------------------
  212. void CWbemServices::SetPriv(LPCTSTR privName /* = SE_SYSTEM_ENVIRONMENT_NAME */)
  213. {
  214. ImpersonateSelf(SecurityImpersonation);
  215. if(OpenThreadToken( GetCurrentThread(),
  216. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  217. FALSE, &m_hAccessToken ) )
  218. {
  219. m_fClearToken = true;
  220. // Now, get the LUID for the privilege from the local system
  221. ZeroMemory(&m_luid, sizeof(m_luid));
  222. LookupPrivilegeValue(NULL, privName, &m_luid);
  223. m_cloak = true;
  224. EnablePriv(true);
  225. }
  226. else
  227. {
  228. DWORD err = GetLastError();
  229. }
  230. }
  231. //---------------------------------------------------------------
  232. DWORD CWbemServices::EnablePriv(bool fEnable)
  233. {
  234. DWORD dwError = ERROR_SUCCESS;
  235. TOKEN_PRIVILEGES tokenPrivileges;
  236. tokenPrivileges.PrivilegeCount = 1;
  237. tokenPrivileges.Privileges[0].Luid = m_luid;
  238. tokenPrivileges.Privileges[0].Attributes = ( fEnable ? SE_PRIVILEGE_ENABLED : 0 );
  239. if(AdjustTokenPrivileges(m_hAccessToken,
  240. FALSE,
  241. &tokenPrivileges,
  242. 0, NULL, NULL) != 0)
  243. {
  244. IWbemServices *service = NULL;
  245. GetServices(&service);
  246. //
  247. // Enable cloaking so the thread token is available to the
  248. // provider for the priv check. Reciprocate for disable.
  249. //
  250. SetBlanket(service, fEnable ? EOAC_STATIC_CLOAKING : EOAC_NONE);
  251. }
  252. else
  253. {
  254. dwError = ::GetLastError();
  255. }
  256. return dwError;
  257. }
  258. //---------------------------------------------------------------
  259. void CWbemServices::ClearPriv(void)
  260. {
  261. m_cloak = false;
  262. EnablePriv(false);
  263. if(m_fClearToken)
  264. {
  265. CloseHandle(m_hAccessToken);
  266. m_hAccessToken = 0;
  267. m_fClearToken = false;
  268. }
  269. RevertToSelf(); //to make sure impersonation token is removed from thread (counterpart to ImpersonateSelf() in SetPriv())
  270. }
  271. //---------------------------------------------------------------------
  272. HRESULT CWbemServices::SetBlanket(IUnknown *service,
  273. DWORD dwProxyCapabilities) // = EOAC_NONE
  274. {
  275. HRESULT hr = E_FAIL;
  276. if(service)
  277. {
  278. //
  279. // Sigh, maintain this hack for RC1.
  280. //
  281. DWORD eoac = EOAC_NONE;
  282. if(IsClientNT5OrMore() && m_cloak)
  283. {
  284. eoac = EOAC_STATIC_CLOAKING;
  285. }
  286. try
  287. {
  288. RPC_AUTH_IDENTITY_HANDLE AuthInfo = NULL;
  289. DWORD dwAuthnSvc = 0;
  290. DWORD dwAuthnLvl = 0;
  291. hr = CoQueryProxyBlanket(
  292. service,
  293. &dwAuthnSvc,
  294. NULL,
  295. NULL,
  296. &dwAuthnLvl,
  297. NULL,
  298. &AuthInfo,
  299. NULL);
  300. if (SUCCEEDED(hr))
  301. {
  302. hr = SetInterfaceSecurityEx(service,
  303. m_authIdent,
  304. NULL,
  305. dwAuthnLvl,
  306. RPC_C_IMP_LEVEL_IMPERSONATE,
  307. (eoac != EOAC_NONE) ? eoac :
  308. dwProxyCapabilities);
  309. }
  310. }
  311. catch( ... )
  312. {
  313. hr = E_FAIL;
  314. }
  315. }
  316. return hr;
  317. }
  318. //-----------------------------------------------------------------------------
  319. void CWbemServices::DisconnectServer(void)
  320. {
  321. m_pCtx = NULL;
  322. m_pService = NULL;
  323. }
  324. //-----------------------------------------------------------------------------
  325. CWbemClassObject CWbemServices::CreateInstance(_bstr_t _sClass,
  326. IWbemCallResultPtr& _cr)
  327. {
  328. CWbemClassObject coClassDef = GetObject(_sClass,_cr);
  329. CWbemClassObject coRet;
  330. if(!coClassDef.IsNull())
  331. {
  332. coRet = coClassDef.SpawnInstance();
  333. }
  334. return coRet;
  335. }
  336. //-----------------------------------------------------------------------------
  337. CWbemClassObject CWbemServices::CreateInstance(_bstr_t _sClass)
  338. {
  339. IWbemCallResultPtr crUnused;
  340. return CreateInstance(_sClass,crUnused);
  341. }
  342. //-----------------------------------------------------------------------------
  343. HRESULT CWbemServices::DeleteInstance(_bstr_t _sClass)
  344. {
  345. HRESULT hr;
  346. IWbemCallResultPtr pResult;
  347. hr = m_pService->DeleteInstance(_sClass, WBEM_FLAG_RETURN_IMMEDIATELY,
  348. NULL, &pResult);
  349. if (SUCCEEDED(hr)) // Useless probably, but eases my paranoia.
  350. {
  351. hr = this->SetBlanket(pResult);
  352. if (SUCCEEDED(hr))
  353. {
  354. HRESULT hrTemp;
  355. hr = pResult->GetCallStatus(WBEM_INFINITE, &hrTemp);
  356. if (SUCCEEDED(hr))
  357. {
  358. hr = hrTemp;
  359. }
  360. }
  361. }
  362. return hr;
  363. }
  364. //-----------------------------------------------------------------------------
  365. HRESULT CWbemServices::GetMethodSignatures(const _bstr_t& _sObjectName,
  366. const _bstr_t& _sMethodName,
  367. CWbemClassObject& _in,
  368. CWbemClassObject& _out)
  369. {
  370. CWbemClassObject methodClass = GetObject(_sObjectName);
  371. if(methodClass)
  372. {
  373. m_hr = methodClass.GetMethod(_sMethodName,_in,_out);
  374. }
  375. return m_hr;
  376. }
  377. //-----------------------------------------------------------------------------
  378. HRESULT CWbemServices::PutInstance(CWbemClassObject& _object,
  379. IWbemCallResultPtr& _cr,
  380. long _lFlags /*= WBEM_FLAG_CREATE_OR_UPDATE*/)
  381. {
  382. HRESULT hr = E_FAIL;
  383. IWbemServicesPtr pServices;
  384. GetInterfacePtr(pServices);
  385. #ifdef NOT_NECESSARY
  386. if(m_pCtx == NULL)
  387. {
  388. IWbemContext *pContext = NULL;
  389. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  390. if(FAILED(hr))
  391. {
  392. // ::MessageBox(NULL,_T("Cannot CoCreateInstance"),_T("Error"),MB_OK);
  393. return hr;
  394. }
  395. m_pCtx = pContext;
  396. }
  397. hr = pServices->PutInstance(_object,_lFlags,m_pCtx,&_cr);
  398. #endif // NOT_NECESSARY
  399. hr = pServices->PutInstance(_object,_lFlags,NULL,&_cr);
  400. return hr;
  401. }
  402. //---------------- VINOTH ------------------------------------------------------
  403. HRESULT CWbemServices::PutInstance(
  404. CWbemClassObject& _object,
  405. IWbemContext *pContext,
  406. long _lFlags, // = WBEM_FLAG_CREATE_OR_UPDATE
  407. DWORD _dwProxyCapabilities) // = EOAC_NONE
  408. {
  409. HRESULT hr = E_FAIL;
  410. IWbemServicesPtr pServices;
  411. GetInterfacePtr(pServices, _dwProxyCapabilities);
  412. #ifdef NOT_NECESSARY
  413. IWbemCallResultPtr crUnused;
  414. hr = pServices->PutInstance(_object,_lFlags,pContext,&crUnused);
  415. #endif // NOT_NECESSARY
  416. hr = pServices->PutInstance(_object,_lFlags,pContext,NULL);
  417. return hr;
  418. }
  419. //-----------------------------------------------------------------------------
  420. HRESULT CWbemServices::PutInstance(
  421. CWbemClassObject& _object,
  422. long _lFlags, // = WBEM_FLAG_CREATE_OR_UPDATE
  423. DWORD dwProxyCapabilities) // = EOAC_NONE
  424. {
  425. #ifdef NOT_NECESSARY
  426. IWbemCallResultPtr crUnused;
  427. return PutInstance(_object,crUnused,_lFlags);
  428. #endif // NOT_NECESSARY
  429. return PutInstance(_object,NULL,_lFlags,dwProxyCapabilities);
  430. }
  431. //-----------------------------------------------------------------------------
  432. bool CWbemServices::IsNull()
  433. {
  434. bool bRet = m_pService == NULL;
  435. return bRet;
  436. }
  437. //-----------------------------------------------------------------------------
  438. CWbemServices::operator bool()
  439. {
  440. bool bRet = m_pService != NULL;
  441. return bRet;
  442. }
  443. //-----------------------------------------------------------------------------
  444. HRESULT CWbemServices::CreateInstanceEnum(_bstr_t Class,
  445. long lFlags,
  446. IEnumWbemClassObject **ppEnum)
  447. {
  448. HRESULT hr = E_FAIL;
  449. if(m_pService)
  450. {
  451. SetBlanket(m_pService);
  452. if(m_pCtx == NULL)
  453. {
  454. IWbemContext *pContext = NULL;
  455. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  456. if(FAILED(hr))
  457. {
  458. return hr;
  459. }
  460. m_pCtx = pContext;
  461. }
  462. hr = m_pService->CreateInstanceEnum(Class, lFlags, m_pCtx, ppEnum);
  463. SetBlanket(*ppEnum);
  464. }
  465. return hr;
  466. }
  467. //-----------------------------------------------------------------------------
  468. HRESULT CWbemServices::CreateInstanceEnumAsync(_bstr_t Class,
  469. IWbemObjectSink *ppSink,
  470. long lFlags /*= 0*/)
  471. {
  472. HRESULT hr = E_FAIL;
  473. if(m_pService)
  474. {
  475. if(m_pCtx == NULL)
  476. {
  477. IWbemContext *pContext = NULL;
  478. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  479. if(FAILED(hr))
  480. {
  481. return hr;
  482. }
  483. m_pCtx = pContext;
  484. }
  485. SetBlanket(m_pService);
  486. hr = m_pService->CreateInstanceEnumAsync(Class, lFlags, m_pCtx, ppSink);
  487. }
  488. return hr;
  489. }
  490. //-----------------------------------------------------------------------------
  491. HRESULT CWbemServices::CreateClassEnum(_bstr_t Class,
  492. long lFlags,
  493. IEnumWbemClassObject **ppEnum)
  494. {
  495. HRESULT hr = E_FAIL;
  496. if(m_pService)
  497. {
  498. SetBlanket(m_pService);
  499. if(m_pCtx == NULL)
  500. {
  501. IWbemContext *pContext = NULL;
  502. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  503. if(FAILED(hr))
  504. {
  505. return hr;
  506. }
  507. m_pCtx = pContext;
  508. }
  509. hr = m_pService->CreateClassEnum(Class, lFlags, m_pCtx, ppEnum);
  510. SetBlanket(*ppEnum);
  511. }
  512. return hr;
  513. }
  514. //-----------------------------------------------------------------------------
  515. HRESULT CWbemServices::CreateClassEnumAsync(_bstr_t Class,
  516. IWbemObjectSink *ppSink,
  517. long lFlags /*= 0*/)
  518. {
  519. HRESULT hr = E_FAIL;
  520. if(m_pService)
  521. {
  522. SetBlanket(m_pService);
  523. if(m_pCtx == NULL)
  524. {
  525. IWbemContext *pContext = NULL;
  526. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  527. if(FAILED(hr))
  528. {
  529. return hr;
  530. }
  531. m_pCtx = pContext;
  532. }
  533. hr = m_pService->CreateClassEnumAsync(Class, lFlags, m_pCtx, ppSink);
  534. }
  535. return hr;
  536. }
  537. //-----------------------------------------------------------------------------
  538. HRESULT CWbemServices::ExecQuery(_bstr_t QueryLanguage,
  539. _bstr_t Query,
  540. long lFlags,
  541. IEnumWbemClassObject **ppEnum)
  542. {
  543. HRESULT hr = E_FAIL;
  544. if(m_pService)
  545. {
  546. if(m_pCtx == NULL)
  547. {
  548. IWbemContext *pContext = NULL;
  549. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  550. if(FAILED(hr))
  551. {
  552. return hr;
  553. }
  554. m_pCtx = pContext;
  555. }
  556. hr = m_pService->ExecQuery(QueryLanguage, Query,lFlags, m_pCtx, ppEnum);
  557. SetBlanket(*ppEnum);
  558. }
  559. return hr;
  560. }
  561. //-----------------------------------------------------------------------------
  562. HRESULT CWbemServices::ExecQuery(_bstr_t Query,
  563. long lFlags,
  564. IEnumWbemClassObject **ppEnum)
  565. {
  566. HRESULT hr = E_FAIL;
  567. if(m_pService)
  568. {
  569. if(m_pCtx == NULL)
  570. {
  571. IWbemContext *pContext = NULL;
  572. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  573. if(FAILED(hr))
  574. {
  575. return hr;
  576. }
  577. m_pCtx = pContext;
  578. }
  579. hr = m_pService->ExecQuery(_bstr_t("WQL"), Query,lFlags, m_pCtx, ppEnum);
  580. SetBlanket(*ppEnum);
  581. }
  582. return hr;
  583. }
  584. //-----------------------------------------------------------------------------
  585. HRESULT CWbemServices::ExecQuery(_bstr_t Query,
  586. IEnumWbemClassObject **ppEnum)
  587. {
  588. HRESULT hr = E_FAIL;
  589. if(m_pService)
  590. {
  591. if(m_pCtx == NULL)
  592. {
  593. IWbemContext *pContext = NULL;
  594. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  595. if(FAILED(hr))
  596. {
  597. return hr;
  598. }
  599. m_pCtx = pContext;
  600. }
  601. hr = m_pService->ExecQuery(_bstr_t("WQL"), Query,0, m_pCtx, ppEnum);
  602. SetBlanket(*ppEnum);
  603. }
  604. return hr;
  605. }
  606. //-----------------------------------------------------------------------------
  607. HRESULT CWbemServices::ExecQueryAsync(_bstr_t Query,
  608. IWbemObjectSink * pSink,
  609. long lFlags /*= 0*/)
  610. {
  611. HRESULT hr = E_FAIL;
  612. if(m_pService)
  613. {
  614. if(m_pCtx == NULL)
  615. {
  616. IWbemContext *pContext = NULL;
  617. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  618. if(FAILED(hr))
  619. {
  620. return hr;
  621. }
  622. m_pCtx = pContext;
  623. }
  624. hr = m_pService->ExecQueryAsync(_bstr_t("WQL"), Query,0, m_pCtx, pSink);
  625. }
  626. return hr;
  627. }
  628. //-----------------------------------------------------------------------------
  629. HRESULT CWbemServices::ExecMethod(_bstr_t sPath,
  630. _bstr_t sMethod,
  631. CWbemClassObject& inParams,
  632. CWbemClassObject& outParams)
  633. {
  634. IWbemCallResultPtr crUnused;
  635. HRESULT hr = E_FAIL;
  636. if(m_pService)
  637. {
  638. if(m_pCtx == NULL)
  639. {
  640. IWbemContext *pContext = NULL;
  641. hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  642. if(FAILED(hr))
  643. {
  644. return hr;
  645. }
  646. m_pCtx = pContext;
  647. }
  648. hr = m_pService->ExecMethod(sPath, sMethod,0, m_pCtx, inParams,&outParams,&crUnused);
  649. }
  650. return hr;
  651. }
  652. //-----------------------------------------------------------------------------
  653. HRESULT CWbemServices::CancelAsyncCall(IWbemObjectSink * pSink)
  654. {
  655. HRESULT hr = E_FAIL;
  656. if(m_pService)
  657. {
  658. hr = m_pService->CancelAsyncCall(pSink);
  659. }
  660. return hr;
  661. }
  662. //-----------------------------------------------------------------------------
  663. HRESULT CWbemServices::GetServices(IWbemServices ** ppServices)
  664. {
  665. IWbemServicesPtr pServices;
  666. GetInterfacePtr(pServices);
  667. *ppServices = pServices.Detach();
  668. return S_OK;
  669. }
  670. //-----------------------------------------------------------------------------
  671. HRESULT CWbemServices::ConnectServer(_bstr_t sNetworkResource)
  672. {
  673. IWbemLocatorPtr pLocator;
  674. HRESULT hr;
  675. // Get a pointer locator and use it to get a pointer to WbemServices!
  676. hr = pLocator.CreateInstance(CLSID_WbemLocator);
  677. if(SUCCEEDED(hr))
  678. {
  679. IWbemServicesPtr pServices = 0;
  680. hr = pLocator->ConnectServer(sNetworkResource, // Network
  681. NULL, // User
  682. NULL, // Password
  683. NULL, // Locale
  684. 0, // Security Flags
  685. NULL, // Authority
  686. NULL, // Context
  687. &pServices); // Namespace
  688. if(SUCCEEDED(hr))
  689. {
  690. hr = CommonInit(pServices);
  691. m_path = sNetworkResource;
  692. }
  693. }
  694. return hr;
  695. }
  696. //-----------------------------------------------------------
  697. CWbemServices CWbemServices::OpenNamespace(_bstr_t sNetworkResource)
  698. {
  699. CWbemServices coRet;
  700. IWbemServicesPtr pServices = NULL, pTemp = NULL;
  701. GetInterfacePtr(pServices);
  702. m_hr = S_OK;
  703. if(pServices)
  704. {
  705. try {
  706. m_hr = pServices->OpenNamespace(sNetworkResource,// Network
  707. 0, NULL, // Password
  708. &pTemp, NULL); // Namespace
  709. if(SUCCEEDED(m_hr))
  710. {
  711. coRet = (IWbemServices *)pTemp;
  712. coRet.m_authIdent = m_authIdent;
  713. coRet.SetBlanket(pTemp);
  714. coRet.m_path = sNetworkResource;
  715. }
  716. }
  717. catch( ... )
  718. {
  719. }
  720. }
  721. return coRet;
  722. }
  723. //-----------------------------------------------------------------------------
  724. HRESULT CWbemServices::ConnectServer(_bstr_t sNetworkResource,
  725. _bstr_t sUser,
  726. _bstr_t sPassword,
  727. long lSecurityFlags)
  728. {
  729. IWbemLocatorPtr pLocator;
  730. HRESULT hr = S_OK;
  731. // Get a pointer locator and use it to get a pointer to WbemServices!
  732. pLocator.CreateInstance(CLSID_WbemLocator);
  733. if(SUCCEEDED(hr))
  734. {
  735. IWbemServicesPtr pServices = 0;
  736. hr = pLocator->ConnectServer(sNetworkResource, // Network
  737. sUser, // User
  738. sPassword, // Password
  739. NULL, // Locale
  740. lSecurityFlags, // Security Flags
  741. NULL, // Authority
  742. NULL, // Context
  743. &pServices);
  744. if(SUCCEEDED(hr))
  745. {
  746. //Now Store the User,Passowrd and Security Flags for later use
  747. m_User = sUser;
  748. m_Password = sPassword;
  749. m_lFlags = lSecurityFlags;
  750. hr = CommonInit(pServices);
  751. m_path = sNetworkResource;
  752. }
  753. }
  754. return hr;
  755. }
  756. //-----------------------------------------------------------------------------
  757. HRESULT CWbemServices::ConnectServer(_bstr_t sNetworkResource,
  758. LOGIN_CREDENTIALS *user,
  759. long lSecurityFlags/* = 0*/)
  760. {
  761. HRESULT hr = S_OK;
  762. if((user == NULL) || user->currUser)
  763. {
  764. if(m_authIdent)
  765. {
  766. WbemFreeAuthIdentity(m_authIdent);
  767. m_authIdent = 0;
  768. }
  769. hr = ConnectServer(sNetworkResource);
  770. }
  771. else
  772. {
  773. IWbemLocatorPtr pLocator;
  774. m_authIdent = user->authIdent;
  775. // Get a pointer locator and use it to get a pointer to WbemServices!
  776. pLocator.CreateInstance(CLSID_WbemLocator);
  777. if(SUCCEEDED(hr))
  778. {
  779. IWbemServicesPtr pServices = 0;
  780. bstr_t usr(user->fullAcct), pwd;
  781. if(user->authIdent->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
  782. {
  783. WCHAR temp[100] = {0};
  784. mbstowcs(temp, (const char *)user->authIdent->Password, sizeof(temp)/sizeof(temp[0]));
  785. pwd = temp;
  786. }
  787. else
  788. {
  789. // unicode to unicode
  790. pwd = user->authIdent->Password;
  791. }
  792. hr = pLocator->ConnectServer(sNetworkResource, // Network
  793. (bstr_t)usr, // User
  794. (bstr_t)pwd, // Password
  795. NULL, // Locale
  796. lSecurityFlags, // Security Flags
  797. NULL, // Authority
  798. NULL, // Context
  799. &pServices);
  800. if (SUCCEEDED(hr))
  801. {
  802. // Now Store the User,Password and Security Flags for
  803. // later use
  804. m_User = usr;
  805. m_Password = pwd;
  806. m_lFlags = lSecurityFlags;
  807. hr = CommonInit(pServices);
  808. m_path = sNetworkResource;
  809. }
  810. }
  811. }
  812. return hr;
  813. }
  814. //-----------------------------------------------------------------------------
  815. CWbemClassObject CWbemServices::GetObject(_bstr_t _sName,
  816. IWbemCallResultPtr &_cr,
  817. long flags /*= 0*/)
  818. {
  819. CWbemClassObject coRet;
  820. m_hr = S_OK;
  821. IWbemServicesPtr pServices;
  822. GetInterfacePtr(pServices);
  823. if(m_pCtx == NULL)
  824. {
  825. IWbemContext *pContext = NULL;
  826. m_hr = CoCreateInstance(CLSID_WbemContext, NULL, CLSCTX_INPROC_SERVER, IID_IWbemContext, (void **)&pContext);
  827. if(FAILED(m_hr))
  828. {
  829. // ::MessageBox(NULL,_T("Cannot CoCreateInstance"),_T("Error"),MB_OK);
  830. return coRet;
  831. }
  832. m_pCtx = pContext;
  833. }
  834. if( pServices )
  835. {
  836. m_hr = pServices->GetObject(_sName, flags, m_pCtx,&coRet, &_cr);
  837. } else {
  838. //
  839. // Have no idea whether connected to Remote
  840. // otherwise the error should have been
  841. // RPC_E_DISCONNECTED
  842. //
  843. m_hr = E_POINTER;
  844. }
  845. return coRet;
  846. }
  847. //------------------------------------------------
  848. CWbemClassObject CWbemServices::GetObject(_bstr_t _sName, long flags /*= 0*/)
  849. {
  850. IWbemCallResultPtr crUnused;
  851. return GetObject(_sName,crUnused, flags);
  852. }
  853. //------------------------------------------------
  854. // get the first instance of the named class.
  855. IWbemClassObject *CWbemServices::FirstInstanceOf(bstr_t className)
  856. {
  857. IWbemClassObject *pInst = NULL;
  858. ULONG uReturned;
  859. IEnumWbemClassObject *Enum = NULL;
  860. m_hr = S_OK;
  861. // get the class.
  862. m_hr = CreateInstanceEnum(className, WBEM_FLAG_SHALLOW, &Enum);
  863. if(SUCCEEDED(m_hr))
  864. {
  865. // get the first and only instance.
  866. Enum->Next(-1, 1, &pInst, &uReturned);
  867. Enum->Release();
  868. }
  869. return pInst;
  870. }
  871. //-----------------------------------------------------------------------------
  872. HRESULT CWbemServices::SetContextValue(_bstr_t sName,_variant_t value)
  873. {
  874. HRESULT hr = S_OK;
  875. if(!bool(m_pCtx))
  876. {
  877. hr = m_pCtx.CreateInstance(CLSID_WbemContext);
  878. }
  879. if( SUCCEEDED(hr) )
  880. {
  881. hr = m_pCtx->SetValue(sName,0,&value);
  882. }
  883. return hr;
  884. }
  885. //-----------------------------------------------------------------------------
  886. HRESULT CWbemServices::SetContext(IWbemContext * pWbemContext)
  887. {
  888. HRESULT hr = S_OK;
  889. m_pCtx = pWbemContext;
  890. return hr;
  891. }
  892. //-----------------------------------------------------------------------------
  893. HRESULT CWbemServices::GetContextValue(_bstr_t sName,_variant_t& value)
  894. {
  895. HRESULT hr = S_OK;
  896. if(m_pCtx)
  897. {
  898. hr = m_pCtx->GetValue(sName,0,&value);
  899. }
  900. else
  901. {
  902. hr = E_FAIL;
  903. }
  904. return hr;
  905. }
  906. //-----------------------------------------------------------------------------
  907. HRESULT CWbemServices::DeleteContextValue(_bstr_t sName)
  908. {
  909. HRESULT hr = S_OK;
  910. if(m_pCtx)
  911. {
  912. hr = m_pCtx->DeleteValue(sName,0);
  913. }
  914. else
  915. {
  916. hr = E_FAIL;
  917. }
  918. return hr;
  919. }
  920. //-----------------------------------------------------------------------------
  921. HRESULT CWbemServices::DeleteAllContextValues()
  922. {
  923. HRESULT hr = S_OK;
  924. if(m_pCtx)
  925. {
  926. hr = m_pCtx->DeleteAll();
  927. }
  928. else
  929. {
  930. hr = E_FAIL;
  931. }
  932. return hr;
  933. }
  934. //-----------------------------------------------------------------------------
  935. HRESULT CWbemServices::GetContext(IWbemContext **ppWbemContext)
  936. {
  937. HRESULT hr = E_FAIL;
  938. if(m_pCtx)
  939. {
  940. m_pCtx->AddRef();
  941. *ppWbemContext = m_pCtx;
  942. hr = S_OK;
  943. }
  944. return hr;
  945. }
  946. //-----------------------------------------------------------------------------
  947. //-----------------------------------------------------------------------------
  948. DWORD g_ObjectCount = 0;
  949. #define TESTOBJ if(m_pWbemObject==0)return WBEM_E_NOT_AVAILABLE;
  950. //-----------------------------------------------------------------------------
  951. CWbemClassObject::CWbemClassObject(const CWbemClassObject& _in):
  952. m_pWbemObject(_in.m_pWbemObject)
  953. {
  954. ref = 0;
  955. g_ObjectCount++;
  956. }
  957. //-----------------------------------------------------------------------------
  958. CWbemClassObject::CWbemClassObject(IWbemClassObject * const _in):
  959. m_pWbemObject(_in)
  960. {
  961. ref = 0;
  962. g_ObjectCount++;
  963. }
  964. //-----------------------------------------------------------------------------
  965. CWbemClassObject::CWbemClassObject(IWbemClassObjectPtr& _in) :
  966. m_pWbemObject(_in)
  967. {
  968. ref = 0;
  969. g_ObjectCount++;
  970. }
  971. //-----------------------------------------------------------------------------
  972. CWbemClassObject::CWbemClassObject(IUnknown * _in):
  973. m_pWbemObject(_in)
  974. {
  975. ref = 0;
  976. g_ObjectCount++;
  977. }
  978. //-----------------------------------------------------------------------------
  979. CWbemClassObject::CWbemClassObject(IUnknownPtr& _in):
  980. m_pWbemObject(_in)
  981. {
  982. ref = 0;
  983. g_ObjectCount++;
  984. }
  985. //-----------------------------------------------------------------------------
  986. CWbemClassObject::CWbemClassObject():
  987. m_pWbemObject()
  988. {
  989. ref = 0;
  990. g_ObjectCount++;
  991. }
  992. //-----------------------------------------------------------------------------
  993. CWbemClassObject::~CWbemClassObject()
  994. {
  995. g_ObjectCount--;
  996. }
  997. //-----------------------------------------------------------------------------
  998. void CWbemClassObject::Attach(IWbemClassObject * pWbemObject)
  999. {
  1000. m_pWbemObject.Attach(pWbemObject);
  1001. }
  1002. //-----------------------------------------------------------------------------
  1003. void CWbemClassObject::Attach(IWbemClassObject * pWbemObject,bool bAddRef)
  1004. {
  1005. m_pWbemObject.Attach(pWbemObject,bAddRef);
  1006. }
  1007. //-----------------------------------------------------------------------------
  1008. IWbemClassObject *CWbemClassObject::operator->()
  1009. {
  1010. return m_pWbemObject;
  1011. }
  1012. //-----------------------------------------------------------------------------
  1013. _bstr_t CWbemClassObject::GetObjectText()
  1014. {
  1015. _bstr_t bRet;
  1016. BSTR bstr;
  1017. if( !FAILED(m_pWbemObject->GetObjectText(0,&bstr)) )
  1018. {
  1019. bRet = _bstr_t(bstr,false);
  1020. }
  1021. return bRet;
  1022. }
  1023. //-----------------------------------------------------------------------------
  1024. IWbemClassObject * CWbemClassObject::Detach()
  1025. {
  1026. return m_pWbemObject.Detach();
  1027. }
  1028. //-----------------------------------------------------------------------------
  1029. CWbemClassObject::operator IWbemClassObject*()
  1030. {
  1031. return m_pWbemObject;
  1032. }
  1033. //-----------------------------------------------------------------------------
  1034. CWbemClassObject::operator IWbemClassObject**()
  1035. {
  1036. return &m_pWbemObject;
  1037. }
  1038. //-----------------------------------------------------------------------------
  1039. CWbemClassObject::operator IWbemClassObjectPtr()
  1040. {
  1041. return m_pWbemObject;
  1042. }
  1043. //-----------------------------------------------------------------------------
  1044. CWbemClassObject::operator IUnknown *()
  1045. {
  1046. return (IUnknown *)(IWbemClassObject *)m_pWbemObject;
  1047. }
  1048. //-----------------------------------------------------------------------------
  1049. IWbemClassObject ** CWbemClassObject::operator &()
  1050. {
  1051. return &m_pWbemObject;
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. bool CWbemClassObject::operator!()
  1055. {
  1056. return m_pWbemObject == NULL;
  1057. }
  1058. //-----------------------------------------------------------------------------
  1059. CWbemClassObject::operator bool()
  1060. {
  1061. return m_pWbemObject != NULL;
  1062. }
  1063. //-----------------------------------------------------------------------------
  1064. ULONG CWbemClassObject::AddRef()
  1065. {
  1066. ref++;
  1067. return m_pWbemObject->AddRef();
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. ULONG CWbemClassObject::Release()
  1071. {
  1072. ref--;
  1073. return m_pWbemObject->Release();
  1074. }
  1075. //-----------------------------------------------------------------------------
  1076. IWbemClassObject* CWbemClassObject::operator=(IWbemClassObject* _p)
  1077. {
  1078. m_pWbemObject = _p;
  1079. return m_pWbemObject;
  1080. }
  1081. //-----------------------------------------------------------------------------
  1082. IWbemClassObjectPtr CWbemClassObject::operator=(IWbemClassObjectPtr& _p)
  1083. {
  1084. m_pWbemObject = _p;
  1085. return m_pWbemObject;
  1086. }
  1087. //-----------------------------------------------------------------------------
  1088. IWbemClassObject* CWbemClassObject::operator=(IUnknown * _p)
  1089. {
  1090. m_pWbemObject = _p;
  1091. return m_pWbemObject;
  1092. }
  1093. //-----------------------------------------------------------------------------
  1094. IWbemClassObjectPtr CWbemClassObject::operator=(IUnknownPtr& _p)
  1095. {
  1096. m_pWbemObject = _p;
  1097. return m_pWbemObject;
  1098. }
  1099. //-----------------------------------------------------------------------------
  1100. IWbemClassObject* CWbemClassObject::operator=(const CWbemClassObject& _p)
  1101. {
  1102. m_pWbemObject = _p.m_pWbemObject;
  1103. return m_pWbemObject;
  1104. }
  1105. //-----------------------------------------------------------------------------
  1106. bool CWbemClassObject::IsNull() const
  1107. {
  1108. return m_pWbemObject ==NULL;
  1109. }
  1110. //-----------------------------------------------------------------------------
  1111. HRESULT CWbemClassObject::Clone(CWbemClassObject& _newObject)
  1112. {
  1113. return m_pWbemObject->Clone(_newObject);
  1114. }
  1115. //-----------------------------------------------------------------------------
  1116. CWbemClassObject CWbemClassObject::SpawnInstance()
  1117. {
  1118. CWbemClassObject coRet;
  1119. HRESULT hr = m_pWbemObject->SpawnInstance(0,coRet);
  1120. return coRet;
  1121. }
  1122. //-----------------------------------------------------------------------------
  1123. HRESULT CWbemClassObject::GetMethod(const IN _bstr_t& _name,
  1124. CWbemClassObject& coInSignature,
  1125. CWbemClassObject& coOutSignature,
  1126. long _lFlags /*= 0*/)
  1127. {
  1128. TESTOBJ;
  1129. return m_pWbemObject->GetMethod(_name, _lFlags, coInSignature, coOutSignature);
  1130. }
  1131. //-----------------------------------------------------------------------------
  1132. bool CWbemClassObject::operator<(const CWbemClassObject& _comp)
  1133. {
  1134. return m_pWbemObject < _comp.m_pWbemObject;
  1135. }
  1136. //-----------------------------------------------------------------------------
  1137. // put overloads
  1138. HRESULT CWbemClassObject::Put(const _bstr_t& _Name,_variant_t _value,CIMTYPE vType /*= 0*/)
  1139. {
  1140. TESTOBJ;
  1141. return m_pWbemObject->Put(_Name,0,&_value,vType);
  1142. }
  1143. //-----------------------------------------------------------------------------
  1144. HRESULT CWbemClassObject::Put(const _bstr_t& _Name,const _bstr_t& _value,CIMTYPE vType /*= 0*/)
  1145. {
  1146. TESTOBJ;
  1147. return m_pWbemObject->Put(_Name,0,&_variant_t(_value),vType);
  1148. }
  1149. //-----------------------------------------------------------------------------
  1150. HRESULT CWbemClassObject::Put(const _bstr_t& _Name, const long _value, CIMTYPE vType /*= 0*/)
  1151. {
  1152. TESTOBJ;
  1153. return m_pWbemObject->Put(_Name,0,&_variant_t(_value), vType);
  1154. }
  1155. //-----------------------------------------------------------------------------
  1156. HRESULT CWbemClassObject::Put(const _bstr_t& _Name, const bool _value,CIMTYPE vType /*= 0*/)
  1157. {
  1158. TESTOBJ;
  1159. return m_pWbemObject->Put(_Name,0,&_variant_t(_value),vType);
  1160. }
  1161. //-----------------------------------------------------------------------------
  1162. HRESULT CWbemClassObject::Get(const _bstr_t& _Name, _bstr_t& _value)
  1163. {
  1164. _variant_t v1;
  1165. TESTOBJ;
  1166. HRESULT hr = m_pWbemObject->Get (_Name, 0, &v1, NULL, NULL);
  1167. _value = v1;
  1168. return hr;
  1169. }
  1170. //-----------------------------------------------------------------------------
  1171. HRESULT CWbemClassObject::Get(const _bstr_t& _Name, long& _value)
  1172. {
  1173. _variant_t v1;
  1174. TESTOBJ;
  1175. HRESULT hr = m_pWbemObject->Get (_Name, 0, &v1, NULL, NULL);
  1176. _value = v1;
  1177. return hr;
  1178. }
  1179. //-----------------------------------------------------------------------------
  1180. HRESULT CWbemClassObject::Get(const _bstr_t& _Name, bool& _value)
  1181. {
  1182. _variant_t v1;
  1183. TESTOBJ;
  1184. HRESULT hr = m_pWbemObject->Get (_Name, 0, &v1, NULL, NULL);
  1185. _value = v1;
  1186. return hr;
  1187. }
  1188. //-----------------------------------------------------------------------------
  1189. HRESULT CWbemClassObject::Get(const _bstr_t& _Name,_variant_t& _value)
  1190. {
  1191. TESTOBJ;
  1192. return m_pWbemObject->Get (_Name, 0, &_value, NULL, NULL);
  1193. }
  1194. //-----------------------------------------------------------------------------
  1195. _variant_t CWbemClassObject::Get(const _bstr_t& _Name,CIMTYPE& vType,long& lFlavor)
  1196. {
  1197. _variant_t vRet;
  1198. m_pWbemObject->Get(_Name, 0, &vRet, &vType, &lFlavor);
  1199. return vRet;
  1200. }
  1201. //-----------------------------------------------------------------------------
  1202. unsigned long CWbemClassObject::GetObjectSize()
  1203. {
  1204. unsigned long ulRet = 0;
  1205. IMarshalPtr pTest = (IUnknown*)m_pWbemObject;
  1206. if(pTest)
  1207. {
  1208. pTest->GetMarshalSizeMax(IID_IWbemClassObject,(IUnknown*)m_pWbemObject,MSHCTX_LOCAL,NULL,MSHLFLAGS_NORMAL,&ulRet);
  1209. }
  1210. return ulRet;
  1211. }
  1212. //-----------------------------------------------------------------------------
  1213. _bstr_t CWbemClassObject::GetString(const _bstr_t& _Name)
  1214. {
  1215. HRESULT hr;
  1216. _variant_t v1;
  1217. hr = Get(_Name, v1);
  1218. if(v1.vt == VT_NULL)
  1219. {
  1220. return "";
  1221. }
  1222. return v1;
  1223. }
  1224. //-----------------------------------------------------------------------------
  1225. _int64 CWbemClassObject::GetI64(const _bstr_t& _Name)
  1226. {
  1227. HRESULT hr;
  1228. _variant_t v1;
  1229. _bstr_t str;
  1230. hr = Get(_Name, v1);
  1231. if(v1.vt == VT_NULL)
  1232. {
  1233. return 0;
  1234. }
  1235. str = (_bstr_t) v1;
  1236. return _atoi64(str);
  1237. }
  1238. //-----------------------------------------------------------------------------
  1239. long CWbemClassObject::GetLong(const _bstr_t& _Name)
  1240. {
  1241. HRESULT hr;
  1242. _variant_t v1;
  1243. hr = Get(_Name,v1);
  1244. if(v1.vt == VT_NULL)
  1245. {
  1246. return 0;
  1247. }
  1248. return v1;
  1249. }
  1250. //-----------------------------------------------------------------------------
  1251. bool CWbemClassObject::GetBool(const _bstr_t& _Name)
  1252. {
  1253. HRESULT hr;
  1254. _variant_t v1;
  1255. hr = Get (_Name, v1);
  1256. if(v1.vt == VT_NULL)
  1257. {
  1258. return false;
  1259. }
  1260. return v1;
  1261. }
  1262. //-----------------------------------------------------------------------------
  1263. _bstr_t CWbemClassObject::GetCIMTYPE(const _bstr_t& _Name)
  1264. {
  1265. IWbemQualifierSetPtr pQualifierSet;
  1266. _bstr_t sRet;
  1267. if(m_pWbemObject->GetPropertyQualifierSet(_Name, &pQualifierSet) == S_OK)
  1268. {
  1269. _variant_t vt;
  1270. if(pQualifierSet->Get(_bstr_t("CIMTYPE"), 0, &vt, NULL) == S_OK)
  1271. {
  1272. sRet = vt;
  1273. }
  1274. }
  1275. return sRet;
  1276. }
  1277. //-----------------------------------------------------------------------------
  1278. CWbemClassObject CWbemClassObject::GetEmbeddedObject(const _bstr_t& _Name)
  1279. {
  1280. CWbemClassObject coRet;
  1281. HRESULT hr;
  1282. _variant_t v1;
  1283. hr = Get(_Name,v1);
  1284. if(hr == S_OK)
  1285. {
  1286. if(v1.vt == VT_UNKNOWN)
  1287. {
  1288. coRet = (IWbemClassObject*) v1.punkVal;
  1289. }
  1290. }
  1291. return coRet;
  1292. }
  1293. //-----------------------------------------------------------------------------
  1294. HRESULT CWbemClassObject::PutEmbeddedObject(const _bstr_t& _Name,
  1295. CWbemClassObject &obj)
  1296. {
  1297. HRESULT hr = S_OK;
  1298. IWbemClassObject *temp = obj;
  1299. LPUNKNOWN pUnk = 0;
  1300. if(temp)
  1301. {
  1302. hr = temp->QueryInterface(IID_IUnknown, (void**)&pUnk);
  1303. if(SUCCEEDED(hr))
  1304. {
  1305. _variant_t v1((IUnknown *)pUnk);
  1306. hr = Put(_Name, v1);
  1307. }
  1308. }
  1309. return hr;
  1310. }
  1311. //--------------------------------------------------------------------------
  1312. HRESULT CWbemClassObject::GetBLOB(const _bstr_t& _Name,
  1313. BYTE **ptr,
  1314. DWORD *outLen)
  1315. {
  1316. void* pVoid;
  1317. variant_t vValue;
  1318. SAFEARRAY* sa;
  1319. DWORD len = 0;
  1320. HRESULT hr = Get(_Name, vValue);
  1321. // if got a BYTE array back....
  1322. if(SUCCEEDED(hr) &&
  1323. (vValue.vt & VT_ARRAY) &&
  1324. (vValue.vt & VT_UI1))
  1325. {
  1326. // get it out.
  1327. sa = V_ARRAY(&vValue);
  1328. long lLowerBound = 0, lUpperBound = 0 ;
  1329. SafeArrayGetLBound(sa, 1, &lLowerBound);
  1330. SafeArrayGetUBound(sa, 1, &lUpperBound);
  1331. len = lUpperBound - lLowerBound + 1;
  1332. if(ptr)
  1333. {
  1334. // I want an null ptr ptr.
  1335. if(*ptr)
  1336. {
  1337. hr = E_FAIL;
  1338. }
  1339. else
  1340. {
  1341. // Get a pointer to read the data into.
  1342. *ptr = (LPBYTE)LocalAlloc(LPTR, len);
  1343. if(*ptr != NULL)
  1344. {
  1345. SafeArrayAccessData(sa, &pVoid);
  1346. memcpy(*ptr, pVoid, len);
  1347. SafeArrayUnaccessData(sa);
  1348. if(outLen)
  1349. *outLen = len;
  1350. hr = S_OK;
  1351. }
  1352. else
  1353. {
  1354. hr = E_FAIL;
  1355. }
  1356. }
  1357. }
  1358. }
  1359. return hr;
  1360. }
  1361. //--------------------------------------------------------------------------
  1362. HRESULT CWbemClassObject::PutBLOB(const _bstr_t& _Name,
  1363. const BYTE *ptr,
  1364. const DWORD len)
  1365. {
  1366. variant_t vValue;
  1367. HRESULT hr = E_FAIL;
  1368. void *pBuf = 0;
  1369. // got something to do?
  1370. if(ptr)
  1371. {
  1372. // set the UINT8 array for the BLOB.
  1373. SAFEARRAY* sa;
  1374. SAFEARRAYBOUND rgsabound[1];
  1375. rgsabound[0].cElements = len;
  1376. rgsabound[0].lLbound = 0;
  1377. sa = SafeArrayCreate(VT_UI1, 1, rgsabound);
  1378. // Get a pointer to read the data into
  1379. SafeArrayAccessData(sa, &pBuf);
  1380. memcpy(pBuf, ptr, rgsabound[0].cElements);
  1381. SafeArrayUnaccessData(sa);
  1382. // Put the safearray into a variant, and send it off
  1383. V_VT(&vValue) = VT_UI1 | VT_ARRAY;
  1384. V_ARRAY(&vValue) = sa;
  1385. hr = Put(_Name, vValue);
  1386. }
  1387. return hr;
  1388. }
  1389. //----------------------------------------------------------------------
  1390. HRESULT CWbemClassObject::GetDIB(const _bstr_t& _Name, HDC hDC,
  1391. HBITMAP &hDDBitmap)
  1392. {
  1393. //NOTE: THIS DOESN'T WORK YET.
  1394. variant_t blob;
  1395. HRESULT hr = Get(_Name, blob);
  1396. // if got a BYTE array back....
  1397. if(SUCCEEDED(hr) &&
  1398. (blob.vt & VT_ARRAY) &&
  1399. (blob.vt & VT_UI1))
  1400. {
  1401. BITMAPFILEHEADER *lpFile;
  1402. BYTE *blobData;
  1403. DWORD len = 0;
  1404. // get it out.
  1405. SAFEARRAY *sa = V_ARRAY(&blob);
  1406. // go right to the bytes.
  1407. SafeArrayAccessData(sa, (void **)&blobData);
  1408. // cast to FileHeader
  1409. lpFile = (BITMAPFILEHEADER *)blobData;
  1410. // is it a DIB?
  1411. if(lpFile->bfType == 0x4d42) /* 'BM' */
  1412. {
  1413. DWORD bfileSize = lpFile->bfOffBits;
  1414. BITMAPINFOHEADER *lpInfo;
  1415. // pt to the BITMAPINFO which immediately follows the BITMAPFILEHEADER.
  1416. lpInfo = (BITMAPINFOHEADER *)blobData + sizeof(BITMAPFILEHEADER);
  1417. // let this guy do the work.
  1418. hDDBitmap = CreateDIBitmap(hDC,
  1419. (LPBITMAPINFOHEADER)lpInfo,
  1420. CBM_INIT,
  1421. (LPSTR)lpInfo + lpInfo->biSize + PaletteSize(lpInfo),
  1422. (BITMAPINFO *)lpInfo,
  1423. DIB_RGB_COLORS);
  1424. }
  1425. else
  1426. {
  1427. hDDBitmap = 0;
  1428. hr = WBEM_E_TYPE_MISMATCH; // not a DIB.
  1429. }
  1430. SafeArrayUnaccessData(sa);
  1431. hr = S_OK;
  1432. }
  1433. return hr;
  1434. }
  1435. //-------------------------------------------------------------------
  1436. WORD CWbemClassObject::PaletteSize(LPBITMAPINFOHEADER lpbi)
  1437. {
  1438. WORD NumColors = DibNumColors(lpbi);
  1439. if(lpbi->biSize == sizeof(BITMAPCOREHEADER))
  1440. {
  1441. return (WORD)(NumColors * sizeof(RGBTRIPLE));
  1442. }
  1443. else
  1444. {
  1445. return (WORD)(NumColors * sizeof(RGBQUAD));
  1446. }
  1447. }
  1448. //-------------------------------------------------------------------
  1449. WORD CWbemClassObject::DibNumColors(LPBITMAPINFOHEADER lpbi)
  1450. {
  1451. int bits = 0;
  1452. LPBITMAPCOREHEADER lpbc = (LPBITMAPCOREHEADER)lpbi;
  1453. /* With the BITMAPINFO format headers, the size of the palette
  1454. * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
  1455. * is dependent on the bits per pixel ( = 2 raised to the power of
  1456. * bits/pixel).
  1457. */
  1458. if(lpbi->biSize != sizeof(BITMAPCOREHEADER))
  1459. {
  1460. if (lpbi->biClrUsed != 0)
  1461. return (WORD)lpbi->biClrUsed;
  1462. bits = lpbi->biBitCount;
  1463. }
  1464. else
  1465. {
  1466. bits = lpbc->bcBitCount;
  1467. }
  1468. switch(bits)
  1469. {
  1470. case 1: return 2;
  1471. case 4: return 16;
  1472. case 8: return 256;
  1473. default: return 0; /* A 24 bitcount DIB has no color table */
  1474. }
  1475. }
  1476. //-------------------------------------------------------------------
  1477. HRESULT CWbemClassObject::GetValueMap(const _bstr_t& _Name,
  1478. long value,
  1479. _bstr_t &str)
  1480. {
  1481. HRESULT hrMap, hr = WBEM_E_NOT_FOUND;
  1482. _variant_t vArray, vMapArray;
  1483. IWbemQualifierSet *qual = NULL;
  1484. // get the qualifiers.
  1485. hr = m_pWbemObject->GetPropertyQualifierSet(_Name, &qual);
  1486. if(SUCCEEDED(hr) && qual)
  1487. {
  1488. // see if there's a valueMap.
  1489. hrMap = qual->Get(L"ValueMap", 0, &vMapArray, NULL);
  1490. // get the Value qualifier.
  1491. hr = qual->Get(L"Values", 0, &vArray, NULL);
  1492. if(SUCCEEDED(hr) && (vArray.vt == (VT_BSTR|VT_ARRAY)))
  1493. {
  1494. // get the property value we're mapping.
  1495. long index;
  1496. // do we need to walk through the valueMap?
  1497. if(SUCCEEDED(hrMap))
  1498. {
  1499. SAFEARRAY *pma = V_ARRAY(&vMapArray);
  1500. long lLowerBound = 0, lUpperBound = 0 ;
  1501. SafeArrayGetLBound(pma, 1, &lLowerBound);
  1502. SafeArrayGetUBound(pma, 1, &lUpperBound);
  1503. for(long x = lLowerBound; x <= lUpperBound; x++)
  1504. {
  1505. BSTR vMap;
  1506. SafeArrayGetElement(pma, &x, &vMap);
  1507. long vInt = _ttol((LPCTSTR)vMap);
  1508. if(value == vInt)
  1509. {
  1510. index = x;
  1511. break; // found it.
  1512. }
  1513. } //endfor
  1514. }
  1515. else
  1516. {
  1517. index = value;
  1518. }
  1519. // lookup the string.
  1520. SAFEARRAY *psa = V_ARRAY(&vArray);
  1521. long ix[1] = {index};
  1522. BSTR str2;
  1523. hr = SafeArrayGetElement(psa, ix, &str2);
  1524. if(SUCCEEDED(hr))
  1525. {
  1526. str = str2;
  1527. SysFreeString(str2);
  1528. hr = S_OK;
  1529. }
  1530. else
  1531. {
  1532. hr = WBEM_E_VALUE_OUT_OF_RANGE;
  1533. }
  1534. }
  1535. qual->Release();
  1536. }
  1537. return hr;
  1538. }
  1539. //-----------------------------------------------------------
  1540. #define ITSA_BAD_PREFIX -3
  1541. #define ITSA_GOT_LETTERS -2
  1542. #define ITSA_MISSING_DECIMAL -1
  1543. #define ITSA_WRONG_SIZE 0
  1544. #define ITSA_DATETIME 1
  1545. #define ITSA_INTERVAL 2
  1546. int CWbemClassObject::ValidDMTF(bstr_t dmtf)
  1547. {
  1548. int retval = ITSA_DATETIME;
  1549. TCHAR wszText[26] = {0};
  1550. _tcscpy(wszText, (LPCTSTR)dmtf);
  1551. if(_tcslen(wszText) != 25)
  1552. retval = ITSA_WRONG_SIZE; // wrong size.
  1553. else if(wszText[14] != _T('.'))
  1554. retval = ITSA_MISSING_DECIMAL; // missing decimal
  1555. else if(_tcsspn(wszText, _T("0123456789-+:.")) != 25)
  1556. retval = ITSA_GOT_LETTERS;
  1557. else if(retval > 0)
  1558. {
  1559. if(wszText[21] == _T('+'))
  1560. retval = ITSA_DATETIME;
  1561. else if(wszText[21] == _T('-'))
  1562. retval = ITSA_DATETIME;
  1563. else if(wszText[21] == _T(':'))
  1564. retval = ITSA_INTERVAL;
  1565. else
  1566. retval = ITSA_BAD_PREFIX; // wrong utc prefix.
  1567. }
  1568. return retval;
  1569. }
  1570. //-----------------------------------------------------------
  1571. HRESULT CWbemClassObject::GetDateTimeFormat(const _bstr_t& _Name,
  1572. bstr_t &timeStr)
  1573. {
  1574. int v = 0;
  1575. HRESULT hr = WBEM_E_NOT_FOUND;
  1576. SYSTEMTIME st, local;
  1577. TCHAR temp[100] = {0};
  1578. bstr_t dmtf = GetString(_Name);
  1579. // validate it.
  1580. if((v = ValidDMTF(dmtf)) == ITSA_DATETIME)
  1581. {
  1582. _stscanf(dmtf, _T("%4hu%2hu%2hu%2hu%2hu%2hu"),
  1583. &st.wYear, &st.wMonth, &st.wDay,
  1584. &st.wHour, &st.wMinute, &st.wSecond);
  1585. st.wMilliseconds = 0;
  1586. // its always GMT so localize it.
  1587. TIME_ZONE_INFORMATION tzi;
  1588. DWORD zone = GetTimeZoneInformation(&tzi);
  1589. if(SystemTimeToTzSpecificLocalTime(&tzi, &st, &local) == 0)
  1590. {
  1591. // argh 9x, we're on our own.
  1592. LARGE_INTEGER UTC_FT, local_FT, bias;
  1593. // failed cuz its 9x so GetTzInfo() return behavior is "obvious".
  1594. bias.QuadPart = Int32x32To64((zone == TIME_ZONE_ID_DAYLIGHT ?
  1595. (tzi.Bias + tzi.DaylightBias)*60 :
  1596. (tzi.Bias + tzi.StandardBias)*60), // Bias in seconds
  1597. 10000000);
  1598. // convert the UTC systemtime to UTC filetime.
  1599. if(SystemTimeToFileTime(&st, (LPFILETIME)&UTC_FT))
  1600. {
  1601. // now we can trust the math.
  1602. local_FT.QuadPart = UTC_FT.QuadPart - bias.QuadPart;
  1603. if(!FileTimeToSystemTime((LPFILETIME)&local_FT, &local))
  1604. {
  1605. // failed. Pass through UTC.
  1606. memcpy(&local, &st, sizeof(SYSTEMTIME));
  1607. }
  1608. }
  1609. else
  1610. {
  1611. // failed. Pass through UTC.
  1612. memcpy(&local, &st, sizeof(SYSTEMTIME));
  1613. }
  1614. }
  1615. DWORD chUsed = GetDateFormat(NULL, 0, &local, NULL, temp, 100);
  1616. if(chUsed <= 0)
  1617. {
  1618. hr = HRESULT_FROM_WIN32(chUsed);
  1619. }
  1620. else
  1621. {
  1622. temp[chUsed-1] = _T(' ');
  1623. chUsed = GetTimeFormat(NULL, TIME_NOSECONDS, &local, NULL, &(temp[chUsed]), 100 - chUsed);
  1624. //claim victory.
  1625. if(chUsed <= 0)
  1626. {
  1627. hr = HRESULT_FROM_WIN32(chUsed);
  1628. }
  1629. else
  1630. {
  1631. timeStr = temp;
  1632. hr = S_OK;
  1633. }
  1634. }
  1635. }
  1636. else
  1637. {
  1638. hr = WBEM_E_TYPE_MISMATCH;
  1639. }
  1640. return hr;
  1641. }
  1642. //-----------------------------------------------------------------------------
  1643. // these cast string props fm the parm.
  1644. HRESULT CWbemClassObject::PutEx(const _bstr_t& _Name, const long _value, CIMTYPE vType)
  1645. {
  1646. _variant_t test;
  1647. HRESULT hr = Get(_Name, test);
  1648. // it wants a string...
  1649. if(test.vt == VT_BSTR)
  1650. {
  1651. TCHAR temp[40] = {0};
  1652. _ltot(_value, temp, 10);
  1653. return Put(_Name, (bstr_t)temp);
  1654. }
  1655. else
  1656. return Put(_Name, (long)_value);
  1657. }
  1658. //-----------------------------------------------------------------------------
  1659. HRESULT CWbemClassObject::PutEx(const _bstr_t& _Name, const bool _value,CIMTYPE vType)
  1660. {
  1661. _variant_t test;
  1662. HRESULT hr = Get(_Name, test);
  1663. // it wants a string...
  1664. if(test.vt == VT_BSTR)
  1665. {
  1666. bstr_t temp = (_value? _T("1"):_T("0"));
  1667. return Put(_Name, temp);
  1668. }
  1669. else
  1670. return Put(_Name, (long)_value);
  1671. }
  1672. //-----------------------------------------------------------------------------
  1673. long CWbemClassObject::GetLongEx(const _bstr_t& _Name)
  1674. {
  1675. _variant_t _value(0L);
  1676. HRESULT hr = Get(_Name, _value);
  1677. if(FAILED(hr))
  1678. return 0;
  1679. if(_value.vt == VT_BSTR)
  1680. {
  1681. bstr_t temp = V_BSTR(&_value);
  1682. return _ttol(temp);
  1683. }
  1684. else if (_value.vt == VT_NULL)
  1685. return 0;
  1686. else
  1687. return _value;
  1688. }
  1689. //-----------------------------------------------------------------------------
  1690. bool CWbemClassObject::GetBoolEx(const _bstr_t& _Name)
  1691. {
  1692. _variant_t _value;
  1693. HRESULT hr = Get(_Name, _value);
  1694. if(_value.vt == VT_BSTR)
  1695. {
  1696. LPWSTR temp = V_BSTR(&_value);
  1697. return (temp[0] != L'0');
  1698. }
  1699. else if (_value.vt == VT_NULL)
  1700. return false;
  1701. else
  1702. return _value;
  1703. }