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.

745 lines
15 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CopyRight ( c ) 1999 Microsoft Corporation
  4. //
  5. // Module Name: instanceprov.cpp
  6. //
  7. // Description:
  8. // Implementation of CInstanceProv class
  9. //
  10. // Author:
  11. // Henry Wang ( henrywa ) March 8, 2000
  12. //
  13. //
  14. //////////////////////////////////////////////////////////////////////
  15. #include "DnsWmi.h"
  16. long glNumInst = 0;
  17. //***************************************************************************
  18. //
  19. // CInstanceProv::CInstanceProv
  20. // CInstanceProv::~CInstanceProv
  21. //
  22. //***************************************************************************
  23. CInstanceProv::CInstanceProv(
  24. BSTR ObjectPath,
  25. BSTR User,
  26. BSTR Password,
  27. IWbemContext * pCtx)
  28. {
  29. DBG_FN( "ctor" )
  30. DNS_DEBUG( INSTPROV, (
  31. "%s: count before increment is %d\n"
  32. " ObjectPath %S\n"
  33. " User %S\n",
  34. fn, g_cObj, ObjectPath, User ));
  35. InterlockedIncrement(&g_cObj);
  36. return;
  37. }
  38. CInstanceProv::~CInstanceProv(void)
  39. {
  40. DBG_FN( "dtor" )
  41. DNS_DEBUG( INSTPROV, (
  42. "%s: count before decrement is %d\n", fn, g_cObj ));
  43. InterlockedDecrement(&g_cObj);
  44. #ifdef _DEBUG
  45. _CrtDumpMemoryLeaks();
  46. #endif
  47. return;
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. //++
  51. //
  52. // CInstanceProv::DoExecQueryAsync
  53. //
  54. // Description:
  55. // enum instance for a given class
  56. //
  57. // Arguments:
  58. // QueryLanguage [IN]
  59. // A valid BSTR containing one of the query languages
  60. // supported by Windows Management. This must be WQL.
  61. // Query [IN]
  62. // A valid BSTR containing the text of the query
  63. // lFlags [IN] WMI flag
  64. // pCtx* [IN] WMI context
  65. // pHandler* [IN] WMI sink pointer
  66. //
  67. // Return Value:
  68. // WBEM_S_NO_ERROR
  69. //
  70. //--
  71. /////////////////////////////////////////////////////////////////////////////
  72. SCODE
  73. CInstanceProv::DoExecQueryAsync(
  74. BSTR QueryLanguage,
  75. BSTR Query,
  76. long lFlags,
  77. IWbemContext *pCtx,
  78. IWbemObjectSink *pHandler
  79. )
  80. {
  81. DBG_FN( "CIP::DoExecQueryAsync" )
  82. DNS_DEBUG( INSTPROV, (
  83. "%s: %S flags=%lu %S\n",
  84. fn, QueryLanguage, lFlags, Query ));
  85. SCODE sc = WBEM_S_NO_ERROR;
  86. try
  87. {
  88. CTextLexSource objQuerySource(Query);
  89. SQL1_Parser objParser(&objQuerySource);
  90. SQL_LEVEL_1_RPN_EXPRESSION * objQueryExpr = NULL;
  91. objParser.Parse( &objQueryExpr );
  92. if ( !objQueryExpr )
  93. {
  94. throw( WBEM_E_OUT_OF_MEMORY );
  95. }
  96. CDnsBase* pDns = NULL ;
  97. sc = CreateClass(
  98. objQueryExpr->bsClassName,
  99. m_pNamespace,
  100. (void**) &pDns);
  101. if( FAILED ( sc ) )
  102. {
  103. return sc;
  104. }
  105. auto_ptr<CDnsBase> pDnsBase(pDns);
  106. int nNumTokens = objQueryExpr->nNumTokens;
  107. CSqlEval* pEval = CSqlEval::CreateClass(
  108. objQueryExpr,
  109. &nNumTokens);
  110. auto_ptr<CSqlEval> apEval(pEval);
  111. sc = pDnsBase->ExecQuery(
  112. &(*apEval),
  113. lFlags,
  114. pCtx,
  115. pHandler);
  116. }
  117. catch(CDnsProvException e)
  118. {
  119. CWbemClassObject Status;
  120. SCODE sc = SetExtendedStatus(e.what(), Status);
  121. if ( SUCCEEDED ( sc ) )
  122. {
  123. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  124. return sc;
  125. }
  126. }
  127. catch(SCODE exSc)
  128. {
  129. sc = exSc;
  130. }
  131. catch(...)
  132. {
  133. sc = WBEM_E_FAILED;
  134. }
  135. pHandler->SetStatus(0, sc,NULL,NULL);
  136. return sc;
  137. };
  138. /////////////////////////////////////////////////////////////////////////////
  139. //++
  140. //
  141. // CInstanceProv::DoCreateInstanceEnumAsync
  142. //
  143. // Description:
  144. // enum instance for a given class
  145. //
  146. // Arguments:
  147. // RefStr [IN[ name the class to enumerate
  148. // lFlags [IN] WMI flag
  149. // pCtx* [IN] WMI context
  150. // pHandler* [IN] WMI sink pointer
  151. //
  152. // Return Value:
  153. // WBEM_S_NO_ERROR
  154. //
  155. //--
  156. /////////////////////////////////////////////////////////////////////////////
  157. SCODE
  158. CInstanceProv::DoCreateInstanceEnumAsync(
  159. BSTR RefStr,
  160. long lFlags,
  161. IWbemContext *pCtx,
  162. IWbemObjectSink *pHandler
  163. )
  164. {
  165. DBG_FN( "CIP::DoCreateInstanceEnumAsync" );
  166. DNS_DEBUG( INSTPROV, (
  167. "%s: flags=%lu %S\n",
  168. fn, lFlags, RefStr ));
  169. SCODE sc;
  170. int iCnt;
  171. // Do a check of arguments and make sure we have pointer to Namespace
  172. if(pHandler == NULL || m_pNamespace == NULL)
  173. return WBEM_E_INVALID_PARAMETER;
  174. try
  175. {
  176. CDnsBase* pDns = NULL ;
  177. sc = CreateClass(RefStr, m_pNamespace, (void**) &pDns);
  178. if( FAILED ( sc ) )
  179. {
  180. return sc;
  181. }
  182. auto_ptr<CDnsBase> pDnsBase(pDns);
  183. sc = pDnsBase->EnumInstance(
  184. lFlags,
  185. pCtx,
  186. pHandler);
  187. }
  188. catch(CDnsProvException e)
  189. {
  190. CWbemClassObject Status;
  191. SCODE sc = SetExtendedStatus(e.what(), Status);
  192. if ( SUCCEEDED ( sc ) )
  193. {
  194. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  195. return sc;
  196. }
  197. }
  198. catch(SCODE exSc)
  199. {
  200. sc = exSc;
  201. }
  202. catch(...)
  203. {
  204. sc = WBEM_E_FAILED;
  205. }
  206. pHandler->SetStatus(0, sc,NULL,NULL);
  207. return sc;
  208. }
  209. /////////////////////////////////////////////////////////////////////////////
  210. //++
  211. //
  212. // CInstanceProv::GetObject
  213. //
  214. // Description:
  215. // Creates an instance given a particular path value.
  216. //
  217. // Arguments:
  218. // ObjectPath [IN] object path to an object
  219. // lFlags [IN] WMI flag
  220. // pCtx* [IN] WMI context
  221. // pHandler* [IN] WMI sink pointer
  222. //
  223. // Return Value:
  224. // WBEM_S_NO_ERROR
  225. // win32 error
  226. //
  227. //--
  228. /////////////////////////////////////////////////////////////////////////////
  229. SCODE
  230. CInstanceProv::DoGetObjectAsync(
  231. BSTR ObjectPath,
  232. long lFlags,
  233. IWbemContext *pCtx,
  234. IWbemObjectSink FAR* pHandler
  235. )
  236. {
  237. DBG_FN( "CIP::DoGetObjectAsync" );
  238. DNS_DEBUG( INSTPROV, (
  239. "%s: flags=%lu %S\n",
  240. fn, lFlags, ObjectPath ));
  241. SCODE sc;
  242. IWbemClassObject FAR* pObj;
  243. BOOL bOK = FALSE;
  244. // Do a check of arguments and make sure we have pointer to Namespace
  245. if( ObjectPath == NULL || pHandler == NULL || m_pNamespace == NULL )
  246. {
  247. DNS_DEBUG( INSTPROV, (
  248. "%s: bad parameter - WBEM_E_INVALID_PARAMETER\n", fn ));
  249. return WBEM_E_INVALID_PARAMETER;
  250. }
  251. // do the get, pass the object on to the notify
  252. try
  253. {
  254. CObjPath ObjPath;
  255. if(!ObjPath.Init(ObjectPath))
  256. {
  257. DNS_DEBUG( INSTPROV, (
  258. "%s: bad object path - WBEM_E_INVALID_PARAMETER\n", fn ));
  259. return WBEM_E_INVALID_PARAMETER;
  260. }
  261. CDnsBase* pDns = NULL;
  262. wstring wstrClass = ObjPath.GetClassName();
  263. sc = CreateClass(
  264. (WCHAR*)wstrClass.data(),
  265. m_pNamespace,
  266. (void**) &pDns);
  267. if( FAILED(sc) )
  268. {
  269. DNS_DEBUG( INSTPROV, (
  270. "%s: CreateClass returned 0x%08X\n", fn, sc ));
  271. return sc;
  272. }
  273. auto_ptr<CDnsBase> pDnsBase(pDns);
  274. sc = pDnsBase->GetObject(
  275. ObjPath,
  276. lFlags,
  277. pCtx,
  278. pHandler);
  279. }
  280. catch( CDnsProvException e )
  281. {
  282. DNS_DEBUG( INSTPROV, (
  283. "%s: caught CDnsProvException \"%s\"\n", fn, e.what() ));
  284. CWbemClassObject Status;
  285. SCODE sc = SetExtendedStatus(e.what(), Status);
  286. if ( SUCCEEDED ( sc ) )
  287. {
  288. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  289. return sc;
  290. }
  291. }
  292. catch(SCODE exSc)
  293. {
  294. sc = exSc;
  295. }
  296. catch(...)
  297. {
  298. sc = WBEM_E_FAILED;
  299. }
  300. pHandler->SetStatus(0, sc,NULL,NULL);
  301. #ifdef _DEBUG
  302. // _CrtDumpMemoryLeaks();
  303. #endif
  304. return sc;
  305. }
  306. /////////////////////////////////////////////////////////////////////////////
  307. //++
  308. //
  309. // CInstanceProv::DoPutInstanceAsync
  310. //
  311. // Description:
  312. // save this instance
  313. //
  314. // Arguments:
  315. // pInst [IN] WMI object to be saved
  316. // lFlags [IN] WMI flag
  317. // pCtx* [IN] WMI context
  318. // pHandler* [IN] WMI sink pointer
  319. //
  320. // Return Value:
  321. // WBEM_S_NO_ERROR
  322. //
  323. //--
  324. /////////////////////////////////////////////////////////////////////////////
  325. SCODE
  326. CInstanceProv::DoPutInstanceAsync(
  327. IWbemClassObject *pInst,
  328. long lFlags,
  329. IWbemContext *pCtx,
  330. IWbemObjectSink *pHandler
  331. )
  332. {
  333. DBG_FN( "CIP::DoPutInstanceAsync" )
  334. DNS_DEBUG( INSTPROV, (
  335. "%s: flags=%lu pInst=%p\n",
  336. fn, lFlags, pInst ));
  337. SCODE sc;
  338. if(pInst == NULL || pHandler == NULL )
  339. {
  340. DNS_DEBUG( INSTPROV, (
  341. "%s: returning WBEM_E_INVALID_PARAMETER\n" ));
  342. return WBEM_E_INVALID_PARAMETER;
  343. }
  344. try
  345. {
  346. // get class name
  347. wstring wstrClass;
  348. CWbemClassObject Inst(pInst);
  349. Inst.GetProperty(
  350. wstrClass,
  351. L"__Class");
  352. wstring wstrPath;
  353. Inst.GetProperty(
  354. wstrPath,
  355. L"__RelPath");
  356. CDnsBase* pDns = NULL;
  357. sc = CreateClass(
  358. wstrClass.data(),
  359. m_pNamespace,
  360. (void**) &pDns);
  361. if( FAILED(sc) )
  362. {
  363. return sc;
  364. }
  365. auto_ptr<CDnsBase> pDnsBase(pDns);
  366. DNS_DEBUG( INSTPROV, (
  367. "%s: doing base PutInstance\n"
  368. " class: %S\n"
  369. " path: %S\n",
  370. fn,
  371. wstrClass.c_str(),
  372. wstrPath.c_str() ));
  373. sc = pDnsBase->PutInstance(
  374. pInst,
  375. lFlags,
  376. pCtx,
  377. pHandler);
  378. }
  379. catch(CDnsProvException e)
  380. {
  381. DNS_DEBUG( INSTPROV, (
  382. "%s: caught CDnsProvException \"%s\"\n", fn,
  383. e.what() ));
  384. CWbemClassObject Status;
  385. SCODE sc = SetExtendedStatus(e.what(), Status);
  386. if (SUCCEEDED ( sc ))
  387. {
  388. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  389. return sc;
  390. }
  391. }
  392. catch(SCODE exSc)
  393. {
  394. DNS_DEBUG( INSTPROV, (
  395. "%s: cauught SCODE 0x%08X\n", fn, exSc ));
  396. sc = exSc;
  397. }
  398. catch(...)
  399. {
  400. DNS_DEBUG( INSTPROV, (
  401. "%s: cauught unknown exception returning WBEM_E_FAILED\n", fn ));
  402. sc = WBEM_E_FAILED;
  403. }
  404. pHandler->SetStatus(0, sc,NULL,NULL);
  405. #ifdef _DEBUG
  406. // _CrtDumpMemoryLeaks();
  407. #endif
  408. return sc;
  409. }
  410. /////////////////////////////////////////////////////////////////////////////
  411. //++
  412. //
  413. // CInstanceProv::DoDeleteInstanceAsync
  414. //
  415. // Description:
  416. // delete this instance
  417. //
  418. // Arguments:
  419. // rObjPath [IN] ObjPath for the instance to be deleted
  420. // lFlags [IN] WMI flag
  421. // pCtx* [IN] WMI context
  422. // pHandler* [IN] WMI sink pointer
  423. //
  424. // Return Value:
  425. // WBEM_S_NO_ERROR
  426. //
  427. //--
  428. /////////////////////////////////////////////////////////////////////////////
  429. SCODE CInstanceProv::DoDeleteInstanceAsync(
  430. BSTR ObjectPath,
  431. long lFlags,
  432. IWbemContext * pCtx,
  433. IWbemObjectSink * pHandler
  434. )
  435. {
  436. DBG_FN( "CIP::DoDeleteInstanceAsync" );
  437. DNS_DEBUG( INSTPROV, (
  438. "%s: flags=%lu %S\n",
  439. fn, lFlags, ObjectPath ));
  440. SCODE sc;
  441. // Do a check of arguments and make sure we have pointer to Namespace
  442. if(ObjectPath == NULL || pHandler == NULL )
  443. {
  444. return WBEM_E_INVALID_PARAMETER;
  445. }
  446. // do the get, pass the object on to the notify
  447. try
  448. {
  449. CObjPath ObjPath;
  450. if(!ObjPath.Init(ObjectPath))
  451. {
  452. return WBEM_E_INVALID_PARAMETER;
  453. }
  454. CDnsBase* pDns = NULL;
  455. wstring wstrClass = ObjPath.GetClassName();
  456. sc = CreateClass(
  457. (WCHAR*)wstrClass.data(),
  458. m_pNamespace,
  459. (void**) &pDns);
  460. if( FAILED(sc) )
  461. {
  462. return sc;
  463. }
  464. auto_ptr<CDnsBase> pDnsBase(pDns);
  465. sc = pDnsBase->DeleteInstance(
  466. ObjPath,
  467. lFlags,
  468. pCtx,
  469. pHandler);
  470. }
  471. catch(CDnsProvException e)
  472. {
  473. CWbemClassObject Status;
  474. SCODE sc = SetExtendedStatus(e.what(), Status);
  475. if (SUCCEEDED ( sc ))
  476. {
  477. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  478. return sc;
  479. }
  480. }
  481. catch(SCODE exSc)
  482. {
  483. sc = exSc;
  484. }
  485. catch(...)
  486. {
  487. sc = WBEM_E_FAILED;
  488. }
  489. pHandler->SetStatus(0, sc,NULL,NULL);
  490. return sc;
  491. }
  492. /////////////////////////////////////////////////////////////////////////////
  493. //++
  494. //
  495. // CInstanceProv::DoExecMethodAsync
  496. //
  497. // Description:
  498. // execute methods for the given object
  499. //
  500. // Arguments:
  501. // ObjectPath [IN] object path to a given object
  502. // pwszMethodName [IN] name of the method to be invoked
  503. // lFlags [IN] WMI flag
  504. // pInParams* [IN] Input parameters for the method
  505. // pHandler* [IN] WMI sink pointer
  506. //
  507. // Return Value:
  508. // WBEM_S_NO_ERROR
  509. //
  510. //--
  511. /////////////////////////////////////////////////////////////////////////////
  512. SCODE
  513. CInstanceProv::DoExecMethodAsync(
  514. BSTR strObjectPath,
  515. BSTR strMethodName,
  516. long lFlags,
  517. IWbemContext *pCtx,
  518. IWbemClassObject *pInParams,
  519. IWbemObjectSink *pHandler
  520. )
  521. {
  522. DBG_FN( "CIP::DoExecMethodAsync" );
  523. DNS_DEBUG( INSTPROV, (
  524. "%s: flags=%lu method=%S %S\n",
  525. fn, lFlags, strMethodName, strObjectPath ));
  526. SCODE sc;
  527. if(strObjectPath == NULL || pHandler == NULL || m_pNamespace == NULL
  528. || strMethodName == NULL )
  529. return WBEM_E_INVALID_PARAMETER;
  530. CDnsBase * pDns;
  531. try
  532. {
  533. CObjPath ObjPath;
  534. if(!ObjPath.Init(strObjectPath))
  535. {
  536. return WBEM_E_INVALID_PARAMETER;
  537. }
  538. wstring wstrClass = ObjPath.GetClassName();
  539. sc = CreateClass(
  540. wstrClass.data(),
  541. m_pNamespace,
  542. (void**) &pDns);
  543. if( FAILED(sc) )
  544. {
  545. return sc;
  546. }
  547. auto_ptr<CDnsBase> pDnsBase(pDns);
  548. sc = pDnsBase->ExecuteMethod(
  549. ObjPath,
  550. strMethodName,
  551. lFlags,
  552. pInParams,
  553. pHandler);
  554. }
  555. catch(CDnsProvException e)
  556. {
  557. DNS_DEBUG( INSTPROV, (
  558. "%s: caught CDnsProvException %s\n",
  559. fn, e.what() ));
  560. CWbemClassObject Status;
  561. SCODE sc = SetExtendedStatus(e.what(), Status);
  562. if (SUCCEEDED ( sc ))
  563. {
  564. sc = pHandler->SetStatus(
  565. 0,
  566. WBEM_E_FAILED,
  567. NULL,
  568. *(&Status));
  569. return sc;
  570. }
  571. }
  572. catch(SCODE exSc)
  573. {
  574. sc = exSc;
  575. DNS_DEBUG( INSTPROV, ( "%s: caught SCODE 0x%08X\n", fn, sc ));
  576. }
  577. catch(...)
  578. {
  579. sc = WBEM_E_FAILED;
  580. DNS_DEBUG( INSTPROV, ( "%s: caught unknown exception returning WBEM_E_FAILED\n", fn ));
  581. }
  582. pHandler->SetStatus(0, sc,NULL,NULL);
  583. #ifdef _DEBUG
  584. // _CrtDumpMemoryLeaks();
  585. #endif
  586. DNS_DEBUG( INSTPROV, ( "%s: returning 0x%08X\n", fn, sc ));
  587. return sc;
  588. }
  589. /////////////////////////////////////////////////////////////////////////////
  590. //++
  591. //
  592. // CInstanceProv::SetExtendedStatus
  593. //
  594. // Description:
  595. // create and set extended error status
  596. //
  597. // Arguments:
  598. // ErrString [IN] Error message string
  599. // Inst [IN OUT] reference to WMI instance
  600. //
  601. // Return Value:
  602. // WBEM_S_NO_ERROR
  603. //
  604. //--
  605. /////////////////////////////////////////////////////////////////////////////
  606. SCODE
  607. CInstanceProv::SetExtendedStatus(
  608. const char* ErrString,
  609. CWbemClassObject& Inst)
  610. {
  611. DBG_FN( "CIP::SetExtendedStatus" );
  612. DNS_DEBUG( INSTPROV, ( "%s\n", fn ));
  613. IWbemClassObject* pStatus;
  614. BSTR bstrStatus = SysAllocString(L"__ExtendedStatus");
  615. if( bstrStatus == NULL)
  616. {
  617. return WBEM_E_OUT_OF_MEMORY;
  618. }
  619. SCODE sc = m_pNamespace->GetObject(
  620. bstrStatus,
  621. 0,
  622. 0,
  623. &pStatus,
  624. NULL) ;
  625. SysFreeString(bstrStatus);
  626. if( SUCCEEDED ( sc ) )
  627. {
  628. sc = pStatus->SpawnInstance(0, &Inst);
  629. if ( SUCCEEDED ( sc ))
  630. {
  631. sc = Inst.SetProperty(
  632. ErrString,
  633. L"Description");
  634. }
  635. }
  636. return sc;
  637. }