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.

5216 lines
152 KiB

  1. /****************************************************************************
  2. Copyright information : Copyright (c) 1998-1999 Microsoft Corporation
  3. File Name : ExecEngine.cpp
  4. Project Name : WMI Command Line
  5. Author Name : Ch. Sriramachandramurthy
  6. Date of Creation (dd/mm/yy) : 27th-September-2000
  7. Version Number : 1.0
  8. Brief Description : This class encapsulates the functionality of
  9. Execution Engine. Obtains the needed information
  10. from CGlobalSwitches and CCommandSwitches of
  11. CParsedInfo and executes needed WMI operations.
  12. The result is sent to Format Engine via
  13. CGlobalSwitches and CCommandSwicthes
  14. of CParsedInfo.
  15. Revision History :
  16. Last Modified By : Ch. Sriramachandramurthy
  17. Last Modified Date : 20th-March-2001
  18. ****************************************************************************/
  19. // include files
  20. #include "Precomp.h"
  21. #include "GlobalSwitches.h"
  22. #include "CommandSwitches.h"
  23. #include "HelpInfo.h"
  24. #include "ErrorLog.h"
  25. #include "ParsedInfo.h"
  26. #include "ErrorInfo.h"
  27. #include "WmiCliXMLLog.h"
  28. #include "FormatEngine.h"
  29. #include "CmdTokenizer.h"
  30. #include "CmdAlias.h"
  31. #include "ParserEngine.h"
  32. #include "ExecEngine.h"
  33. #include "WmiCmdLn.h"
  34. /*------------------------------------------------------------------------
  35. Name :CExecEngine
  36. Synopsis :Constructor, This function initializes the necessary
  37. member variables.
  38. Type :Constructor
  39. Input Parameter(s) :None
  40. Output Parameter(s):None
  41. Return Type :None
  42. Global Variables :None
  43. Calling Syntax :None
  44. Notes :None
  45. ------------------------------------------------------------------------*/
  46. CExecEngine::CExecEngine()
  47. {
  48. m_pITextSrc = NULL;
  49. m_pIWbemLocator = NULL;
  50. m_pITargetNS = NULL;
  51. m_pIContext = NULL;
  52. m_bTrace = FALSE;
  53. m_bNoAssoc = FALSE;
  54. }
  55. /*------------------------------------------------------------------------
  56. Name :~CExecEngine
  57. Synopsis :Destructor, This function call Uninitialize() which
  58. frees memory held by the object.
  59. Type :Destructor
  60. Input Parameter(s) :None
  61. Output Parameter(s):None
  62. Return Type :None
  63. Global Variables :None
  64. Calling Syntax :None
  65. Notes :None
  66. ------------------------------------------------------------------------*/
  67. CExecEngine::~CExecEngine()
  68. {
  69. SAFEIRELEASE(m_pITextSrc);
  70. SAFEIRELEASE(m_pIContext);
  71. SAFEIRELEASE(m_pIWbemLocator);
  72. Uninitialize();
  73. }
  74. /*------------------------------------------------------------------------
  75. Name :Uninitialize
  76. Synopsis :This function uninitializes the member variables.
  77. Type :Member Function
  78. Input Parameter(s):
  79. bFinal - boolean value which when set indicates that the
  80. program
  81. Output Parameter(s):None
  82. Return Type :void
  83. Global Variables :None
  84. Calling Syntax :Uninitialize()
  85. Notes :None
  86. ------------------------------------------------------------------------*/
  87. void CExecEngine::Uninitialize(BOOL bFinal)
  88. {
  89. SAFEIRELEASE(m_pITargetNS);
  90. m_bTrace = FALSE;
  91. m_eloErrLogOpt = NO_LOGGING;
  92. m_bNoAssoc = FALSE;
  93. if (bFinal)
  94. {
  95. SAFEIRELEASE(m_pITextSrc);
  96. SAFEIRELEASE(m_pIContext);
  97. SAFEIRELEASE(m_pIWbemLocator);
  98. }
  99. }
  100. /*------------------------------------------------------------------------
  101. Name :SetLocatorObject
  102. Synopsis :Sets the locator object passed via parameter to member
  103. of the class.
  104. Type :Member Function
  105. Input Parameter(s):
  106. pILocator - Pointer to IWbemLocator
  107. Output Parameter(s):None
  108. Return Type :BOOL
  109. Global Variables :None
  110. Calling Syntax :SetLocatorObject(pILocator)
  111. Notes :None
  112. ------------------------------------------------------------------------*/
  113. BOOL CExecEngine::SetLocatorObject(IWbemLocator* pILocator)
  114. {
  115. static BOOL bFirst = TRUE;
  116. BOOL bRet = TRUE;
  117. if (bFirst)
  118. {
  119. if (pILocator != NULL)
  120. {
  121. SAFEIRELEASE(m_pIWbemLocator);
  122. m_pIWbemLocator = pILocator;
  123. m_pIWbemLocator->AddRef();
  124. }
  125. else
  126. bRet = FALSE;
  127. bFirst = FALSE;
  128. }
  129. return bRet;
  130. }
  131. /*------------------------------------------------------------------------
  132. Name :ExecuteCommand
  133. Synopsis :Executes the command referring to information
  134. available with the CParsedInfo object.
  135. Stores the results in the CParsedInfo object.
  136. Type :Member Function
  137. Input Parameter(s):
  138. rParsedInfo - reference to CParsedInfo class object
  139. Output Parameter(s):
  140. rParsedInfo - reference to CParsedInfo class object
  141. Return Type :BOOL
  142. Global Variables :None
  143. Calling Syntax :ExecuteCommand(rParsedInfo)
  144. Notes :None
  145. ------------------------------------------------------------------------*/
  146. BOOL CExecEngine::ExecuteCommand(CParsedInfo& rParsedInfo)
  147. {
  148. BOOL bRet = TRUE;
  149. HRESULT hr = S_OK;
  150. _TCHAR *pszVerb = NULL;
  151. BOOL bContinue = TRUE;
  152. DWORD dwThreadId = GetCurrentThreadId();
  153. try
  154. {
  155. // Obtain the TRACE flag
  156. m_bTrace = rParsedInfo.GetGlblSwitchesObject().GetTraceStatus();
  157. // Obtain the Logging mode
  158. m_eloErrLogOpt = rParsedInfo.GetErrorLogObject().GetErrLogOption();
  159. // Enable|Disable the privileges
  160. hr = ModifyPrivileges(rParsedInfo.GetGlblSwitchesObject().
  161. GetPrivileges());
  162. if ( m_eloErrLogOpt )
  163. {
  164. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  165. _T("ModifyPrivileges(-)"), dwThreadId, rParsedInfo, FALSE);
  166. }
  167. ONFAILTHROWERROR(hr);
  168. // Obtian the verb name
  169. pszVerb = rParsedInfo.GetCmdSwitchesObject().GetVerbName();
  170. if (pszVerb != NULL)
  171. {
  172. // If GET | LIST verb is specified.
  173. if (CompareTokens(pszVerb, CLI_TOKEN_GET) ||
  174. CompareTokens(pszVerb, CLI_TOKEN_LIST))
  175. {
  176. bRet = ProcessSHOWInfo(rParsedInfo);
  177. }
  178. // If SET verb is specified.
  179. else if (CompareTokens(pszVerb, CLI_TOKEN_SET))
  180. {
  181. bRet = ProcessSETVerb(rParsedInfo);
  182. }
  183. // If CALL verb is specified.
  184. else if (CompareTokens(pszVerb, CLI_TOKEN_CALL))
  185. {
  186. bRet = ProcessCALLVerb(rParsedInfo);
  187. }
  188. // If ASSOC verb is specified.
  189. else if (CompareTokens(pszVerb, CLI_TOKEN_ASSOC))
  190. {
  191. bRet = ProcessASSOCVerb(rParsedInfo);
  192. }
  193. // If CREATE verb is specified.
  194. else if (CompareTokens(pszVerb, CLI_TOKEN_CREATE))
  195. {
  196. bRet = ProcessCREATEVerb(rParsedInfo);
  197. }
  198. // If DELETE verb is specified.
  199. else if (CompareTokens(pszVerb, CLI_TOKEN_DELETE))
  200. {
  201. bRet = ProcessDELETEVerb(rParsedInfo);
  202. }
  203. // If user defined verb is specified.
  204. else
  205. bRet = ProcessCALLVerb(rParsedInfo);
  206. }
  207. // If no verb is specified, (default behavior is assumed to be that of
  208. // GET i.e a command like 'w class Win32_Process' go ahead with
  209. // displaying the instance information.
  210. else
  211. {
  212. if (rParsedInfo.GetCmdSwitchesObject().
  213. SetVerbName(_T("GET")))
  214. {
  215. bRet = ProcessSHOWInfo(rParsedInfo);
  216. }
  217. else
  218. bRet = FALSE;
  219. }
  220. }
  221. catch(_com_error& e)
  222. {
  223. // To check unhandled exceptions thrown by _bstr_t objects etc..
  224. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  225. bRet = FALSE;
  226. }
  227. return bRet;
  228. }
  229. /*------------------------------------------------------------------------
  230. Name :ObtainXMLResultSet
  231. Synopsis :Executes query and obtain the result in XML file format.
  232. Refers data in the CCommnadSwicthes object of
  233. CParsedInfo object.
  234. Type :Member Function
  235. Input Parameter(s):
  236. bstrQuery - WQL query
  237. rParsedInfo - reference to CParsedInfo class object
  238. bstrXML - reference to XML result set obtained
  239. bSysProp - boolean flag indicating the presence of system
  240. properties.
  241. bNotAssoc - boolean flag indicating whether the query cotains
  242. ASSOCIATORS OF {xxxx} (or) SELECT * FROM xxx form.
  243. Output Parameter(s):
  244. rParsedInfo - reference to CParsedInfo class object
  245. Return Type :HRESULT
  246. Global Variables :None
  247. Calling Syntax :ObtainXMLResultSet(bstrQuery, rParsedInfo, bstrXML,
  248. bSysProp, bNotAssoc);
  249. Notes :None
  250. ------------------------------------------------------------------------*/
  251. HRESULT CExecEngine::ObtainXMLResultSet(BSTR bstrQuery,
  252. CParsedInfo& rParsedInfo,
  253. _bstr_t& bstrXML,
  254. BOOL bSysProp,
  255. BOOL bNotAssoc)
  256. {
  257. IWbemClassObject *pIObject = NULL;
  258. HRESULT hr = S_OK;
  259. IEnumWbemClassObject *pIEnum = NULL;
  260. ULONG ulReturned = 0;
  261. BSTR bstrInstXML = NULL;
  262. BOOL bInstances = FALSE;
  263. CHString chsMsg;
  264. DWORD dwThreadId = GetCurrentThreadId();
  265. VARIANT vSystem;
  266. try
  267. {
  268. VariantInit(&vSystem);
  269. if ( g_wmiCmd.GetBreakEvent() == FALSE )
  270. {
  271. // Add the <CIM> or <ASSOC.OBJECTARRAY> to the beginning of the
  272. // XML result. This is to facilitate storing of mutiple object
  273. // instances information.
  274. bstrXML = (bNotAssoc) ? MULTINODE_XMLSTARTTAG :
  275. MULTINODE_XMLASSOCSTAG1;
  276. // Create the IWbemContext object, used for suppressing
  277. // the system properties.
  278. if (m_pIContext == NULL)
  279. {
  280. hr = CreateContext(rParsedInfo);
  281. if ( m_eloErrLogOpt )
  282. {
  283. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  284. _T("CreateContext(rParsedInfo)"), dwThreadId,
  285. rParsedInfo, FALSE);
  286. }
  287. ONFAILTHROWERROR(hr);
  288. }
  289. // Execute the WQL query
  290. // WBEM_FLAG_FORWARD_ONLY flag Increases the speed of execution
  291. // WBEM_FLAG_RETURN_IMMEDIATELY flag makes semisynchronous call
  292. // Setting these flags in combination saves time, space, and
  293. // improves responsiveness.enumerators can be polled for the
  294. // results of the call.
  295. hr = m_pITargetNS->ExecQuery(_bstr_t(L"WQL"), bstrQuery,
  296. WBEM_FLAG_FORWARD_ONLY |
  297. WBEM_FLAG_RETURN_IMMEDIATELY,
  298. NULL, &pIEnum);
  299. if (m_bTrace || m_eloErrLogOpt)
  300. {
  301. chsMsg.Format(L"IWbemServices::ExecQuery(L\"WQL\", L\"%s\", "
  302. L"0, NULL, -)", (LPWSTR) bstrQuery);
  303. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  304. dwThreadId, rParsedInfo, m_bTrace);
  305. }
  306. ONFAILTHROWERROR(hr);
  307. // If no system properties are specified adjust the context to
  308. // filter out the system properties.
  309. vSystem.vt = VT_BOOL;
  310. // Filterout the system properties.
  311. if (!bSysProp)
  312. vSystem.boolVal = VARIANT_TRUE;
  313. // Don't filter the system properties.
  314. else
  315. vSystem.boolVal = VARIANT_FALSE;
  316. hr = m_pIContext->SetValue(_bstr_t(EXCLUDESYSPROP), 0, &vSystem);
  317. if (m_bTrace || m_eloErrLogOpt)
  318. {
  319. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  320. _T("IWbemContext::SetValue(L\"ExcludeSystemProperties\","
  321. L"0, -)"), dwThreadId, rParsedInfo, m_bTrace);
  322. }
  323. ONFAILTHROWERROR(hr);
  324. VARIANTCLEAR(vSystem);
  325. // Set the interface level security for the IEnumWbemClass object.
  326. hr = SetSecurity(pIEnum,
  327. rParsedInfo.GetAuthorityPrinciple(),
  328. rParsedInfo.GetNode(),
  329. rParsedInfo.GetUser(),
  330. rParsedInfo.GetPassword(),
  331. rParsedInfo.GetGlblSwitchesObject().
  332. GetAuthenticationLevel(),
  333. rParsedInfo.GetGlblSwitchesObject().
  334. GetImpersonationLevel());
  335. if (m_bTrace || m_eloErrLogOpt)
  336. {
  337. _TCHAR* pszAuthority = rParsedInfo.GetAuthorityPrinciple();
  338. if( pszAuthority != NULL &&
  339. _tcslen(pszAuthority) > 9 &&
  340. _tcsnicmp(pszAuthority, _T("KERBEROS:"), 9) == 0)
  341. {
  342. BSTR bstrPrincipalName = ::SysAllocString(&pszAuthority[9]);
  343. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_GSS_KERBEROS,"
  344. L"RPC_C_AUTHZ_NONE, %s, %d, %d, -, EOAC_NONE)",
  345. (LPWSTR)bstrPrincipalName,
  346. rParsedInfo.GetGlblSwitchesObject().
  347. GetAuthenticationLevel(),
  348. rParsedInfo.GetGlblSwitchesObject().
  349. GetImpersonationLevel());
  350. SAFEBSTRFREE(bstrPrincipalName);
  351. }
  352. else
  353. {
  354. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_WINNT,"
  355. L"RPC_C_AUTHZ_NONE, NULL, %d, %d, -, EOAC_NONE)",
  356. rParsedInfo.GetGlblSwitchesObject().
  357. GetAuthenticationLevel(),
  358. rParsedInfo.GetGlblSwitchesObject().
  359. GetImpersonationLevel());
  360. }
  361. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  362. dwThreadId, rParsedInfo, m_bTrace);
  363. }
  364. ONFAILTHROWERROR(hr);
  365. // Loop through the available instances
  366. hr = pIEnum->Next(WBEM_INFINITE, 1, &pIObject, &ulReturned);
  367. if (m_bTrace || m_eloErrLogOpt)
  368. {
  369. chsMsg.Format(L"IEnumWbemClassObject->Next"
  370. L"(WBEM_INFINITE, 1, -, -)");
  371. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  372. dwThreadId, rParsedInfo, m_bTrace);
  373. }
  374. ONFAILTHROWERROR(hr);
  375. while(ulReturned == 1)
  376. {
  377. // Set the instances flag to TRUE
  378. bInstances = TRUE;
  379. // Call the IWbemObjectTextSrc::GetText method, with
  380. // IWbemClassObject as one of the arguments.
  381. hr = m_pITextSrc->GetText(0, pIObject,
  382. WMI_OBJ_TEXT_CIM_DTD_2_0, m_pIContext, &bstrInstXML);
  383. if (m_bTrace || m_eloErrLogOpt)
  384. {
  385. chsMsg.Format(L"IWbemObjectTextSrc::GetText(0, -, "
  386. L"WMI_OBJECT_TEXT_CIM_DTD_2_0, -, -)");
  387. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  388. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, m_bTrace);
  389. }
  390. ONFAILTHROWERROR(hr);
  391. // Associators should be handled separately
  392. if (bNotAssoc == FALSE)
  393. {
  394. // Append the XML node to the XML nodes stream
  395. bstrXML = bstrXML + _bstr_t(MULTINODE_XMLASSOCSTAG2) +
  396. + bstrInstXML + _bstr_t(MULTINODE_XMLASSOCETAG2);
  397. }
  398. else
  399. {
  400. // Append the XML node to the XML nodes stream
  401. bstrXML += bstrInstXML;
  402. }
  403. // Release the memory allocated for bstrInstXML
  404. SAFEBSTRFREE(bstrInstXML);
  405. SAFEIRELEASE(pIObject);
  406. // if break event occurs then terminate the session
  407. if ( g_wmiCmd.GetBreakEvent() == TRUE )
  408. break;
  409. // Move to next instance in the enumeration.
  410. hr = pIEnum->Next(WBEM_INFINITE, 1, &pIObject, &ulReturned);
  411. if (m_bTrace || m_eloErrLogOpt)
  412. {
  413. chsMsg.Format(
  414. L"IEnumWbemClassObject->Next(WBEM_INFINITE, 1, -, -)");
  415. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  416. dwThreadId, rParsedInfo, m_bTrace);
  417. }
  418. ONFAILTHROWERROR(hr);
  419. }
  420. // If instances are available
  421. if (bInstances)
  422. {
  423. // Add the </CIM> or </ASSOC.OBJECTARRAY> at the end.
  424. bstrXML += (bNotAssoc) ? MULTINODE_XMLENDTAG :
  425. MULTINODE_XMLASSOCETAG1;
  426. // if no break event occured then only set the
  427. // xml result set
  428. if ( g_wmiCmd.GetBreakEvent() == FALSE )
  429. {
  430. if (bNotAssoc)
  431. {
  432. // Store the XML result set
  433. rParsedInfo.GetCmdSwitchesObject().
  434. SetXMLResultSet(bstrXML);
  435. bstrXML = L"";
  436. }
  437. }
  438. }
  439. // no instances
  440. else
  441. {
  442. bstrXML = L"<ERROR>";
  443. _bstr_t bstrMsg;
  444. WMIFormatMessage((bNotAssoc) ?
  445. IDS_I_NO_INSTANCES : IDS_I_NO_ASSOCIATIONS,
  446. 0, bstrMsg, NULL);
  447. if (bNotAssoc)
  448. {
  449. DisplayMessage((LPWSTR)bstrMsg, CP_OEMCP, TRUE, TRUE);
  450. }
  451. else
  452. {
  453. m_bNoAssoc = TRUE;
  454. }
  455. CHString sTemp;
  456. sTemp.Format(_T("<DESCRIPTION>%s</DESCRIPTION>"),
  457. (LPWSTR) bstrMsg);
  458. bstrXML += _bstr_t(sTemp);
  459. bstrXML += L"</ERROR>";
  460. if (bNotAssoc)
  461. {
  462. // Store the XML result set
  463. rParsedInfo.GetCmdSwitchesObject().
  464. SetXMLResultSet(bstrXML);
  465. bstrXML = L"";
  466. }
  467. }
  468. SAFEIRELEASE(pIEnum);
  469. }
  470. }
  471. catch(_com_error& e)
  472. {
  473. SAFEIRELEASE(pIObject);
  474. SAFEIRELEASE(pIEnum);
  475. SAFEBSTRFREE(bstrInstXML);
  476. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  477. hr = e.Error();
  478. }
  479. //trap for CHeap_Exception
  480. catch(CHeap_Exception)
  481. {
  482. SAFEIRELEASE(pIObject);
  483. SAFEIRELEASE(pIEnum);
  484. SAFEBSTRFREE(bstrInstXML);
  485. hr = WBEM_E_OUT_OF_MEMORY;
  486. _com_issue_error(hr);
  487. }
  488. return hr;
  489. }
  490. /*------------------------------------------------------------------------
  491. Name :ExecWMIMethod
  492. Synopsis :Executes a WMI method referring to the information
  493. available with CParsedInfo object.
  494. Type :Member Function
  495. Input Parameter(s):
  496. rParsedInfo - reference to CParsedInfo class object
  497. Output Parameter(s):
  498. rParsedInfo - reference to CParsedInfo class object
  499. Return Type :BOOL
  500. Global Variables :None
  501. Calling Syntax :ExecWMIMethod(rParsedInfo)
  502. Notes :None
  503. ------------------------------------------------------------------------*/
  504. BOOL CExecEngine::ExecWMIMethod(CParsedInfo& rParsedInfo)
  505. {
  506. HRESULT hr = S_OK;
  507. IWbemClassObject *pIClassObj = NULL,
  508. *pIInSign = NULL,
  509. *pIOutSign = NULL,
  510. *pIInParam = NULL;
  511. SAFEARRAY *psaNames = NULL;
  512. BSTR bstrInParam = NULL;
  513. BOOL bContinue = TRUE,
  514. bRet = TRUE,
  515. bMethodDtls = FALSE;
  516. CHString chsMsg;
  517. DWORD dwThreadId = GetCurrentThreadId();
  518. CHARVECTOR::iterator cviUnnamedValue = NULL;
  519. BSTRMAP::iterator bmiNamedValue = NULL;
  520. VARIANT varPut,
  521. varGet,
  522. varTemp;
  523. PROPDETMAP pdmPropDetMap;
  524. PROPDETMAP::iterator itrPropDetMap;
  525. VariantInit(&varPut);
  526. VariantInit(&varGet);
  527. VariantInit(&varTemp);
  528. METHDETMAP mdmMethDet;
  529. METHDETMAP::iterator mdmIterator = NULL;
  530. mdmMethDet = rParsedInfo.GetCmdSwitchesObject().GetMethDetMap();
  531. try
  532. {
  533. _bstr_t bstrClassName("");
  534. // Obtain the parameter details.
  535. if (!mdmMethDet.empty())
  536. {
  537. mdmIterator = mdmMethDet.begin();
  538. pdmPropDetMap = (*mdmIterator).second.Params;
  539. bMethodDtls = TRUE;
  540. }
  541. // Obtain the WMI class name
  542. // If <alias> is not specified
  543. if (rParsedInfo.GetCmdSwitchesObject().GetClassPath() != NULL)
  544. {
  545. bstrClassName = _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  546. GetClassPath());
  547. }
  548. // If <alias> specified
  549. else
  550. {
  551. rParsedInfo.GetCmdSwitchesObject().
  552. GetClassOfAliasTarget(bstrClassName);
  553. }
  554. // Obtain the object schema.
  555. hr = m_pITargetNS->GetObject(bstrClassName,
  556. WBEM_FLAG_USE_AMENDED_QUALIFIERS, NULL,
  557. &pIClassObj, NULL);
  558. if (m_bTrace || m_eloErrLogOpt)
  559. {
  560. chsMsg.Format(L"IWbemServices::GetObject(L\"%s\", 0, NULL, -)",
  561. (LPWSTR) bstrClassName);
  562. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  563. dwThreadId, rParsedInfo, m_bTrace);
  564. }
  565. ONFAILTHROWERROR(hr);
  566. // Get method information
  567. hr = pIClassObj->GetMethod(_bstr_t(rParsedInfo.GetCmdSwitchesObject()
  568. .GetMethodName()), 0, &pIInSign, &pIOutSign);
  569. if (m_bTrace || m_eloErrLogOpt)
  570. {
  571. chsMsg.Format(L"IWbemClassObject::GetMethod(L\"%s\", 0, -, -)",
  572. rParsedInfo.GetCmdSwitchesObject().GetMethodName() ?
  573. rParsedInfo.GetCmdSwitchesObject().GetMethodName()
  574. : L"<null>");
  575. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  576. dwThreadId, rParsedInfo, m_bTrace);
  577. }
  578. ONFAILTHROWERROR(hr);
  579. if ( pIInSign != NULL )
  580. {
  581. // Spawn object instance.
  582. hr = pIInSign->SpawnInstance(0, &pIInParam);
  583. if ( m_eloErrLogOpt )
  584. {
  585. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  586. _T("IWbemClassObject::SpawnInstance(0, -)"), dwThreadId,
  587. rParsedInfo, FALSE);
  588. }
  589. ONFAILTHROWERROR(hr);
  590. CHARVECTOR cvInParams =
  591. rParsedInfo.GetCmdSwitchesObject().GetPropertyList();
  592. BSTRMAP bmParameterMap =
  593. rParsedInfo.GetCmdSwitchesObject().GetParameterMap();
  594. // If parameter list is TRUE
  595. if (!cvInParams.empty() || !bmParameterMap.empty())
  596. {
  597. // Get the input paramters for this method from the input
  598. // signature object.
  599. hr = pIInSign->GetNames(NULL,
  600. WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
  601. NULL,
  602. &psaNames);
  603. if (m_bTrace || m_eloErrLogOpt)
  604. {
  605. chsMsg.Format(L"IWbemClassObject::GetNames(NULL, "
  606. L"WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, "
  607. L"NULL, -)");
  608. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  609. dwThreadId, rParsedInfo, m_bTrace);
  610. }
  611. ONFAILTHROWERROR(hr);
  612. LONG lLower = 0, lUpper = 0, lIndex = 0;
  613. hr = SafeArrayGetLBound(psaNames, 1, &lLower);
  614. if ( m_eloErrLogOpt )
  615. {
  616. chsMsg.Format(L"SafeArrayGetLBound(-, 1, -)");
  617. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  618. dwThreadId, rParsedInfo, FALSE);
  619. }
  620. ONFAILTHROWERROR(hr);
  621. hr = SafeArrayGetUBound(psaNames, 1, &lUpper);
  622. if ( m_eloErrLogOpt )
  623. {
  624. chsMsg.Format(L"SafeArrayGetUBound(-, 1, -)");
  625. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  626. dwThreadId, rParsedInfo, FALSE);
  627. }
  628. ONFAILTHROWERROR(hr);
  629. // Puting param values depend on named param list or not.
  630. BOOL bNamedParamList = rParsedInfo.GetCmdSwitchesObject().
  631. GetNamedParamListFlag();
  632. // Make necessary initializations.
  633. if ( bNamedParamList == FALSE)
  634. cviUnnamedValue = cvInParams.begin();
  635. lIndex = lLower;
  636. // Associate the parameter values specified to the input
  637. // parameters in the order available.
  638. while(TRUE)
  639. {
  640. // Breaking conditions.
  641. if ( lIndex > lUpper )
  642. break;
  643. if ( bNamedParamList == FALSE &&
  644. cviUnnamedValue == cvInParams.end())
  645. break;
  646. hr = SafeArrayGetElement(psaNames, &lIndex, &bstrInParam);
  647. if ( m_eloErrLogOpt )
  648. {
  649. chsMsg.Format(L"SafeArrayGetElement(-, -, -)");
  650. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  651. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  652. }
  653. ONFAILTHROWERROR(hr);
  654. // Obtain the property details
  655. PROPERTYDETAILS pdPropDet;
  656. GetPropertyAttributes(pIInParam, bstrInParam,
  657. pdPropDet, m_bTrace);
  658. _TCHAR* pszValue = NULL;
  659. if ( bNamedParamList == TRUE )
  660. {
  661. // If in parameter not found in named parameter map.
  662. if (!Find(bmParameterMap, bstrInParam, bmiNamedValue))
  663. {
  664. // If not found in alias verb parameters.
  665. if ( !Find(pdmPropDetMap, bstrInParam,
  666. itrPropDetMap) )
  667. {
  668. lIndex++;
  669. SAFEBSTRFREE(bstrInParam);
  670. continue;
  671. }
  672. else // If found in alias verb parameters.
  673. {
  674. // Value should be taken from Default of alias
  675. // verb parameters.
  676. if (!((*itrPropDetMap).second.Default))
  677. {
  678. lIndex++;
  679. SAFEBSTRFREE(bstrInParam);
  680. continue;
  681. }
  682. else
  683. pszValue = (*itrPropDetMap).second.Default;
  684. }
  685. }
  686. else
  687. pszValue = (*bmiNamedValue).second;
  688. }
  689. else
  690. pszValue = *cviUnnamedValue;
  691. if (rParsedInfo.GetCmdSwitchesObject().
  692. GetAliasName() == NULL)
  693. {
  694. // Check the parameter value supplied against
  695. // the qualifier information for the parameter.
  696. bRet = CheckQualifierInfo(rParsedInfo, pIInSign,
  697. bstrInParam, pszValue);
  698. }
  699. else
  700. {
  701. // If method and parameter information is available
  702. if (bMethodDtls && !pdmPropDetMap.empty())
  703. {
  704. bRet = CheckAliasQualifierInfo(rParsedInfo,
  705. bstrInParam, pszValue, pdmPropDetMap);
  706. }
  707. }
  708. // The parameter value does not fit into the qualifier
  709. // allowed values.
  710. if (!bRet)
  711. {
  712. bContinue = FALSE;
  713. break;
  714. }
  715. if(!IsArrayType(pIInParam, bstrInParam))
  716. {
  717. VariantInit(&varTemp);
  718. varTemp.vt = VT_BSTR;
  719. varTemp.bstrVal = SysAllocString(pszValue);
  720. if (varTemp.bstrVal == NULL)
  721. {
  722. //Reset the variant, it will be cleaned up by the catch...
  723. VariantInit(&varTemp);
  724. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  725. }
  726. VariantInit(&varPut);
  727. if(_tcslen(pszValue) > 0){
  728. hr = ConvertCIMTYPEToVarType(varPut, varTemp,
  729. (_TCHAR*)pdPropDet.Type);
  730. } else {
  731. hr = VariantChangeType(&varPut, &varTemp, 0, VT_NULL);
  732. }
  733. if ( m_eloErrLogOpt )
  734. {
  735. chsMsg.Format(L"VariantChangeType(-, -, 0, -)");
  736. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  737. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  738. }
  739. ONFAILTHROWERROR(hr);
  740. }
  741. else
  742. {
  743. BSTRVECTOR vArrayValues;
  744. RemoveParanthesis(pszValue);
  745. GetArrayFromToken(pszValue, vArrayValues);
  746. hr = CheckForArray( pIInParam, bstrInParam,
  747. varPut, vArrayValues, rParsedInfo);
  748. ONFAILTHROWERROR(hr);
  749. }
  750. hr = pIInParam->Put(bstrInParam, 0, &varPut, 0);
  751. if (m_bTrace || m_eloErrLogOpt)
  752. {
  753. chsMsg.Format(L"IWbemClassObject::Put(L\"%s\", 0,"
  754. L"-, 0)", (LPWSTR) bstrInParam);
  755. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  756. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, m_bTrace);
  757. }
  758. ONFAILTHROWERROR(hr);
  759. VARIANTCLEAR(varPut);
  760. VARIANTCLEAR(varGet);
  761. VARIANTCLEAR(varTemp);
  762. SAFEBSTRFREE(bstrInParam);
  763. // Looping statements.
  764. if ( bNamedParamList == FALSE )
  765. cviUnnamedValue++;
  766. lIndex++;
  767. }
  768. // Free the memory
  769. SAFEADESTROY(psaNames);
  770. if (bContinue)
  771. {
  772. // If insufficient parameters are specified.
  773. if ( bNamedParamList == FALSE &&
  774. cviUnnamedValue != cvInParams.end() )
  775. {
  776. bContinue = FALSE;
  777. rParsedInfo.GetCmdSwitchesObject().SetErrataCode(
  778. IDS_E_INVALID_NO_OF_PARAMS);
  779. bRet = FALSE;
  780. }
  781. }
  782. }
  783. }
  784. else // No input parameters are available for this function.
  785. {
  786. // If unnamed parameters are specified.
  787. if (!rParsedInfo.GetCmdSwitchesObject().GetPropertyList().empty())
  788. {
  789. bContinue = FALSE;
  790. rParsedInfo.GetCmdSwitchesObject().SetErrataCode(
  791. IDS_E_METHOD_HAS_NO_IN_PARAMS);
  792. bRet = FALSE;
  793. }
  794. }
  795. SAFEIRELEASE(pIInSign);
  796. SAFEIRELEASE(pIOutSign);
  797. SAFEIRELEASE(pIClassObj);
  798. if (bContinue)
  799. {
  800. hr = FormQueryAndExecuteMethodOrUtility(rParsedInfo, pIInParam);
  801. ONFAILTHROWERROR(hr);
  802. }
  803. }
  804. catch(_com_error& e)
  805. {
  806. // Free the interface pointers and memory allocated.
  807. SAFEIRELEASE(pIClassObj);
  808. SAFEIRELEASE(pIInSign);
  809. SAFEIRELEASE(pIOutSign);
  810. SAFEIRELEASE(pIInParam);
  811. SAFEADESTROY(psaNames);
  812. SAFEBSTRFREE(bstrInParam);
  813. VARIANTCLEAR(varPut);
  814. VARIANTCLEAR(varGet);
  815. VARIANTCLEAR(varTemp);
  816. // Store the COM error object and set the return value to FALSE
  817. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  818. bRet = FALSE;
  819. }
  820. catch(CHeap_Exception)
  821. {
  822. SAFEIRELEASE(pIClassObj);
  823. SAFEIRELEASE(pIInSign);
  824. SAFEIRELEASE(pIOutSign);
  825. SAFEIRELEASE(pIInParam);
  826. SAFEADESTROY(psaNames);
  827. SAFEBSTRFREE(bstrInParam);
  828. VARIANTCLEAR(varPut);
  829. VARIANTCLEAR(varGet);
  830. VARIANTCLEAR(varTemp);
  831. hr = WBEM_E_OUT_OF_MEMORY;
  832. _com_issue_error(hr);
  833. }
  834. return bRet;
  835. }
  836. /*------------------------------------------------------------------------
  837. Name :CheckForArray
  838. Synopsis :Check for the parameter of array type. If it is of
  839. array type then creates a safearray and fills the
  840. values passed in it.
  841. Type :Member Function
  842. Input Parameter(s):
  843. pIObj - pointer to IWbemClassObject object
  844. bstrProp - property name
  845. varDest - VARIANT Destination
  846. vArrayValues - Input elements in Array for Array parameter
  847. rParsedInfo - reference to CParsedInfo class object
  848. Output Parameter(s): None
  849. Return Type :HRESULT
  850. Global Variables :None
  851. Calling Syntax :CheckForArray( pIInParam, bstrInParam,
  852. varPut, varTemp, rParsedInfo)
  853. Notes :None
  854. ------------------------------------------------------------------------*/
  855. HRESULT CExecEngine::CheckForArray(IWbemClassObject* pIObj,
  856. BSTR bstrProp, VARIANT& varDest,
  857. BSTRVECTOR& vArrayValues,CParsedInfo& rParsedInfo)
  858. {
  859. HRESULT hr = S_OK;
  860. IWbemQualifierSet* pIQualSet = NULL;
  861. VARIANT vtType, vtTypeProp, vtTemp, varSrc;
  862. CIMTYPE ctCimType;
  863. CHString chsMsg;
  864. DWORD dwThreadId = GetCurrentThreadId();
  865. SAFEARRAY* pSa = NULL;
  866. VariantInit(&vtType);
  867. VariantInit(&vtTypeProp);
  868. VariantInit(&vtTemp);
  869. VariantInit(&varSrc);
  870. try
  871. {
  872. // Obtain the property qualifier set for the property
  873. hr = pIObj->GetPropertyQualifierSet(bstrProp, &pIQualSet);
  874. if ( pIQualSet != NULL )
  875. {
  876. // Obtain the CIM type of the property
  877. hr = pIQualSet->Get(_bstr_t(L"CIMTYPE"), 0L, &vtType, NULL);
  878. if (SUCCEEDED(hr))
  879. {
  880. if ( vtType.vt == VT_BSTR )
  881. {
  882. // Obtain the CIM type of the property
  883. hr = pIObj->Get(bstrProp, 0L, &vtTypeProp, &ctCimType, NULL);
  884. if (SUCCEEDED(hr))
  885. {
  886. if ( ctCimType & VT_ARRAY )
  887. {
  888. WMICLIINT nSize = vArrayValues.size();
  889. SAFEARRAYBOUND pSab[1];
  890. pSab[0].lLbound = 0;
  891. pSab[0].cElements = nSize;
  892. VARTYPE vt = ReturnVarType(vtType.bstrVal);
  893. if(vt != VT_NULL && vt != VT_BYREF && vt != VT_EMPTY)
  894. {
  895. pSa = SafeArrayCreate(vt, 1, pSab);
  896. }
  897. if(pSa != NULL)
  898. {
  899. BOOL bError = FALSE;
  900. for(WMICLIINT i = 0; i < nSize; i++)
  901. {
  902. VariantInit(&varSrc);
  903. VariantInit(&vtTemp);
  904. varSrc.vt = VT_BSTR;
  905. varSrc.bstrVal = SysAllocString(vArrayValues[i]);
  906. if (varSrc.bstrVal == NULL)
  907. {
  908. //Reset the variant, it will be cleaned up by the catch...
  909. VariantInit(&varSrc);
  910. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  911. }
  912. hr = ConvertCIMTYPEToVarType ( vtTemp, varSrc, vtType.bstrVal );
  913. if ( m_eloErrLogOpt )
  914. {
  915. chsMsg.Format(L"VariantChangeType(-, -, 0, -)");
  916. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  917. (LPCWSTR)chsMsg, dwThreadId,
  918. rParsedInfo, FALSE);
  919. }
  920. ONFAILTHROWERROR(hr);
  921. long lIndex[] = {i};
  922. if ( V_VT ( & vtTemp ) == VT_NULL )
  923. {
  924. hr = SafeArrayPutElement(pSa, lIndex, NULL);
  925. }
  926. else
  927. {
  928. VARTYPE vt = ReturnVarType(vtType.bstrVal);
  929. if(vt == VT_BSTR )
  930. hr = SafeArrayPutElement(pSa, lIndex,
  931. vtTemp.bstrVal);
  932. else if(vt == VT_I2 )
  933. hr = SafeArrayPutElement(pSa, lIndex,
  934. &vtTemp.iVal);
  935. else if(vt == VT_I4 )
  936. hr = SafeArrayPutElement(pSa, lIndex,
  937. &vtTemp.lVal);
  938. else if(vt == VT_R4 )
  939. hr = SafeArrayPutElement(pSa, lIndex,
  940. &vtTemp.fltVal);
  941. else if(vt == VT_R8 )
  942. hr = SafeArrayPutElement(pSa, lIndex,
  943. &vtTemp.dblVal);
  944. else if(vt == VT_BOOL )
  945. hr = SafeArrayPutElement(pSa, lIndex,
  946. &vtTemp.boolVal);
  947. else if(vt == VT_DISPATCH )
  948. hr = SafeArrayPutElement(pSa, lIndex,
  949. vtTemp.pdispVal);
  950. else if(vt == VT_INT )
  951. hr = SafeArrayPutElement(pSa, lIndex,
  952. &vtTemp.intVal);
  953. else if(vt == VT_UI1 )
  954. hr = SafeArrayPutElement(pSa, lIndex,
  955. &vtTemp.bVal);
  956. else if(vt == VT_DATE )
  957. hr = SafeArrayPutElement(pSa, lIndex,
  958. &vtTemp.date);
  959. else
  960. {
  961. bError = TRUE;
  962. }
  963. }
  964. VARIANTCLEAR(vtTemp);
  965. VARIANTCLEAR(varSrc);
  966. if(!bError)
  967. {
  968. if ( m_eloErrLogOpt )
  969. {
  970. chsMsg.Format(L"SafeArrayPutElement(-, -, -)");
  971. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  972. (LPCWSTR)chsMsg, dwThreadId,
  973. rParsedInfo, FALSE);
  974. }
  975. ONFAILTHROWERROR(hr);
  976. }
  977. else
  978. {
  979. SAFEADESTROY(pSa);
  980. break;
  981. }
  982. }
  983. if(!bError)
  984. {
  985. varDest.parray = pSa;
  986. varDest.vt = vt | VT_ARRAY;
  987. }
  988. }
  989. }
  990. VARIANTCLEAR(vtTypeProp);
  991. }
  992. }
  993. hr = S_OK;
  994. VARIANTCLEAR(vtType);
  995. }
  996. SAFEIRELEASE(pIQualSet);
  997. }
  998. }
  999. catch(_com_error& e)
  1000. {
  1001. VARIANTCLEAR(vtType);
  1002. VARIANTCLEAR(vtTemp);
  1003. VARIANTCLEAR(varSrc);
  1004. VARIANTCLEAR(vtTypeProp);
  1005. SAFEIRELEASE(pIQualSet);
  1006. SAFEADESTROY(pSa);
  1007. hr = e.Error();
  1008. }
  1009. catch(CHeap_Exception)
  1010. {
  1011. VARIANTCLEAR(vtType);
  1012. VARIANTCLEAR(vtTemp);
  1013. VARIANTCLEAR(varSrc);
  1014. VARIANTCLEAR(vtTypeProp);
  1015. SAFEIRELEASE(pIQualSet);
  1016. SAFEADESTROY(pSa);
  1017. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1018. }
  1019. return hr;
  1020. }
  1021. /*------------------------------------------------------------------------
  1022. Name :IsArrayType
  1023. Synopsis :Check for array type
  1024. Type :Member Function
  1025. Input Parameter(s):
  1026. pIObj - pointer to IWbemClassObject object
  1027. bstrProp - property name
  1028. Output Parameter(s): None
  1029. Return Type :BOOL
  1030. Global Variables :None
  1031. Calling Syntax :IsArrayType(pIInParam, bstrInParam)
  1032. Notes :None
  1033. ------------------------------------------------------------------------*/
  1034. BOOL CExecEngine::IsArrayType( IWbemClassObject* pIObj,
  1035. BSTR bstrProp)
  1036. {
  1037. HRESULT hr = S_OK;
  1038. IWbemQualifierSet* pIQualSet = NULL;
  1039. VARIANT vtType, vtTypeProp;
  1040. CIMTYPE ctCimType;
  1041. BOOL bRet = FALSE;
  1042. VariantInit(&vtType);
  1043. VariantInit(&vtTypeProp);
  1044. try
  1045. {
  1046. // Obtain the property qualifier set for the property
  1047. hr = pIObj->GetPropertyQualifierSet(bstrProp, &pIQualSet);
  1048. if ( pIQualSet != NULL )
  1049. {
  1050. // Obtain the CIM type of the property
  1051. hr = pIQualSet->Get(_bstr_t(L"CIMTYPE"), 0L, &vtType, NULL);
  1052. if (SUCCEEDED(hr))
  1053. {
  1054. if ( vtType.vt == VT_BSTR )
  1055. {
  1056. // Obtain the CIM type of the property
  1057. hr = pIObj->Get(bstrProp, 0L, &vtTypeProp, &ctCimType, NULL);
  1058. if (SUCCEEDED(hr))
  1059. {
  1060. if ( ctCimType & VT_ARRAY )
  1061. {
  1062. bRet = TRUE;
  1063. }
  1064. VARIANTCLEAR(vtTypeProp);
  1065. }
  1066. }
  1067. VARIANTCLEAR(vtType);
  1068. }
  1069. SAFEIRELEASE(pIQualSet);
  1070. }
  1071. }
  1072. catch(_com_error& e)
  1073. {
  1074. VARIANTCLEAR(vtType);
  1075. VARIANTCLEAR(vtTypeProp);
  1076. SAFEIRELEASE(pIQualSet);
  1077. }
  1078. return bRet;
  1079. }
  1080. /*------------------------------------------------------------------------
  1081. Name :GetArrayFromToken
  1082. Synopsis :Separates comma delimited string and fill the values
  1083. in an array
  1084. Type :Member Function
  1085. Input Parameter(s):
  1086. pszValue - Comma separated array elements
  1087. Output Parameter(s):
  1088. vArrayValues - Fills with array elements
  1089. Return Type :void
  1090. Global Variables :None
  1091. Calling Syntax :GetArrayFromToken(pszValue, vArrayValues)
  1092. Notes :None
  1093. ------------------------------------------------------------------------*/
  1094. void CExecEngine::GetArrayFromToken(_TCHAR* pszValue,
  1095. BSTRVECTOR& vArrayValues)
  1096. {
  1097. if(pszValue == NULL)
  1098. return;
  1099. // copy contents into local folder to stop original string changes because
  1100. // in case of multiple instance operations original string need to be called
  1101. // again and again
  1102. // i.e. \0 is inserted by call to _tcstok
  1103. _TCHAR* pszValueDup = new _TCHAR[lstrlen(pszValue)+1];
  1104. if(pszValueDup)
  1105. {
  1106. lstrcpy(pszValueDup, pszValue);
  1107. _TCHAR* pszToken = _tcstok(pszValueDup, CLI_TOKEN_COMMA);
  1108. if(pszToken != NULL)
  1109. {
  1110. TrimBlankSpaces(pszToken);
  1111. UnQuoteString(pszToken);
  1112. vArrayValues.push_back(_bstr_t(pszToken));
  1113. }
  1114. while (pszToken != NULL)
  1115. {
  1116. pszToken = _tcstok(NULL, CLI_TOKEN_COMMA);
  1117. if (pszToken != NULL)
  1118. {
  1119. TrimBlankSpaces(pszToken);
  1120. UnQuoteString(pszToken);
  1121. vArrayValues.push_back(_bstr_t(pszToken));
  1122. }
  1123. }
  1124. SAFEDELETE(pszValueDup);
  1125. }
  1126. }
  1127. /*------------------------------------------------------------------------
  1128. Name :ProcessSHOWInfo
  1129. Synopsis :Executed the functionality requested by GET|LIST verb
  1130. referring to the information available with
  1131. CParsedInfo object or to display help in interactive mode
  1132. by displaying properties of concernrd instance.
  1133. Type :Member Function
  1134. Input Parameter(s):
  1135. rParsedInfo - reference to CParsedInfo class object
  1136. bVerb - Verb or interactive info
  1137. pszPath - the Path expression
  1138. Output Parameter(s):
  1139. rParsedInfo - reference to CParsedInfo class object
  1140. Return Type :BOOL
  1141. Global Variables :None
  1142. Calling Syntax :ProcessSHOWInfo(rParsedInfo, bVerb, pszPath)
  1143. Notes :None
  1144. ------------------------------------------------------------------------*/
  1145. BOOL CExecEngine::ProcessSHOWInfo(CParsedInfo& rParsedInfo, BOOL bVerb,
  1146. _TCHAR* pszPath)
  1147. {
  1148. HRESULT hr = S_OK;
  1149. BOOL bPropList = FALSE,
  1150. bRet = TRUE,
  1151. bSysProp = FALSE;
  1152. _TCHAR *pszWhereExpr = NULL,
  1153. *pszClassPath = NULL;
  1154. CHARVECTOR::iterator theIterator = NULL,
  1155. theEndIterator = NULL;
  1156. try
  1157. {
  1158. _bstr_t bstrPropList(""), bstrClassName(""),
  1159. bstrQuery(""), bstrXML("");
  1160. //Formation of query only once , useful in case /every is specified.
  1161. //if(rParsedInfo.GetCmdSwitchesObject().GetFirstQueryFormFlag() )
  1162. if(rParsedInfo.GetCmdSwitchesObject().GetFormedQuery() == NULL
  1163. || !bVerb)
  1164. {
  1165. // Obtain the list of properties to be retrieved
  1166. if(bVerb)
  1167. {
  1168. theIterator = rParsedInfo.GetCmdSwitchesObject().
  1169. GetPropertyList().begin();
  1170. theEndIterator = rParsedInfo.GetCmdSwitchesObject().
  1171. GetPropertyList().end();
  1172. }
  1173. else
  1174. {
  1175. theIterator = rParsedInfo.GetCmdSwitchesObject().
  1176. GetInteractivePropertyList().begin();
  1177. theEndIterator = rParsedInfo.GetCmdSwitchesObject().
  1178. GetInteractivePropertyList().end();
  1179. }
  1180. // Loop thru the list of properties specified,form comma seaprated
  1181. // string of the properties i.e prop1, prop2, prop3, ....., propn
  1182. while (theIterator != theEndIterator)
  1183. {
  1184. // Set the bPropList to TRUE
  1185. bPropList = TRUE;
  1186. bstrPropList += _bstr_t(*theIterator);
  1187. // If the system properties flag is not set to true
  1188. if (!bSysProp)
  1189. bSysProp = IsSysProp(*theIterator);
  1190. // Move to next property
  1191. theIterator++;
  1192. if (theIterator != theEndIterator)
  1193. bstrPropList += _bstr_t(L", ");
  1194. };
  1195. // If properties are not specified, then by default retrieve all
  1196. // the properties. i.e '*'
  1197. if (!bPropList)
  1198. bstrPropList = ASTERIX;
  1199. // Obtain the alias target class
  1200. rParsedInfo.GetCmdSwitchesObject().
  1201. GetClassOfAliasTarget(bstrClassName);
  1202. // Obtain the class path
  1203. pszClassPath = rParsedInfo.GetCmdSwitchesObject().GetClassPath();
  1204. BOOL bClass = FALSE;
  1205. if(bVerb)
  1206. {
  1207. if(IsClassOperation(rParsedInfo))
  1208. {
  1209. bClass = TRUE;
  1210. }
  1211. }
  1212. // If CLASS | PATH expression is specified.
  1213. if ( pszClassPath != NULL)
  1214. {
  1215. if (bVerb && bClass)
  1216. {
  1217. bstrQuery = _bstr_t(L"SELECT * FROM") +
  1218. _bstr_t(" meta_class ");
  1219. }
  1220. else
  1221. bstrQuery = _bstr_t(L"SELECT ") + bstrPropList +
  1222. _bstr_t(" FROM ") + _bstr_t(pszClassPath);
  1223. }
  1224. else
  1225. {
  1226. bstrQuery = _bstr_t("SELECT ") + bstrPropList +
  1227. _bstr_t(" FROM ") + bstrClassName;
  1228. }
  1229. if(bVerb)
  1230. {
  1231. if (bClass)
  1232. {
  1233. _TCHAR pszWhere[MAX_BUFFER] = NULL_STRING;
  1234. lstrcpy(pszWhere, _T("__Class = \""));
  1235. lstrcat(pszWhere, pszClassPath);
  1236. lstrcat(pszWhere, _T("\""));
  1237. pszWhereExpr = pszWhere;
  1238. }
  1239. else
  1240. pszWhereExpr = rParsedInfo.GetCmdSwitchesObject().
  1241. GetWhereExpression();
  1242. }
  1243. else if(pszPath)
  1244. {
  1245. _TCHAR pszWhere[MAX_BUFFER] = NULL_STRING;
  1246. bRet = ExtractClassNameandWhereExpr(pszPath,
  1247. rParsedInfo, pszWhere);
  1248. if(bRet)
  1249. pszWhereExpr = pszWhere;
  1250. }
  1251. if(pszWhereExpr)
  1252. {
  1253. bstrQuery += _bstr_t(" WHERE ") + _bstr_t(pszWhereExpr);
  1254. }
  1255. rParsedInfo.GetCmdSwitchesObject().SetFormedQuery(bstrQuery);
  1256. rParsedInfo.GetCmdSwitchesObject().SetSysPropFlag(bSysProp);
  1257. }
  1258. else
  1259. {
  1260. bstrQuery = rParsedInfo.GetCmdSwitchesObject().GetFormedQuery();
  1261. bSysProp = rParsedInfo.GetCmdSwitchesObject().GetSysPropFlag();
  1262. }
  1263. // Create the object of IWbemObjectTextSrc interface.
  1264. if (m_pITextSrc == NULL)
  1265. hr = CreateWMIXMLTextSrc(rParsedInfo);
  1266. if (SUCCEEDED(hr))
  1267. {
  1268. // Connect to WMI namespace
  1269. if (m_pITargetNS == NULL)
  1270. {
  1271. if ( IsFailFastAndNodeExist(rParsedInfo) == TRUE )
  1272. {
  1273. hr = ConnectToTargetNS(rParsedInfo);
  1274. ONFAILTHROWERROR(hr);
  1275. }
  1276. else
  1277. hr = E_FAIL;
  1278. }
  1279. if (SUCCEEDED(hr))
  1280. {
  1281. // Obtain the XML Result set.
  1282. hr = ObtainXMLResultSet(bstrQuery, rParsedInfo,
  1283. bstrXML, bSysProp, TRUE);
  1284. }
  1285. if(!bVerb)
  1286. {
  1287. BOOL bRet = g_wmiCmd.GetFormatObject().
  1288. DisplayResults(rParsedInfo, TRUE);
  1289. rParsedInfo.GetCmdSwitchesObject().FreeCOMError();
  1290. rParsedInfo.GetCmdSwitchesObject().SetErrataCode(0);
  1291. rParsedInfo.GetCmdSwitchesObject().SetInformationCode(0);
  1292. }
  1293. }
  1294. bRet = FAILED(hr) ? FALSE : TRUE;
  1295. }
  1296. catch(_com_error& e)
  1297. {
  1298. bRet = FALSE;
  1299. _com_issue_error(e.Error());
  1300. }
  1301. return bRet;
  1302. }
  1303. /*------------------------------------------------------------------------
  1304. Name :ProcessCALLVerb
  1305. Synopsis :Processes the CALL verb request referring to the
  1306. information available with CParsedInfo object.
  1307. Type :Member Function
  1308. Input Parameter(s):
  1309. rParsedInfo - reference to CParsedInfo class object
  1310. Output Parameter(s):
  1311. rParsedInfo - reference to CParsedInfo class object
  1312. Return Type :BOOL
  1313. Global Variables :None
  1314. Calling Syntax :ProcessCALLVerb(rParsedInfo)
  1315. Notes :None
  1316. ------------------------------------------------------------------------*/
  1317. BOOL CExecEngine::ProcessCALLVerb(CParsedInfo& rParsedInfo)
  1318. {
  1319. HRESULT hr = S_OK;
  1320. BOOL bRet = TRUE;
  1321. try
  1322. {
  1323. // Connect to WMI namespace
  1324. if (m_pITargetNS == NULL)
  1325. {
  1326. if ( IsFailFastAndNodeExist(rParsedInfo) == TRUE )
  1327. hr = ConnectToTargetNS(rParsedInfo);
  1328. else
  1329. bRet = FALSE;
  1330. ONFAILTHROWERROR(hr);
  1331. }
  1332. if ( bRet == TRUE )
  1333. {
  1334. // Check for the verb type, so as to handle lauching of other
  1335. // commandline utilities from the shell.
  1336. if ( rParsedInfo.GetCmdSwitchesObject().GetVerbType() == CMDLINE )
  1337. {
  1338. if (!ExecOtherCmdLineUtlty(rParsedInfo))
  1339. bRet = FALSE;
  1340. }
  1341. else
  1342. {
  1343. if (!ExecWMIMethod(rParsedInfo))
  1344. bRet = FALSE;
  1345. }
  1346. }
  1347. }
  1348. catch(_com_error& e)
  1349. {
  1350. // Store the COM error and set the return value to FALSE
  1351. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  1352. bRet = FALSE;
  1353. }
  1354. return bRet;
  1355. }
  1356. /*------------------------------------------------------------------------
  1357. Name :ProcessASSOCVerb
  1358. Synopsis :Processes the ASSOC verb request referring to the
  1359. information available with CParsedInfo object.
  1360. Type :Member Function
  1361. Input Parameter(s):
  1362. rParsedInfo - reference to CParsedInfo class object
  1363. Output Parameter(s):
  1364. rParsedInfo - reference to CParsedInfo class object
  1365. Return Type :BOOL
  1366. Global Variables :None
  1367. Calling Syntax :ProcessASSOCVerb(rParsedInfo)
  1368. Notes :None
  1369. ------------------------------------------------------------------------*/
  1370. BOOL CExecEngine::ProcessASSOCVerb(CParsedInfo& rParsedInfo)
  1371. {
  1372. HRESULT hr = S_OK;
  1373. BOOL bRet = TRUE;
  1374. WMICLIINT nReqType = 0;
  1375. BOOL bSwitches = FALSE,
  1376. bClass = TRUE,
  1377. bInstances = FALSE;
  1378. IEnumWbemClassObject *pIEnumObj = NULL;
  1379. IWbemClassObject *pIWbemObj = NULL;
  1380. VARIANT varPath;
  1381. VariantInit(&varPath);
  1382. try
  1383. {
  1384. _bstr_t bstrClassName(""), bstrQuery(""), bstrAssocWhere(""),
  1385. bstrResult(""), bstrXML(""), bstrAggResult("");
  1386. bstrAggResult = MULTINODE_XMLSTARTTAG;
  1387. //If assoc switches are specified, bSwitches is set and correspondingly
  1388. //assoc where clause is framed
  1389. bSwitches =((rParsedInfo.GetCmdSwitchesObject().GetResultClassName())||
  1390. (rParsedInfo.GetCmdSwitchesObject().GetResultRoleName()) ||
  1391. (rParsedInfo.GetCmdSwitchesObject().GetAssocClassName()));
  1392. if(bSwitches)
  1393. {
  1394. bstrAssocWhere += _bstr_t(" WHERE ");
  1395. if((rParsedInfo.GetCmdSwitchesObject().GetResultClassName())
  1396. != NULL )
  1397. {
  1398. bstrAssocWhere += _bstr_t(L" ResultClass = ") +
  1399. _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  1400. GetResultClassName());
  1401. }
  1402. if((rParsedInfo.GetCmdSwitchesObject().GetResultRoleName())
  1403. != NULL)
  1404. {
  1405. bstrAssocWhere += _bstr_t(L" ResultRole = ") +
  1406. _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  1407. GetResultRoleName());
  1408. }
  1409. if((rParsedInfo.GetCmdSwitchesObject().GetAssocClassName())
  1410. != NULL)
  1411. {
  1412. bstrAssocWhere += _bstr_t(L" AssocClass = ") +
  1413. _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  1414. GetAssocClassName());
  1415. }
  1416. }
  1417. //NOTE: nReqType = 2 implies that first get all instances and then
  1418. // find associations for each instance
  1419. // If PATH is specified
  1420. if (rParsedInfo.GetCmdSwitchesObject().GetPathExpression() != NULL)
  1421. {
  1422. // If PATH is specified (with key expression).
  1423. if (!rParsedInfo.GetCmdSwitchesObject().
  1424. GetExplicitWhereExprFlag())
  1425. {
  1426. if (rParsedInfo.GetCmdSwitchesObject().
  1427. GetWhereExpression() == NULL)
  1428. {
  1429. nReqType = 2;
  1430. }
  1431. else
  1432. {
  1433. nReqType = 1;
  1434. bstrQuery = _bstr_t(L"ASSOCIATORS OF {")
  1435. + _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  1436. .GetPathExpression()
  1437. + _bstr_t("}"));
  1438. }
  1439. }
  1440. else
  1441. nReqType = 2;
  1442. }
  1443. // If CLASS expression is specified.
  1444. //associators of the class need to be displayed
  1445. if (rParsedInfo.GetCmdSwitchesObject().GetClassPath() != NULL
  1446. && rParsedInfo.GetCmdSwitchesObject().
  1447. GetPathExpression() == NULL)
  1448. {
  1449. nReqType = 1;
  1450. bstrQuery = _bstr_t(L"ASSOCIATORS OF {")
  1451. + _bstr_t(rParsedInfo.GetCmdSwitchesObject().GetClassPath())
  1452. + _bstr_t("}");
  1453. if (!bSwitches)
  1454. bstrQuery += _bstr_t(L" WHERE SchemaOnly");
  1455. else
  1456. bstrQuery += bstrAssocWhere + _bstr_t(L" SchemaOnly");
  1457. }
  1458. // Check for <alias> or alias and path without keyclause
  1459. if (nReqType != 1)
  1460. {
  1461. // Obtain the alias target class
  1462. if(rParsedInfo.GetCmdSwitchesObject().GetAliasName() != NULL)
  1463. {
  1464. rParsedInfo.GetCmdSwitchesObject().GetClassOfAliasTarget(
  1465. bstrClassName);
  1466. }
  1467. else
  1468. bstrClassName = _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  1469. GetClassPath());
  1470. //obtain the instances corresponding to the alias target class
  1471. bstrQuery = _bstr_t(L"SELECT * FROM ") + bstrClassName;
  1472. //if pwhere expression is specified or where is specified
  1473. if (rParsedInfo.GetCmdSwitchesObject().
  1474. GetWhereExpression() != NULL)
  1475. {
  1476. bstrQuery += _bstr_t(" WHERE ") +_bstr_t(rParsedInfo.
  1477. GetCmdSwitchesObject().GetWhereExpression());
  1478. }
  1479. nReqType = 2;
  1480. }
  1481. // Create the object of IWbemObjectTextSrc interface.
  1482. if (m_pITextSrc == NULL)
  1483. hr = CreateWMIXMLTextSrc(rParsedInfo);
  1484. if (SUCCEEDED(hr))
  1485. {
  1486. // Connect to WMI namespace
  1487. if (m_pITargetNS == NULL)
  1488. {
  1489. if ( IsFailFastAndNodeExist(rParsedInfo) == TRUE )
  1490. {
  1491. hr = ConnectToTargetNS(rParsedInfo);
  1492. ONFAILTHROWERROR(hr);
  1493. }
  1494. else
  1495. hr = E_FAIL; // Explicitly set error
  1496. }
  1497. if (SUCCEEDED(hr))
  1498. {
  1499. if(nReqType != 2)
  1500. {
  1501. // Obtain the XML Result Set.
  1502. hr = ObtainXMLResultSet(bstrQuery, rParsedInfo, bstrXML,
  1503. TRUE, FALSE);
  1504. ONFAILTHROWERROR(hr);
  1505. if (m_bNoAssoc)
  1506. {
  1507. _bstr_t bstrMsg;
  1508. WMIFormatMessage(IDS_I_NO_ASSOC, 0, bstrMsg, NULL);
  1509. DisplayMessage((LPWSTR)bstrMsg, CP_OEMCP, TRUE, TRUE);
  1510. m_bNoAssoc = FALSE;
  1511. }
  1512. if (rParsedInfo.GetCmdSwitchesObject().
  1513. GetPathExpression() == NULL)
  1514. {
  1515. bClass = TRUE;
  1516. hr = FrameAssocHeader(rParsedInfo.
  1517. GetCmdSwitchesObject().GetClassPath(),
  1518. bstrResult, bClass);
  1519. ONFAILTHROWERROR(hr);
  1520. }
  1521. else
  1522. {
  1523. bClass = FALSE;
  1524. hr = FrameAssocHeader(
  1525. rParsedInfo.GetCmdSwitchesObject()
  1526. .GetPathExpression(), bstrResult, bClass);
  1527. ONFAILTHROWERROR(hr);
  1528. }
  1529. bstrResult += bstrXML;
  1530. bstrResult += (bClass) ? L"</CLASS>" : L"</INSTANCE>";
  1531. bstrAggResult += bstrResult;
  1532. }
  1533. else
  1534. {
  1535. // Set the class flag to FALSE
  1536. bClass = FALSE;
  1537. ULONG ulReturned = 0;
  1538. CHString chsMsg;
  1539. DWORD dwThreadId =
  1540. GetCurrentThreadId();
  1541. VariantInit(&varPath);
  1542. try
  1543. {
  1544. //enumerate the instances
  1545. hr = m_pITargetNS->ExecQuery(_bstr_t(L"WQL"),
  1546. bstrQuery,
  1547. WBEM_FLAG_FORWARD_ONLY |
  1548. WBEM_FLAG_RETURN_IMMEDIATELY,
  1549. NULL, &pIEnumObj);
  1550. if (m_bTrace || m_eloErrLogOpt)
  1551. {
  1552. chsMsg.Format(L"IWbemServices::ExecQuery(L\"WQL\","
  1553. L"L\"%s\", 0, NULL, -)",
  1554. (LPWSTR)bstrQuery);
  1555. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  1556. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo,
  1557. m_bTrace);
  1558. }
  1559. ONFAILTHROWERROR(hr);
  1560. // Set the interface security
  1561. hr = SetSecurity(pIEnumObj,
  1562. rParsedInfo.GetAuthorityPrinciple(),
  1563. rParsedInfo.GetNode(),
  1564. rParsedInfo.GetUser(),
  1565. rParsedInfo.GetPassword(),
  1566. rParsedInfo.GetGlblSwitchesObject().
  1567. GetAuthenticationLevel(),
  1568. rParsedInfo.GetGlblSwitchesObject().
  1569. GetImpersonationLevel());
  1570. if (m_bTrace || m_eloErrLogOpt)
  1571. {
  1572. _TCHAR* pszAuthority = rParsedInfo.GetAuthorityPrinciple();
  1573. if( pszAuthority != NULL &&
  1574. _tcslen(pszAuthority) > 9 &&
  1575. _tcsnicmp(pszAuthority, _T("KERBEROS:"), 9) == 0)
  1576. {
  1577. BSTR bstrPrincipalName = ::SysAllocString(&pszAuthority[9]);
  1578. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_GSS_KERBEROS,"
  1579. L"RPC_C_AUTHZ_NONE, %s, %d, %d, -, EOAC_NONE)",
  1580. (LPWSTR)bstrPrincipalName,
  1581. rParsedInfo.GetGlblSwitchesObject().
  1582. GetAuthenticationLevel(),
  1583. rParsedInfo.GetGlblSwitchesObject().
  1584. GetImpersonationLevel());
  1585. SAFEBSTRFREE(bstrPrincipalName);
  1586. }
  1587. else
  1588. {
  1589. chsMsg.Format(
  1590. L"CoSetProxyBlanket(-, RPC_C_AUTHN_WINNT, "
  1591. L"RPC_C_AUTHZ_NONE, NULL, %d, %d, -, "
  1592. L"EOAC_NONE)",
  1593. rParsedInfo.GetGlblSwitchesObject().
  1594. GetAuthenticationLevel(),
  1595. rParsedInfo.GetGlblSwitchesObject().
  1596. GetImpersonationLevel());
  1597. }
  1598. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  1599. (LPCWSTR)chsMsg, dwThreadId,
  1600. rParsedInfo, m_bTrace);
  1601. }
  1602. ONFAILTHROWERROR(hr);
  1603. // Loop thru the available instances
  1604. hr = pIEnumObj->Next( WBEM_INFINITE, 1, &pIWbemObj,
  1605. &ulReturned );
  1606. if (m_bTrace || m_eloErrLogOpt)
  1607. {
  1608. chsMsg.Format(
  1609. L"IEnumWbemClassObject->Next(WBEM_INFINITE, 1,"
  1610. L"-, -)");
  1611. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  1612. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo,
  1613. m_bTrace);
  1614. }
  1615. ONFAILTHROWERROR(hr);
  1616. // Set this property in all objects of the collection
  1617. while (ulReturned == 1)
  1618. {
  1619. bInstances = TRUE;
  1620. VariantInit(&varPath);
  1621. hr = pIWbemObj->Get(L"__PATH", 0, &varPath, 0, 0);
  1622. if (m_bTrace || m_eloErrLogOpt)
  1623. {
  1624. chsMsg.Format(
  1625. L"IWbemClassObject::Get(L\"__PATH\", 0, -,"
  1626. L"0, 0)");
  1627. GetBstrTFromVariant(varPath, bstrResult);
  1628. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  1629. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo,
  1630. m_bTrace, 0, bstrResult);
  1631. }
  1632. ONFAILTHROWERROR(hr);
  1633. //form the query for finding the associators
  1634. //of each of the instances
  1635. bstrQuery = _bstr_t(L"ASSOCIATORS OF {")
  1636. + varPath.bstrVal
  1637. + _bstr_t("}") ;
  1638. if (bSwitches)
  1639. bstrQuery += bstrAssocWhere;
  1640. hr = FrameAssocHeader(varPath.bstrVal, bstrResult,
  1641. bClass);
  1642. ONFAILTHROWERROR(hr);
  1643. //Obtain the result set for the associators
  1644. //of the corresponding instance
  1645. hr = ObtainXMLResultSet(bstrQuery, rParsedInfo,
  1646. bstrXML, TRUE, FALSE);
  1647. ONFAILTHROWERROR(hr);
  1648. if (m_bNoAssoc)
  1649. {
  1650. _bstr_t bstrMsg;
  1651. WMIFormatMessage(IDS_I_NO_ASSOCIATIONS, 1,
  1652. bstrMsg, (LPWSTR)varPath.bstrVal);
  1653. DisplayMessage((LPWSTR)bstrMsg, CP_OEMCP,
  1654. TRUE, TRUE);
  1655. m_bNoAssoc = FALSE;
  1656. }
  1657. bstrResult += bstrXML;
  1658. bstrResult += L"</INSTANCE>";
  1659. bstrAggResult += bstrResult;
  1660. //check for ctrl+c
  1661. if ( g_wmiCmd.GetBreakEvent() == TRUE )
  1662. {
  1663. VARIANTCLEAR(varPath);
  1664. SAFEIRELEASE(pIWbemObj);
  1665. break;
  1666. }
  1667. VARIANTCLEAR(varPath);
  1668. SAFEIRELEASE(pIWbemObj);
  1669. if ( bRet == FALSE )
  1670. break;
  1671. // Obtain the next instance in the enumeration.
  1672. hr = pIEnumObj->Next( WBEM_INFINITE, 1, &pIWbemObj,
  1673. &ulReturned);
  1674. if (m_bTrace || m_eloErrLogOpt)
  1675. {
  1676. chsMsg.Format(
  1677. L"IEnumWbemClassObject->Next(WBEM_INFINITE,"
  1678. L"1, -, -)");
  1679. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  1680. (LPCWSTR)chsMsg,
  1681. dwThreadId, rParsedInfo, m_bTrace);
  1682. }
  1683. ONFAILTHROWERROR(hr);
  1684. }
  1685. SAFEIRELEASE(pIEnumObj);
  1686. // If no instances are available
  1687. if (bInstances == FALSE)
  1688. {
  1689. _bstr_t bstrMsg;
  1690. WMIFormatMessage(IDS_I_NO_INSTANCES,
  1691. 0, bstrMsg, NULL);
  1692. DisplayMessage((LPWSTR)bstrMsg, CP_OEMCP,
  1693. TRUE, TRUE);
  1694. CHString sTemp;
  1695. sTemp.Format(
  1696. _T("<ERROR><DESCRIPTION>%s</DESCRIPTION></ERROR>"),
  1697. (LPWSTR) bstrMsg);
  1698. bstrAggResult = _bstr_t(sTemp);
  1699. }
  1700. }
  1701. catch(_com_error& e)
  1702. {
  1703. VARIANTCLEAR(varPath);
  1704. SAFEIRELEASE(pIWbemObj);
  1705. SAFEIRELEASE(pIEnumObj);
  1706. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  1707. }
  1708. }
  1709. if (SUCCEEDED(hr))
  1710. {
  1711. if ((nReqType != 2) || ((nReqType == 2) && bInstances))
  1712. {
  1713. bstrAggResult += L"</CIM>";
  1714. rParsedInfo.GetCmdSwitchesObject().
  1715. SetXMLResultSet(bstrAggResult);
  1716. }
  1717. }
  1718. }
  1719. bRet = FAILED(hr) ? FALSE : TRUE;
  1720. }
  1721. }
  1722. catch(_com_error& e)
  1723. {
  1724. VARIANTCLEAR(varPath);
  1725. SAFEIRELEASE(pIWbemObj);
  1726. SAFEIRELEASE(pIEnumObj);
  1727. _com_issue_error(e.Error());
  1728. }
  1729. // trap for CHeap_Exception
  1730. catch(CHeap_Exception)
  1731. {
  1732. VARIANTCLEAR(varPath);
  1733. SAFEIRELEASE(pIWbemObj);
  1734. SAFEIRELEASE(pIEnumObj);
  1735. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1736. }
  1737. return bRet;
  1738. }
  1739. /*------------------------------------------------------------------------
  1740. Name :ProcessSETVerb
  1741. Synopsis :Processes the SET verb referring to the information
  1742. available with CParsedInfo object.
  1743. Type :Member Function
  1744. Input Parameter(s):
  1745. rParsedInfo - reference to CParsedInfo class object
  1746. Output Parameter(s):
  1747. rParsedInfo - reference to CParsedInfo class object
  1748. Return Type :BOOL
  1749. Global Variables :None
  1750. Calling Syntax :ProcessSETVerb(rParsedInfo)
  1751. Notes :None
  1752. ------------------------------------------------------------------------*/
  1753. BOOL CExecEngine::ProcessSETVerb(CParsedInfo& rParsedInfo)
  1754. {
  1755. // SET verb processing
  1756. BOOL bRet = TRUE;
  1757. HRESULT hr = S_OK;
  1758. try
  1759. {
  1760. _bstr_t bstrQuery(""), bstrObject(""), bstrClass("");
  1761. // If anyone of the following is specified:
  1762. // a) PATH <path expr>
  1763. // b) PATH <class path expr> WHERE <where expr>
  1764. if (rParsedInfo.GetCmdSwitchesObject().GetPathExpression() != NULL)
  1765. {
  1766. bstrClass = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  1767. .GetClassPath());
  1768. bstrObject = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  1769. .GetPathExpression());
  1770. // Form the query
  1771. bstrQuery = _bstr_t(L"SELECT * FROM ") + bstrClass ;
  1772. // If WHERE expresion is given
  1773. if (rParsedInfo.GetCmdSwitchesObject().
  1774. GetWhereExpression() != NULL)
  1775. {
  1776. bstrQuery += _bstr_t(L" WHERE ")
  1777. + _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  1778. .GetWhereExpression());
  1779. }
  1780. }
  1781. // If <alias> WHERE expression is specified.
  1782. else if (rParsedInfo.GetCmdSwitchesObject().GetWhereExpression()
  1783. != NULL)
  1784. {
  1785. rParsedInfo.GetCmdSwitchesObject().
  1786. GetClassOfAliasTarget(bstrObject);
  1787. bstrQuery = _bstr_t(L"SELECT * FROM ")
  1788. + bstrObject
  1789. + _bstr_t(L" WHERE ")
  1790. + _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  1791. .GetWhereExpression());
  1792. bstrClass = bstrObject;
  1793. }
  1794. // If CLASS is specified.
  1795. else if (rParsedInfo.GetCmdSwitchesObject().GetClassPath() != NULL)
  1796. {
  1797. bstrObject = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  1798. .GetClassPath());
  1799. bstrClass = bstrObject;
  1800. }
  1801. // if only <alias> is specified
  1802. else
  1803. {
  1804. rParsedInfo.GetCmdSwitchesObject().
  1805. GetClassOfAliasTarget(bstrObject);
  1806. bstrQuery = _bstr_t(L"SELECT * FROM ")
  1807. + bstrObject;
  1808. bstrClass = bstrObject;
  1809. }
  1810. // Connect to WMI namespace
  1811. if (m_pITargetNS == NULL)
  1812. {
  1813. if ( IsFailFastAndNodeExist(rParsedInfo) == TRUE )
  1814. {
  1815. hr = ConnectToTargetNS(rParsedInfo);
  1816. ONFAILTHROWERROR(hr);
  1817. }
  1818. else
  1819. hr = E_FAIL; // Explicitly set error
  1820. }
  1821. if (SUCCEEDED(hr))
  1822. {
  1823. // Validate the property values against the property
  1824. // qualifier information if available.
  1825. if (rParsedInfo.GetCmdSwitchesObject().GetAliasName() != NULL)
  1826. {
  1827. // Validate the input parameters against the alias
  1828. // qualifier information.
  1829. bRet = ValidateAlaisInParams(rParsedInfo);
  1830. }
  1831. else
  1832. {
  1833. // Validate the input parameters against the class
  1834. // qualifier information
  1835. bRet = ValidateInParams(rParsedInfo, bstrClass);
  1836. }
  1837. if (bRet)
  1838. {
  1839. // Set the values passed as input to the appropriate properties.
  1840. bRet = SetPropertyInfo(rParsedInfo, bstrQuery, bstrObject);
  1841. }
  1842. }
  1843. else
  1844. bRet = FALSE;
  1845. }
  1846. catch(_com_error& e)
  1847. {
  1848. _com_issue_error(e.Error());
  1849. bRet = FALSE;
  1850. }
  1851. return bRet;
  1852. }
  1853. /*------------------------------------------------------------------------
  1854. Name :SetPropertyInfo
  1855. Synopsis :This function updates the property value for the
  1856. given property name and value
  1857. Type :Member Function
  1858. Input Parameter(s):
  1859. rParsedInfo - reference to the CParsedInfo object
  1860. bstrQuery - String consist of WQL query
  1861. bstrObject - String consist of object path
  1862. Output Parameter(s):
  1863. rParsedInfo - reference to the CParsedInfo object
  1864. Return Type :BOOL
  1865. Global Variables :None
  1866. Calling Syntax :SetPropertyInfo(rParsedInfo, bstrQuery, bstrObject)
  1867. Notes :None
  1868. ------------------------------------------------------------------------*/
  1869. BOOL CExecEngine::SetPropertyInfo(CParsedInfo& rParsedInfo,
  1870. _bstr_t& bstrQuery, _bstr_t& bstrObject)
  1871. {
  1872. HRESULT hr = S_OK;
  1873. IEnumWbemClassObject *pIEnumObj = NULL;
  1874. IWbemClassObject *pIWbemObj = NULL;
  1875. ULONG ulReturned = 0;
  1876. BOOL bSuccess = TRUE;
  1877. CHString chsMsg;
  1878. DWORD dwThreadId = GetCurrentThreadId();
  1879. VARIANT varPath;
  1880. VariantInit(&varPath);
  1881. try
  1882. {
  1883. if (bstrQuery == _bstr_t(""))
  1884. {
  1885. // If query is NULL then get the object of WMI Class based on
  1886. // PATH expression
  1887. hr = m_pITargetNS->GetObject(bstrObject,
  1888. 0, NULL, &pIWbemObj, NULL);
  1889. if (m_bTrace || m_eloErrLogOpt)
  1890. {
  1891. chsMsg.Format(L"IWbemServices::GetObject(L\"%s\", 0, "
  1892. L"NULL, -)", (LPWSTR) bstrObject);
  1893. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  1894. dwThreadId, rParsedInfo, m_bTrace);
  1895. }
  1896. ONFAILTHROWERROR(hr);
  1897. // If instance path is specified then modify the instance
  1898. // properties otherwise modify class properties
  1899. if(rParsedInfo.GetCmdSwitchesObject().GetWhereExpression() == NULL)
  1900. bSuccess = SetProperties(rParsedInfo, pIWbemObj, TRUE);
  1901. else
  1902. bSuccess = SetProperties(rParsedInfo, pIWbemObj, FALSE);
  1903. SAFEIRELEASE(pIWbemObj);
  1904. }
  1905. else
  1906. {
  1907. // Execute the query to get collection of objects
  1908. hr = m_pITargetNS->ExecQuery(_bstr_t(L"WQL"), bstrQuery, 0,
  1909. NULL, &pIEnumObj);
  1910. if (m_bTrace || m_eloErrLogOpt)
  1911. {
  1912. chsMsg.Format(L"IWbemServices::ExecQuery(L\"WQL\","
  1913. L"L\"%s\", 0, NULL, -)", (LPWSTR)bstrQuery);
  1914. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  1915. dwThreadId, rParsedInfo, m_bTrace);
  1916. }
  1917. ONFAILTHROWERROR(hr);
  1918. // Set the interface security
  1919. hr = SetSecurity(pIEnumObj,
  1920. rParsedInfo.GetAuthorityPrinciple(),
  1921. rParsedInfo.GetNode(),
  1922. rParsedInfo.GetUser(),
  1923. rParsedInfo.GetPassword(),
  1924. rParsedInfo.GetGlblSwitchesObject().
  1925. GetAuthenticationLevel(),
  1926. rParsedInfo.GetGlblSwitchesObject().
  1927. GetImpersonationLevel());
  1928. if (m_bTrace || m_eloErrLogOpt)
  1929. {
  1930. _TCHAR* pszAuthority = rParsedInfo.GetAuthorityPrinciple();
  1931. if( pszAuthority != NULL &&
  1932. _tcslen(pszAuthority) > 9 &&
  1933. _tcsnicmp(pszAuthority, _T("KERBEROS:"), 9) == 0)
  1934. {
  1935. BSTR bstrPrincipalName = ::SysAllocString(&pszAuthority[9]);
  1936. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_GSS_KERBEROS,"
  1937. L"RPC_C_AUTHZ_NONE, %s, %d, %d, -, EOAC_NONE)",
  1938. (LPWSTR)bstrPrincipalName,
  1939. rParsedInfo.GetGlblSwitchesObject().
  1940. GetAuthenticationLevel(),
  1941. rParsedInfo.GetGlblSwitchesObject().
  1942. GetImpersonationLevel());
  1943. SAFEBSTRFREE(bstrPrincipalName);
  1944. }
  1945. else
  1946. {
  1947. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_WINNT, "
  1948. L"RPC_C_AUTHZ_NONE, NULL, %d, %d, -, EOAC_NONE)",
  1949. rParsedInfo.GetGlblSwitchesObject().
  1950. GetAuthenticationLevel(),
  1951. rParsedInfo.GetGlblSwitchesObject().
  1952. GetImpersonationLevel());
  1953. }
  1954. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  1955. dwThreadId, rParsedInfo, m_bTrace);
  1956. }
  1957. ONFAILTHROWERROR(hr);
  1958. BOOL bInstances = FALSE;
  1959. // Loop thru the available instances
  1960. hr = pIEnumObj->Next( WBEM_INFINITE, 1, &pIWbemObj, &ulReturned );
  1961. // Set this property in all objects of the collection
  1962. while (ulReturned == 1)
  1963. {
  1964. bInstances = TRUE;
  1965. // If instance updation failed.
  1966. if (!SetProperties(rParsedInfo, pIWbemObj, FALSE))
  1967. {
  1968. bSuccess = FALSE;
  1969. VARIANTCLEAR(varPath);
  1970. SAFEIRELEASE(pIEnumObj);
  1971. SAFEIRELEASE(pIWbemObj);
  1972. break;
  1973. }
  1974. VARIANTCLEAR(varPath);
  1975. SAFEIRELEASE(pIWbemObj);
  1976. // Obtain the next instance in the enumeration.
  1977. hr = pIEnumObj->Next( WBEM_INFINITE, 1,
  1978. &pIWbemObj, &ulReturned);
  1979. }
  1980. SAFEIRELEASE(pIEnumObj);
  1981. // If no instances are available
  1982. if (!bInstances)
  1983. {
  1984. rParsedInfo.GetCmdSwitchesObject().
  1985. SetInformationCode(IDS_I_NO_INSTANCES);
  1986. }
  1987. }
  1988. }
  1989. catch(_com_error& e)
  1990. {
  1991. SAFEIRELEASE(pIEnumObj);
  1992. SAFEIRELEASE(pIWbemObj);
  1993. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  1994. bSuccess = FALSE;
  1995. }
  1996. // trap for CHeap_Exception
  1997. catch(CHeap_Exception)
  1998. {
  1999. SAFEIRELEASE(pIEnumObj);
  2000. SAFEIRELEASE(pIWbemObj);
  2001. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2002. }
  2003. return bSuccess;
  2004. }
  2005. /*------------------------------------------------------------------------
  2006. Name :SetProperties
  2007. Synopsis :This function changes the property values for the
  2008. given property names and values in a passed
  2009. IWbemClassObject
  2010. Type :Member Function
  2011. Input Parameter(s):
  2012. rParsedInfo - CParsedInfo object consist of parsed tokens
  2013. pIWbemObj - IWbemClassObject in which property has to be set
  2014. bClass - Flag to indicate whether class object is passed or
  2015. instance is passed
  2016. Output Parameter(s):None
  2017. Return Type :BOOL
  2018. Global Variables :None
  2019. Calling Syntax :SetProperties(rParsedInfo, pIWbemObj, bClass)
  2020. Notes :None
  2021. ------------------------------------------------------------------------*/
  2022. BOOL CExecEngine::SetProperties(CParsedInfo& rParsedInfo,
  2023. IWbemClassObject* pIWbemObj, BOOL bClass)
  2024. {
  2025. HRESULT hr = S_OK;
  2026. IWbemQualifierSet *pIQualSet = NULL;
  2027. BOOL bRet = TRUE,
  2028. bInteractive = FALSE,
  2029. bChange = FALSE;
  2030. CHString chsMsg;
  2031. DWORD dwThreadId = GetCurrentThreadId();
  2032. BSTRMAP::iterator theIterator = NULL;
  2033. VARIANT varValue,
  2034. varDest,
  2035. varSrc,
  2036. varPath,
  2037. varType;
  2038. INTEROPTION interOption = YES;
  2039. VariantInit(&varValue);
  2040. VariantInit(&varDest);
  2041. VariantInit(&varSrc);
  2042. VariantInit(&varPath);
  2043. VariantInit(&varType);
  2044. // Get the proprty name and their corresponding value
  2045. BSTRMAP theMap = rParsedInfo.GetCmdSwitchesObject().GetParameterMap();
  2046. // Set the iterator to the start of the map.
  2047. theIterator = theMap.begin();
  2048. // obtian the verb interactive mode status.
  2049. bInteractive = IsInteractive(rParsedInfo);
  2050. try
  2051. {
  2052. _bstr_t bstrResult;
  2053. // Obtain the __PATH property value
  2054. hr = pIWbemObj->Get(_bstr_t(L"__PATH"), 0, &varPath, 0, 0);
  2055. if (m_bTrace || m_eloErrLogOpt)
  2056. {
  2057. chsMsg.Format(L"IWbemClassObject::Get(L\"__PATH\", 0, -,"
  2058. L"0, 0)");
  2059. GetBstrTFromVariant(varPath, bstrResult);
  2060. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2061. dwThreadId, rParsedInfo, m_bTrace, 0, bstrResult);
  2062. }
  2063. ONFAILTHROWERROR(hr);
  2064. // If /INTERACTIVE switch is specified, obtain the user response.
  2065. if (bInteractive)
  2066. {
  2067. _bstr_t bstrMsg;
  2068. while(TRUE)
  2069. {
  2070. if(IsClassOperation(rParsedInfo))
  2071. {
  2072. WMIFormatMessage(IDS_I_UPDATE_PROMPT, 1, bstrMsg,
  2073. (LPWSTR) _bstr_t(varPath.bstrVal));
  2074. interOption = GetUserResponse((LPWSTR)bstrMsg);
  2075. }
  2076. else
  2077. {
  2078. WMIFormatMessage(IDS_I_UPDATE_PROMPT2, 1, bstrMsg,
  2079. (LPWSTR) _bstr_t(varPath.bstrVal));
  2080. interOption = GetUserResponseEx((LPWSTR)bstrMsg);
  2081. }
  2082. if (interOption == YES || interOption == NO)
  2083. break;
  2084. else
  2085. if (interOption == HELP)
  2086. {
  2087. rParsedInfo.GetCmdSwitchesObject().
  2088. SetInformationCode(0);
  2089. ProcessSHOWInfo(rParsedInfo, FALSE,
  2090. (_TCHAR*)_bstr_t(varPath.bstrVal));
  2091. }
  2092. }
  2093. }
  2094. else
  2095. {
  2096. _bstr_t bstrMsg;
  2097. WMIFormatMessage(IDS_I_PROMPT_UPDATING, 1, bstrMsg,
  2098. (LPWSTR) _bstr_t(varPath.bstrVal));
  2099. DisplayMessage((LPWSTR)bstrMsg, CP_OEMCP, FALSE, TRUE);
  2100. }
  2101. VARIANTCLEAR(varPath);
  2102. VariantInit(&varSrc);
  2103. VariantInit(&varDest);
  2104. if (interOption == YES)
  2105. {
  2106. PROPDETMAP pdmPropDetMap = rParsedInfo.GetCmdSwitchesObject().
  2107. GetPropDetMap();
  2108. PROPDETMAP::iterator itrPropDetMap;
  2109. BOOL bPropType = FALSE;
  2110. // Update all properties
  2111. while (theIterator != theMap.end())
  2112. {
  2113. // Get the property names and their corresponding values
  2114. _bstr_t bstrProp = _bstr_t((_TCHAR*)(*theIterator).first);
  2115. // Get the derivation of property name
  2116. if ( Find(pdmPropDetMap, bstrProp, itrPropDetMap) == TRUE )
  2117. {
  2118. if ( !((*itrPropDetMap).second.Derivation) == FALSE )
  2119. bstrProp = (*itrPropDetMap).second.Derivation;
  2120. bPropType = TRUE;
  2121. }
  2122. else
  2123. bPropType = FALSE;
  2124. // Check for the property validity(i.e. does it exist or not?)
  2125. VariantInit(&varValue);
  2126. hr = pIWbemObj->Get(bstrProp, 0, &varValue, 0, 0);
  2127. if (m_bTrace || m_eloErrLogOpt)
  2128. {
  2129. chsMsg.Format(L"IWbemClassObject::Get(L\"%s\", 0, -,"
  2130. L"0, 0)", (LPWSTR) bstrProp);
  2131. if ( bPropType )
  2132. {
  2133. GetBstrTFromVariant(varValue, bstrResult,
  2134. (*itrPropDetMap).second.Type);
  2135. }
  2136. else
  2137. GetBstrTFromVariant(varValue, bstrResult);
  2138. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2139. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo,
  2140. m_bTrace, 0, bstrResult);
  2141. }
  2142. ONFAILTHROWERROR(hr);
  2143. // Set the change flag to TRUE
  2144. bChange = TRUE;
  2145. // If the property content is <empty>
  2146. if ((varValue.vt == VT_EMPTY) || (varValue.vt == VT_NULL))
  2147. {
  2148. // Obtain the property qualifier set for the property
  2149. hr = pIWbemObj->GetPropertyQualifierSet(bstrProp,
  2150. &pIQualSet);
  2151. if (m_bTrace || m_eloErrLogOpt)
  2152. {
  2153. chsMsg.Format(
  2154. L"IWbemClassObject::GetPropertyQualifierSet(L\"%s\","
  2155. L" -)", (LPWSTR)bstrProp);
  2156. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2157. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, m_bTrace);
  2158. }
  2159. ONFAILTHROWERROR(hr);
  2160. VariantInit(&varType);
  2161. if (pIQualSet)
  2162. {
  2163. // Obtain the CIM type of the property
  2164. hr = pIQualSet->Get(_bstr_t(L"CIMTYPE"), 0L,
  2165. &varType, NULL);
  2166. if (m_bTrace || m_eloErrLogOpt)
  2167. {
  2168. chsMsg.Format(L"IWbemQualifierSet::Get(L\"CIMTYPE\","
  2169. L" 0, -, 0, 0)");
  2170. GetBstrTFromVariant(varType, bstrResult);
  2171. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2172. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo,
  2173. m_bTrace, 0, bstrResult);
  2174. }
  2175. ONFAILTHROWERROR(hr);
  2176. if(!IsArrayType(pIWbemObj, bstrProp))
  2177. {
  2178. bool bNullConst = FALSE;
  2179. varSrc.vt = VT_BSTR;
  2180. _TCHAR* pszValue = (*theIterator).second;
  2181. if(_tcsicmp(pszValue,_T("NULL")) == 0){
  2182. bNullConst = TRUE;
  2183. }
  2184. if(_tcsicmp(pszValue,_T("\"NULL\"")) == 0){
  2185. varSrc.bstrVal = SysAllocString(_T("NULL"));
  2186. } else {
  2187. varSrc.bstrVal = SysAllocString(pszValue);
  2188. }
  2189. if (varSrc.bstrVal == NULL)
  2190. {
  2191. //Reset the variant, it will be cleaned up by the catch...
  2192. VariantInit(&varSrc);
  2193. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  2194. }
  2195. if (bNullConst)
  2196. hr = VariantChangeType(&varDest, &varSrc, 0, VT_NULL);
  2197. else
  2198. hr = ConvertCIMTYPEToVarType(varDest, varSrc,
  2199. (_TCHAR*)varType.bstrVal);
  2200. if ( m_eloErrLogOpt )
  2201. {
  2202. chsMsg.Format(L"VariantChangeType(-, -, 0, -)");
  2203. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2204. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  2205. }
  2206. ONFAILTHROWERROR(hr);
  2207. }
  2208. else
  2209. {
  2210. BSTRVECTOR vArrayValues;
  2211. _TCHAR* pszValue = (*theIterator).second;
  2212. RemoveParanthesis(pszValue);
  2213. GetArrayFromToken(pszValue,
  2214. vArrayValues);
  2215. hr = CheckForArray( pIWbemObj, bstrProp,
  2216. varDest, vArrayValues, rParsedInfo);
  2217. ONFAILTHROWERROR(hr);
  2218. }
  2219. VARIANTCLEAR(varType);
  2220. SAFEIRELEASE(pIQualSet);
  2221. }
  2222. }
  2223. // If the property content is not <empty>
  2224. else
  2225. {
  2226. if(!IsArrayType(pIWbemObj, bstrProp))
  2227. {
  2228. bool bNullConst = FALSE;
  2229. varSrc.vt = VT_BSTR;
  2230. _TCHAR* pszValue = (*theIterator).second;
  2231. if(_tcsicmp(pszValue,_T("NULL")) == 0){
  2232. bNullConst = TRUE;
  2233. }
  2234. if(_tcsicmp(pszValue,_T("\"NULL\"")) == 0){
  2235. varSrc.bstrVal = SysAllocString(_T("NULL"));
  2236. } else {
  2237. varSrc.bstrVal = SysAllocString(pszValue);
  2238. }
  2239. if (varSrc.bstrVal == NULL)
  2240. {
  2241. //Reset the variant, it will be cleaned up by the catch...
  2242. VariantInit(&varSrc);
  2243. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  2244. }
  2245. // If _T("NULL") is the value should be treated as
  2246. // equivalent to VT_NULL
  2247. if (bNullConst)
  2248. hr = VariantChangeType(&varDest, &varSrc, 0, VT_NULL);
  2249. else
  2250. hr = VariantChangeType(&varDest, &varSrc,
  2251. 0, varValue.vt);
  2252. if ( m_eloErrLogOpt )
  2253. {
  2254. chsMsg.Format(L"VariantChangeType(-, -, 0, -)");
  2255. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2256. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  2257. }
  2258. ONFAILTHROWERROR(hr);
  2259. }
  2260. else
  2261. {
  2262. BSTRVECTOR vArrayValues;
  2263. _TCHAR* pszValue = (*theIterator).second;
  2264. RemoveParanthesis(pszValue);
  2265. GetArrayFromToken(pszValue,
  2266. vArrayValues);
  2267. hr = CheckForArray( pIWbemObj, bstrProp,
  2268. varDest, vArrayValues, rParsedInfo);
  2269. ONFAILTHROWERROR(hr);
  2270. }
  2271. }
  2272. // Update the property value
  2273. hr = pIWbemObj->Put(bstrProp, 0, &varDest, 0);
  2274. if (m_bTrace || m_eloErrLogOpt)
  2275. {
  2276. chsMsg.Format(L"IWbemClassObject::Put(L\"%s\", 0, -, 0)",
  2277. (LPWSTR)bstrProp);
  2278. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2279. dwThreadId, rParsedInfo, m_bTrace);
  2280. }
  2281. ONFAILTHROWERROR(hr);
  2282. VARIANTCLEAR(varSrc);
  2283. VARIANTCLEAR(varDest);
  2284. VARIANTCLEAR(varValue);
  2285. // Move to next entry
  2286. theIterator++;
  2287. }
  2288. }
  2289. // Write the instance or class object to Windows Management
  2290. // Instrumentation (WMI).
  2291. if (bChange)
  2292. {
  2293. if(bClass)
  2294. {
  2295. // Update the class schema with the changes
  2296. hr = m_pITargetNS->PutClass(pIWbemObj, 0, NULL, NULL);
  2297. if (m_bTrace || m_eloErrLogOpt)
  2298. {
  2299. chsMsg.Format(L"IWbemServices::PutClass(-, 0, "
  2300. L"NULL, NULL)");
  2301. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2302. dwThreadId, rParsedInfo, m_bTrace);
  2303. }
  2304. ONFAILTHROWERROR(hr);
  2305. }
  2306. else
  2307. {
  2308. // Update the instance with the changes
  2309. hr = m_pITargetNS->PutInstance(pIWbemObj, WBEM_FLAG_UPDATE_ONLY, NULL, NULL);
  2310. if (m_bTrace || m_eloErrLogOpt)
  2311. {
  2312. chsMsg.Format(L"IWbemServices::PutInstance(-, 0, NULL"
  2313. L", NULL)");
  2314. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2315. dwThreadId, rParsedInfo, m_bTrace);
  2316. }
  2317. ONFAILTHROWERROR(hr);
  2318. }
  2319. DisplayString(IDS_I_SET_SUCCESS, CP_OEMCP, NULL, FALSE, TRUE);
  2320. }
  2321. }
  2322. catch(_com_error& e)
  2323. {
  2324. VARIANTCLEAR(varSrc);
  2325. VARIANTCLEAR(varDest);
  2326. VARIANTCLEAR(varValue);
  2327. VARIANTCLEAR(varType);
  2328. SAFEIRELEASE(pIQualSet);
  2329. VARIANTCLEAR(varSrc);
  2330. VARIANTCLEAR(varDest);
  2331. // Store the COM error, and set the return value to FALSE.
  2332. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  2333. bRet = FALSE;
  2334. }
  2335. catch(CHeap_Exception)
  2336. {
  2337. VARIANTCLEAR(varSrc);
  2338. VARIANTCLEAR(varDest);
  2339. VARIANTCLEAR(varValue);
  2340. VARIANTCLEAR(varType);
  2341. SAFEIRELEASE(pIQualSet);
  2342. VARIANTCLEAR(varSrc);
  2343. VARIANTCLEAR(varDest);
  2344. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2345. }
  2346. return bRet;
  2347. }
  2348. /*------------------------------------------------------------------------
  2349. Name :ConnectToTargetNS
  2350. Synopsis :This function connects to WMI namespace on the target
  2351. machine with given user credentials.
  2352. Type :Member Function
  2353. Input Parameter(s):
  2354. rParsedInfo - reference to CParsedInfo class object
  2355. Output Parameter(s):
  2356. rParsedInfo - reference to CParsedInfo class object
  2357. Return Type :HRESULT
  2358. Global Variables :None
  2359. Calling Syntax :ConnectToTargetNS(rParsedInfo)
  2360. Notes :None
  2361. ------------------------------------------------------------------------*/
  2362. HRESULT CExecEngine::ConnectToTargetNS(CParsedInfo& rParsedInfo)
  2363. {
  2364. HRESULT hr = S_OK;
  2365. CHString chsMsg;
  2366. DWORD dwThreadId = GetCurrentThreadId();
  2367. try
  2368. {
  2369. SAFEIRELEASE(m_pITargetNS);
  2370. _bstr_t bstrNameSpace = _bstr_t(L"\\\\")
  2371. + _bstr_t(rParsedInfo.GetNode())
  2372. + _bstr_t(L"\\")
  2373. + _bstr_t(rParsedInfo.GetNamespace());
  2374. // Connect to the specified WMI namespace
  2375. hr = Connect(m_pIWbemLocator, &m_pITargetNS,
  2376. bstrNameSpace,
  2377. _bstr_t(rParsedInfo.GetUser()),
  2378. _bstr_t(rParsedInfo.GetPassword()),
  2379. _bstr_t(rParsedInfo.GetLocale()),
  2380. rParsedInfo);
  2381. if (m_bTrace || m_eloErrLogOpt)
  2382. {
  2383. chsMsg.Format(L"IWbemLocator::ConnectServer(L\"%s\", "
  2384. L"L\"%s\", *, L\"%s\", 0L, L\"%s\", NULL, -)",
  2385. (LPWSTR)bstrNameSpace,
  2386. (rParsedInfo.GetUser()) ? rParsedInfo.GetUser() : L"<null>",
  2387. rParsedInfo.GetLocale(),
  2388. (rParsedInfo.GetAuthorityPrinciple()) ?
  2389. rParsedInfo.GetAuthorityPrinciple() : L"<null>");
  2390. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2391. dwThreadId, rParsedInfo, m_bTrace);
  2392. }
  2393. ONFAILTHROWERROR(hr);
  2394. // Set the interface level security
  2395. hr = SetSecurity(m_pITargetNS,
  2396. rParsedInfo.GetAuthorityPrinciple(),
  2397. rParsedInfo.GetNode(),
  2398. rParsedInfo.GetUser(),
  2399. rParsedInfo.GetPassword(),
  2400. rParsedInfo.GetGlblSwitchesObject().GetAuthenticationLevel(),
  2401. rParsedInfo.GetGlblSwitchesObject().GetImpersonationLevel());
  2402. if (m_bTrace || m_eloErrLogOpt)
  2403. {
  2404. _TCHAR* pszAuthority = rParsedInfo.GetAuthorityPrinciple();
  2405. if( pszAuthority != NULL &&
  2406. _tcslen(pszAuthority) > 9 &&
  2407. _tcsnicmp(pszAuthority, _T("KERBEROS:"), 9) == 0)
  2408. {
  2409. BSTR bstrPrincipalName = ::SysAllocString(&pszAuthority[9]);
  2410. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_GSS_KERBEROS,"
  2411. L"RPC_C_AUTHZ_NONE, %s, %d, %d, -, EOAC_NONE)",
  2412. (LPWSTR)bstrPrincipalName,
  2413. rParsedInfo.GetGlblSwitchesObject().
  2414. GetAuthenticationLevel(),
  2415. rParsedInfo.GetGlblSwitchesObject().
  2416. GetImpersonationLevel());
  2417. SAFEBSTRFREE(bstrPrincipalName);
  2418. }
  2419. else
  2420. {
  2421. chsMsg.Format(
  2422. L"CoSetProxyBlanket(-, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,"
  2423. L"NULL, %d, %d, -, EOAC_NONE)",
  2424. rParsedInfo.GetGlblSwitchesObject().GetAuthenticationLevel(),
  2425. rParsedInfo.GetGlblSwitchesObject().GetImpersonationLevel());
  2426. }
  2427. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2428. dwThreadId, rParsedInfo, m_bTrace);
  2429. }
  2430. ONFAILTHROWERROR(hr);
  2431. }
  2432. catch(_com_error& e)
  2433. {
  2434. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  2435. hr = e.Error();
  2436. }
  2437. catch(CHeap_Exception)
  2438. {
  2439. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2440. }
  2441. return hr;
  2442. }
  2443. /*------------------------------------------------------------------------
  2444. Name :CreateWMIXMLTextSrc
  2445. Synopsis :This function creates the IWbemObjectTextSrc instance
  2446. Type :Member Function
  2447. Input Parameter(s):
  2448. rParsedInfo - reference to CParsedInfo class object
  2449. Output Parameter(s):
  2450. rParsedInfo - reference to CParsedInfo class object
  2451. Return Type :HRESULT
  2452. Global Variables :None
  2453. Calling Syntax :CreateWMIXMLTextSrc(rParsedInfo)
  2454. Notes :None
  2455. ------------------------------------------------------------------------*/
  2456. HRESULT CExecEngine::CreateWMIXMLTextSrc(CParsedInfo& rParsedInfo)
  2457. {
  2458. HRESULT hr = S_OK;
  2459. CHString chsMsg;
  2460. DWORD dwThreadId = GetCurrentThreadId();
  2461. try
  2462. {
  2463. hr = CoCreateInstance(CLSID_WbemObjectTextSrc, NULL,
  2464. CLSCTX_INPROC_SERVER,
  2465. IID_IWbemObjectTextSrc,
  2466. (LPVOID*) &m_pITextSrc);
  2467. if (m_bTrace || m_eloErrLogOpt)
  2468. {
  2469. chsMsg.Format(L"CoCreateInstance(CLSID_WbemObjectTextSrc, NULL,"
  2470. L"CLSCTX_INPROC_SERVER, IID_IWbemObjectTextSrc, -)");
  2471. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2472. dwThreadId, rParsedInfo, m_bTrace);
  2473. }
  2474. ONFAILTHROWERROR(hr);
  2475. }
  2476. catch(_com_error& e)
  2477. {
  2478. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  2479. hr = e.Error();
  2480. }
  2481. catch(CHeap_Exception)
  2482. {
  2483. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2484. }
  2485. return hr;
  2486. }
  2487. /*------------------------------------------------------------------------
  2488. Name :CreateContext
  2489. Synopsis :This function creates the IWbemContext instance
  2490. Type :Member Function
  2491. Input Parameter(s):
  2492. rParsedInfo - reference to CParsedInfo class object
  2493. Output Parameter(s):
  2494. rParsedInfo - reference to CParsedInfo class object
  2495. Return Type :HRESULT
  2496. Global Variables :None
  2497. Calling Syntax :CreateContext(rParsedInfo)
  2498. Calls :CParsedInfo::GetCmdSwitchesObject()
  2499. CCommandSwitches::SetCOMError()
  2500. CoCreateInstance()
  2501. Called by :CExecEngine::ObtainXMLResultSet()
  2502. Notes :None
  2503. ------------------------------------------------------------------------*/
  2504. HRESULT CExecEngine::CreateContext(CParsedInfo& rParsedInfo)
  2505. {
  2506. HRESULT hr = S_OK;
  2507. CHString chsMsg;
  2508. DWORD dwThreadId = GetCurrentThreadId();
  2509. try
  2510. {
  2511. //Create context object
  2512. MULTI_QI mqi = { &IID_IWbemContext, 0, 0 };
  2513. hr = CoCreateInstanceEx(CLSID_WbemContext, NULL,
  2514. CLSCTX_INPROC_SERVER,
  2515. 0, 1, &mqi);
  2516. if (m_bTrace || m_eloErrLogOpt)
  2517. {
  2518. chsMsg.Format(L"CoCreateInstanceEx(CLSID_WbemContext, NULL,"
  2519. L"CLSCTX_INPROC_SERVER, 0, 1, -)");
  2520. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2521. dwThreadId, rParsedInfo, m_bTrace);
  2522. }
  2523. ONFAILTHROWERROR(hr);
  2524. m_pIContext = reinterpret_cast<IWbemContext*>(mqi.pItf);
  2525. }
  2526. catch(_com_error& e)
  2527. {
  2528. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  2529. hr = e.Error();
  2530. }
  2531. catch(CHeap_Exception)
  2532. {
  2533. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2534. }
  2535. return hr;
  2536. }
  2537. /*-------------------------------------------------------------------------
  2538. Name :ExecuteMethodAndDisplayResults
  2539. Synopsis :This function executes and displays the
  2540. results corresponding to the method. If
  2541. interactive mode is set, the user is prompted
  2542. choose the method
  2543. Type :Member Function
  2544. Input Parameter(s):
  2545. bstrPath - _bstr_t type,Path expression
  2546. rParsedInfo - reference to CParsedInfo class object
  2547. pIInParam - Pointer to the IWbemclassobject
  2548. Output Parameter(s):
  2549. rParsedInfo - reference to CParsedInfo class object
  2550. Return Type :HRESULT
  2551. Global Variables :None
  2552. Calling Syntax :ExecuteMethodAndDisplayResults(bstrPath, rParsedInfo,
  2553. pIInParam)
  2554. Calls :None
  2555. Called by :CExecEngine::ExecWMIMethod()
  2556. Notes :none
  2557. -------------------------------------------------------------------------*/
  2558. HRESULT CExecEngine::ExecuteMethodAndDisplayResults(_bstr_t bstrPath,
  2559. CParsedInfo& rParsedInfo,
  2560. IWbemClassObject* pIInParam)
  2561. {
  2562. _TCHAR *pszMethodName = NULL;
  2563. INTEROPTION interOption = YES;
  2564. IWbemClassObject *pIOutParam = NULL;
  2565. HRESULT hr = S_OK;
  2566. CHString chsMsg;
  2567. DWORD dwThreadId = GetCurrentThreadId();
  2568. VARIANT varTemp;
  2569. VariantInit(&varTemp);
  2570. // Obtain the method name
  2571. pszMethodName = rParsedInfo.GetCmdSwitchesObject().GetMethodName();
  2572. try
  2573. {
  2574. // If /INTERACTIVE switch is specified, obtain the user response.
  2575. if (IsInteractive(rParsedInfo) == TRUE)
  2576. {
  2577. _bstr_t bstrMsg;
  2578. while ( TRUE )
  2579. {
  2580. BOOL bInstanceLevel = TRUE;
  2581. if(IsClassOperation(rParsedInfo))
  2582. {
  2583. bInstanceLevel = FALSE;
  2584. }
  2585. else
  2586. {
  2587. _TCHAR *pszVerbName = rParsedInfo.GetCmdSwitchesObject().
  2588. GetVerbName();
  2589. if(CompareTokens(pszVerbName, CLI_TOKEN_CALL))
  2590. {
  2591. if ( rParsedInfo.GetCmdSwitchesObject().
  2592. GetAliasName() != NULL )
  2593. {
  2594. if (rParsedInfo.GetCmdSwitchesObject().
  2595. GetWhereExpression() == NULL)
  2596. {
  2597. bInstanceLevel = FALSE;
  2598. }
  2599. else
  2600. bInstanceLevel = TRUE;
  2601. }
  2602. else
  2603. {
  2604. if ((rParsedInfo.GetCmdSwitchesObject().
  2605. GetPathExpression() != NULL)
  2606. && (rParsedInfo.GetCmdSwitchesObject().
  2607. GetWhereExpression() == NULL))
  2608. {
  2609. bInstanceLevel = FALSE;
  2610. }
  2611. else
  2612. bInstanceLevel = TRUE;
  2613. }
  2614. }
  2615. else
  2616. bInstanceLevel = TRUE;
  2617. }
  2618. if(bInstanceLevel)
  2619. {
  2620. WMIFormatMessage(IDS_I_METH_EXEC_PROMPT2, 2, bstrMsg,
  2621. (LPWSTR) bstrPath, pszMethodName);
  2622. interOption = GetUserResponseEx((LPWSTR)bstrMsg);
  2623. }
  2624. else
  2625. {
  2626. WMIFormatMessage(IDS_I_METH_EXEC_PROMPT, 2, bstrMsg,
  2627. (LPWSTR) bstrPath, pszMethodName);
  2628. interOption = GetUserResponse((LPWSTR)bstrMsg);
  2629. }
  2630. if ( interOption == YES || interOption == NO )
  2631. break;
  2632. else if(interOption == HELP)
  2633. {
  2634. rParsedInfo.GetCmdSwitchesObject().
  2635. SetInformationCode(0);
  2636. ProcessSHOWInfo(rParsedInfo, FALSE, (LPWSTR)bstrPath);
  2637. }
  2638. }
  2639. }
  2640. if ( interOption == YES )
  2641. {
  2642. if (IsInteractive(rParsedInfo) == FALSE)
  2643. {
  2644. _bstr_t bstrMsg;
  2645. WMIFormatMessage(IDS_I_METH_EXEC_STATUS, 2, bstrMsg,
  2646. (LPWSTR) bstrPath, pszMethodName);
  2647. DisplayMessage((LPWSTR)bstrMsg, CP_OEMCP, FALSE, TRUE);
  2648. }
  2649. // Execute the method with the given input arguments
  2650. hr = m_pITargetNS->ExecMethod(bstrPath,
  2651. _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  2652. .GetMethodName()),
  2653. 0L,
  2654. NULL,
  2655. pIInParam,
  2656. &pIOutParam,
  2657. NULL);
  2658. if (m_bTrace || m_eloErrLogOpt)
  2659. {
  2660. chsMsg.Format(L"IWbemServices::ExecMethod(L\"%s\", L\"%s\", "
  2661. L"0, NULL, -, -, NULL)", (LPWSTR) bstrPath,
  2662. rParsedInfo.GetCmdSwitchesObject().GetMethodName()
  2663. ? rParsedInfo.GetCmdSwitchesObject().GetMethodName()
  2664. : L"<null>");
  2665. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2666. dwThreadId, rParsedInfo, m_bTrace);
  2667. }
  2668. ONFAILTHROWERROR(hr);
  2669. DisplayString(IDS_I_CALL_SUCCESS, CP_OEMCP, NULL, FALSE, TRUE);
  2670. // Check the method execution status.
  2671. if(pIOutParam)
  2672. {
  2673. _TCHAR szMsg[BUFFER1024] = NULL_STRING;
  2674. rParsedInfo.GetCmdSwitchesObject().
  2675. SetMethExecOutParam(pIOutParam);
  2676. DisplayMethExecOutput(rParsedInfo);
  2677. }
  2678. SAFEIRELEASE(pIOutParam);
  2679. }
  2680. }
  2681. catch(_com_error& e)
  2682. {
  2683. SAFEIRELEASE(pIOutParam);
  2684. VARIANTCLEAR(varTemp);
  2685. hr = e.Error();
  2686. }
  2687. catch(CHeap_Exception)
  2688. {
  2689. SAFEIRELEASE(pIOutParam);
  2690. VARIANTCLEAR(varTemp);
  2691. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2692. }
  2693. return hr;
  2694. }
  2695. /*------------------------------------------------------------------------
  2696. Name :DisplayMethExecOutput
  2697. Synopsis :Displays the result of execution of the method.
  2698. Type :Member Function
  2699. Input Parameter(s):
  2700. rParsedinfo - CParsedInfo object.
  2701. Output Parameter(s):None
  2702. Return Type :void
  2703. Global Variables :None
  2704. Calling Syntax :DisplayMethExecOutput(rParsedInfo)
  2705. Notes :None
  2706. ------------------------------------------------------------------------*/
  2707. void CExecEngine::DisplayMethExecOutput(CParsedInfo& rParsedInfo)
  2708. {
  2709. HRESULT hr = S_OK;
  2710. IWbemClassObject *pIOutParam = NULL;
  2711. CHString chsMsg;
  2712. DWORD dwThreadId = GetCurrentThreadId();
  2713. BSTR pstrMofTextOfObj = NULL;
  2714. VARIANT vtTemp;
  2715. VariantInit(&vtTemp);
  2716. try
  2717. {
  2718. _bstr_t bstrResult;
  2719. pIOutParam = rParsedInfo.GetCmdSwitchesObject().GetMethExecOutParam();
  2720. if ( pIOutParam != NULL )
  2721. {
  2722. hr = pIOutParam->GetObjectText(0, &pstrMofTextOfObj);
  2723. if (m_bTrace || m_eloErrLogOpt)
  2724. {
  2725. chsMsg.Format(L"IWbemClassObject->GetObjectText(0, -)");
  2726. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2727. dwThreadId, rParsedInfo, m_bTrace);
  2728. }
  2729. ONFAILTHROWERROR(hr);
  2730. DisplayString(IDS_I_OUT_PARAMS,CP_OEMCP, NULL, FALSE, TRUE);
  2731. DisplayMessage(_bstr_t(pstrMofTextOfObj), CP_OEMCP, FALSE, TRUE);
  2732. DisplayMessage(_T("\n"), CP_OEMCP, TRUE, TRUE);
  2733. SAFEBSTRFREE(pstrMofTextOfObj);
  2734. }
  2735. }
  2736. catch(_com_error& e)
  2737. {
  2738. SAFEBSTRFREE(pstrMofTextOfObj);
  2739. _com_issue_error(e.Error());
  2740. }
  2741. catch(CHeap_Exception)
  2742. {
  2743. SAFEBSTRFREE(pstrMofTextOfObj);
  2744. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2745. }
  2746. }
  2747. /*------------------------------------------------------------------------
  2748. Name :ExecOtherCmdLineUtlty
  2749. Synopsis :Invokes other command line utility specified in
  2750. Derivation of Verb if Verb Type is "CommandLine"
  2751. Type :Member Function
  2752. Input Parameter(s):
  2753. rParsedinfo - CParsedInfo object.
  2754. Output Parameter(s):None
  2755. Return Type :BOOL
  2756. Global Variables :None
  2757. Calling Syntax :ExecOtherCmdLineUtlty(rParsedInfo)
  2758. Notes :None
  2759. ------------------------------------------------------------------------*/
  2760. BOOL CExecEngine::ExecOtherCmdLineUtlty(CParsedInfo& rParsedInfo)
  2761. {
  2762. BOOL bRet = TRUE;
  2763. BOOL bInvalidNoOfArgs = FALSE;
  2764. if ( rParsedInfo.GetCmdSwitchesObject().GetNamedParamListFlag() == FALSE )
  2765. {
  2766. METHDETMAP mdpMethDetMap =
  2767. rParsedInfo.GetCmdSwitchesObject().GetMethDetMap();
  2768. METHDETMAP::iterator iMethDetMapItr = mdpMethDetMap.begin();
  2769. METHODDETAILS mdMethDet = (*iMethDetMapItr).second;
  2770. CHARVECTOR cvInParams =
  2771. rParsedInfo.GetCmdSwitchesObject().GetPropertyList();
  2772. PROPDETMAP pdmPropDetMap = mdMethDet.Params;
  2773. if ( !pdmPropDetMap.empty() )
  2774. {
  2775. if ( pdmPropDetMap.size() != cvInParams.size() )
  2776. bInvalidNoOfArgs = TRUE;
  2777. }
  2778. }
  2779. if ( bInvalidNoOfArgs == TRUE )
  2780. {
  2781. rParsedInfo.GetCmdSwitchesObject().SetErrataCode(
  2782. IDS_E_INVALID_NO_OF_PARAMS);
  2783. bRet = FALSE;
  2784. }
  2785. else
  2786. {
  2787. HRESULT hr = FormQueryAndExecuteMethodOrUtility(rParsedInfo);
  2788. bRet = FAILED(hr) ? FALSE : TRUE;
  2789. }
  2790. return bRet;
  2791. }
  2792. /*------------------------------------------------------------------------
  2793. Name :ProcessDELETEVerb
  2794. Synopsis :Processes the DELETE verb referring to the information
  2795. available with CParsedInfo object.
  2796. Type :Member Function
  2797. Input parameter :
  2798. rParsedInfo - reference to CParsedInfo class object
  2799. Output parameters :
  2800. rParsedInfo - reference to CParsedInfo class object
  2801. Return Type :BOOL
  2802. Global Variables :None
  2803. Calling Syntax :ProcessDELETEVerb(rParsedInfo)
  2804. Notes :None
  2805. ------------------------------------------------------------------------*/
  2806. BOOL CExecEngine::ProcessDELETEVerb(CParsedInfo& rParsedInfo)
  2807. {
  2808. // DELETE verb processing
  2809. BOOL bRet = TRUE;
  2810. HRESULT hr = S_OK;
  2811. try
  2812. {
  2813. _bstr_t bstrQuery(""), bstrObject("");
  2814. // If anyone of the following is specified:
  2815. // a) PATH <path expr>
  2816. // b) PATH <class path expr> WHERE <where expr>
  2817. if (rParsedInfo.GetCmdSwitchesObject().GetPathExpression() != NULL)
  2818. {
  2819. _bstr_t bstrClass = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  2820. .GetClassPath());
  2821. bstrObject = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  2822. .GetPathExpression());
  2823. // Form the query
  2824. bstrQuery = _bstr_t(L"SELECT * FROM ") + bstrClass ;
  2825. // If WHERE expresion is given
  2826. if (rParsedInfo.GetCmdSwitchesObject().
  2827. GetWhereExpression() != NULL)
  2828. {
  2829. bstrQuery += _bstr_t(L" WHERE ")
  2830. + _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  2831. .GetWhereExpression());
  2832. }
  2833. }
  2834. // If <alias> WHERE expression is specified.
  2835. else if (rParsedInfo.GetCmdSwitchesObject().GetWhereExpression()
  2836. != NULL)
  2837. {
  2838. rParsedInfo.GetCmdSwitchesObject().
  2839. GetClassOfAliasTarget(bstrObject);
  2840. bstrQuery = _bstr_t(L"SELECT * FROM ")
  2841. + bstrObject
  2842. + _bstr_t(L" WHERE ")
  2843. + _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  2844. .GetWhereExpression());
  2845. }
  2846. // If CLASS is specified.
  2847. else if (rParsedInfo.GetCmdSwitchesObject().GetClassPath() != NULL)
  2848. {
  2849. bstrObject = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  2850. .GetClassPath());
  2851. }
  2852. // if Alias name is specified
  2853. else
  2854. {
  2855. rParsedInfo.GetCmdSwitchesObject().
  2856. GetClassOfAliasTarget(bstrObject);
  2857. bstrQuery = _bstr_t (L"SELECT * FROM ")
  2858. +bstrObject;
  2859. }
  2860. // Connect to WMI namespace
  2861. if (m_pITargetNS == NULL)
  2862. {
  2863. if ( IsFailFastAndNodeExist(rParsedInfo) == TRUE )
  2864. {
  2865. hr = ConnectToTargetNS(rParsedInfo);
  2866. ONFAILTHROWERROR(hr);
  2867. }
  2868. else
  2869. hr = E_FAIL; // Explicitly set error
  2870. }
  2871. if (SUCCEEDED(hr))
  2872. bRet = DeleteObjects(rParsedInfo, bstrQuery, bstrObject);
  2873. else
  2874. bRet = FALSE;
  2875. }
  2876. catch(_com_error& e)
  2877. {
  2878. _com_issue_error(e.Error());
  2879. bRet = FALSE;
  2880. }
  2881. return bRet;
  2882. }
  2883. /*------------------------------------------------------------------------
  2884. Name :DeleteObjects
  2885. Synopsis :This function deletes the instances(s) or class
  2886. specified for deletion.
  2887. Type :Member Function
  2888. Input parameter :
  2889. rParsedInfo - reference to the CParsedInfo object
  2890. bstrQuery - String consist of WQL query
  2891. bstrObject - String consist of object path
  2892. Output parameters :
  2893. rParsedInfo - reference to the CParsedInfo object
  2894. Return Type :BOOL
  2895. Global Variables :None
  2896. Calling Syntax :DeleteObjects(rParsedInfo, bstrQuery, bstrObject)
  2897. Notes :None
  2898. ------------------------------------------------------------------------*/
  2899. BOOL CExecEngine::DeleteObjects(CParsedInfo& rParsedInfo,
  2900. _bstr_t& bstrQuery, _bstr_t& bstrObject)
  2901. {
  2902. HRESULT hr = S_OK;
  2903. BOOL bSuccess = TRUE;
  2904. IEnumWbemClassObject *pIEnumObj = NULL;
  2905. IWbemClassObject *pIWbemObj = NULL;
  2906. ULONG ulReturned = 0;
  2907. INTEROPTION interOption = YES;
  2908. CHString chsMsg;
  2909. DWORD dwThreadId = GetCurrentThreadId();
  2910. VARIANT vtPath;
  2911. VariantInit(&vtPath);
  2912. try
  2913. {
  2914. _bstr_t bstrResult;
  2915. if (bstrQuery == _bstr_t(""))
  2916. {
  2917. // If /INTERACTIVE switch is specified, obtain the user response.
  2918. if (IsInteractive(rParsedInfo) == TRUE)
  2919. {
  2920. _bstr_t bstrMsg;
  2921. while ( TRUE )
  2922. {
  2923. if(IsClassOperation(rParsedInfo))
  2924. {
  2925. WMIFormatMessage(IDS_I_DELETE_CLASS_PROMPT, 1, bstrMsg,
  2926. (LPWSTR) bstrObject);
  2927. interOption = GetUserResponse((LPWSTR)bstrMsg);
  2928. }
  2929. else
  2930. {
  2931. WMIFormatMessage(IDS_I_DELETE_CLASS_PROMPT2, 1, bstrMsg,
  2932. (LPWSTR) bstrObject);
  2933. interOption = GetUserResponseEx((LPWSTR)bstrMsg);
  2934. }
  2935. if ( interOption == YES || interOption == NO )
  2936. break;
  2937. else if(interOption == HELP)
  2938. {
  2939. rParsedInfo.GetCmdSwitchesObject().
  2940. SetInformationCode(0);
  2941. ProcessSHOWInfo(rParsedInfo, FALSE,
  2942. (LPWSTR)bstrObject);
  2943. }
  2944. }
  2945. }
  2946. if (interOption == YES)
  2947. {
  2948. // If instance path is specified then delete the instance
  2949. // properties otherwise delete the class
  2950. if(!rParsedInfo.GetCmdSwitchesObject().GetWhereExpression())
  2951. {
  2952. // If WHERE expression is NULL then delete the WMI class
  2953. hr = m_pITargetNS->DeleteClass(bstrObject, 0, NULL, NULL);
  2954. if (m_bTrace || m_eloErrLogOpt)
  2955. {
  2956. chsMsg.Format(L"IWbemServices::DeleteClass"
  2957. L"(L\"%s\", 0, NULL, NULL)", (LPWSTR)bstrObject);
  2958. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2959. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, m_bTrace);
  2960. }
  2961. ONFAILTHROWERROR(hr);
  2962. DisplayString(IDS_I_DELETE_SUCCESS,CP_OEMCP,
  2963. NULL, FALSE, TRUE);
  2964. }
  2965. else
  2966. {
  2967. // If WHERE expression is not NULL then delete the
  2968. // WMI instance
  2969. DisplayString(IDS_I_DELETING_INSTANCE,
  2970. CP_OEMCP,(LPWSTR)vtPath.bstrVal,
  2971. FALSE, TRUE);
  2972. hr = m_pITargetNS->DeleteInstance(bstrObject,
  2973. 0, NULL, NULL);
  2974. if (m_bTrace || m_eloErrLogOpt)
  2975. {
  2976. chsMsg.Format(L"IWbemServices::DeleteInstance"
  2977. L"(L\"%s\", 0, NULL, NULL)",
  2978. (LPWSTR) bstrObject);
  2979. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2980. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo,
  2981. m_bTrace);
  2982. }
  2983. ONFAILTHROWERROR(hr);
  2984. DisplayString(IDS_I_INSTANCE_DELETE_SUCCESS,
  2985. CP_OEMCP, NULL, FALSE, TRUE);
  2986. }
  2987. }
  2988. }
  2989. else
  2990. {
  2991. // Execute the query to get collection of objects
  2992. hr = m_pITargetNS->ExecQuery(_bstr_t(L"WQL"), bstrQuery,
  2993. 0, NULL, &pIEnumObj);
  2994. if (m_bTrace || m_eloErrLogOpt)
  2995. {
  2996. chsMsg.Format(L"IWbemServices::ExecQuery(L\"WQL\","
  2997. L"L\"%s\", 0, NULL, -)", (LPWSTR)bstrQuery);
  2998. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2999. dwThreadId, rParsedInfo, m_bTrace);
  3000. }
  3001. ONFAILTHROWERROR(hr);
  3002. // Set the interface security
  3003. hr = SetSecurity(pIEnumObj,
  3004. rParsedInfo.GetAuthorityPrinciple(),
  3005. rParsedInfo.GetNode(),
  3006. rParsedInfo.GetUser(),
  3007. rParsedInfo.GetPassword(),
  3008. rParsedInfo.GetGlblSwitchesObject().
  3009. GetAuthenticationLevel(),
  3010. rParsedInfo.GetGlblSwitchesObject().
  3011. GetImpersonationLevel());
  3012. if (m_bTrace || m_eloErrLogOpt)
  3013. {
  3014. _TCHAR* pszAuthority = rParsedInfo.GetAuthorityPrinciple();
  3015. if( pszAuthority != NULL &&
  3016. _tcslen(pszAuthority) > 9 &&
  3017. _tcsnicmp(pszAuthority, _T("KERBEROS:"), 9) == 0)
  3018. {
  3019. BSTR bstrPrincipalName = ::SysAllocString(&pszAuthority[9]);
  3020. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_GSS_KERBEROS,"
  3021. L"RPC_C_AUTHZ_NONE, %s, %d, %d, -, EOAC_NONE)",
  3022. (LPWSTR)bstrPrincipalName,
  3023. rParsedInfo.GetGlblSwitchesObject().
  3024. GetAuthenticationLevel(),
  3025. rParsedInfo.GetGlblSwitchesObject().
  3026. GetImpersonationLevel());
  3027. SAFEBSTRFREE(bstrPrincipalName);
  3028. }
  3029. else
  3030. {
  3031. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_WINNT, "
  3032. L"RPC_C_AUTHZ_NONE, NULL, %d, %d, -, EOAC_NONE)",
  3033. rParsedInfo.GetGlblSwitchesObject().
  3034. GetAuthenticationLevel(),
  3035. rParsedInfo.GetGlblSwitchesObject().
  3036. GetImpersonationLevel());
  3037. }
  3038. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3039. dwThreadId, rParsedInfo, m_bTrace);
  3040. }
  3041. ONFAILTHROWERROR(hr);
  3042. BOOL bInstances = FALSE;
  3043. BOOL bInteractive = IsInteractive(rParsedInfo);
  3044. hr = pIEnumObj->Next(WBEM_INFINITE, 1, &pIWbemObj, &ulReturned);
  3045. // Set this property in all objects of the collection
  3046. while (ulReturned == 1)
  3047. {
  3048. INTEROPTION interOption = YES;
  3049. bInstances = TRUE;
  3050. VariantInit(&vtPath);
  3051. // Get the object path.
  3052. hr = pIWbemObj->Get(_bstr_t(L"__PATH"), 0, &vtPath, NULL, NULL);
  3053. if (m_bTrace || m_eloErrLogOpt)
  3054. {
  3055. chsMsg.Format(
  3056. L"IWbemClassObject::Get(L\"__PATH\", 0, -, 0, 0)");
  3057. GetBstrTFromVariant(vtPath, bstrResult);
  3058. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3059. dwThreadId, rParsedInfo, m_bTrace, 0, bstrResult);
  3060. }
  3061. ONFAILTHROWERROR(hr);
  3062. // If /INTERACTIVE switch is specified, obtain user response.
  3063. if (IsInteractive(rParsedInfo) == TRUE)
  3064. {
  3065. _bstr_t bstrMsg;
  3066. while ( TRUE )
  3067. {
  3068. if(IsClassOperation(rParsedInfo))
  3069. {
  3070. WMIFormatMessage(IDS_I_DELETE_CLASS_PROMPT,
  3071. 1, bstrMsg, (LPWSTR) vtPath.bstrVal);
  3072. interOption = GetUserResponse((LPWSTR)bstrMsg);
  3073. }
  3074. else
  3075. {
  3076. WMIFormatMessage(IDS_I_DELETE_CLASS_PROMPT2, 1,
  3077. bstrMsg, (LPWSTR) vtPath.bstrVal);
  3078. interOption = GetUserResponseEx((LPWSTR)bstrMsg);
  3079. }
  3080. if ( interOption == YES || interOption == NO )
  3081. break;
  3082. else if(interOption == HELP)
  3083. {
  3084. rParsedInfo.GetCmdSwitchesObject().
  3085. SetInformationCode(0);
  3086. ProcessSHOWInfo(rParsedInfo, FALSE,
  3087. (LPWSTR)vtPath.bstrVal);
  3088. }
  3089. }
  3090. }
  3091. if (interOption == YES)
  3092. {
  3093. DisplayString(IDS_I_DELETING_INSTANCE,
  3094. CP_OEMCP,(LPWSTR)vtPath.bstrVal, FALSE, TRUE);
  3095. hr = m_pITargetNS->DeleteInstance(vtPath.bstrVal,
  3096. 0, NULL, NULL);
  3097. if (m_bTrace || m_eloErrLogOpt)
  3098. {
  3099. chsMsg.Format(L"IWbemServices::DeleteInstance"
  3100. L"(L\"%s\", 0, NULL, NULL)",
  3101. (LPWSTR) vtPath.bstrVal);
  3102. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  3103. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, m_bTrace);
  3104. }
  3105. ONFAILTHROWERROR(hr);
  3106. VARIANTCLEAR(vtPath);
  3107. SAFEIRELEASE(pIWbemObj);
  3108. DisplayString(IDS_I_INSTANCE_DELETE_SUCCESS,CP_OEMCP,
  3109. NULL, FALSE, TRUE);
  3110. }
  3111. hr = pIEnumObj->Next( WBEM_INFINITE, 1, &pIWbemObj,
  3112. &ulReturned);
  3113. }
  3114. SAFEIRELEASE(pIEnumObj);
  3115. // If no instances are available
  3116. if (!bInstances)
  3117. {
  3118. rParsedInfo.GetCmdSwitchesObject().
  3119. SetInformationCode(IDS_I_NO_INSTANCES);
  3120. }
  3121. }
  3122. }
  3123. catch(_com_error& e)
  3124. {
  3125. VARIANTCLEAR(vtPath);
  3126. SAFEIRELEASE(pIEnumObj);
  3127. SAFEIRELEASE(pIWbemObj);
  3128. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  3129. bSuccess = FALSE;
  3130. }
  3131. catch(CHeap_Exception)
  3132. {
  3133. VARIANTCLEAR(vtPath);
  3134. SAFEIRELEASE(pIEnumObj);
  3135. SAFEIRELEASE(pIWbemObj);
  3136. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  3137. }
  3138. return bSuccess;
  3139. }
  3140. /*------------------------------------------------------------------------
  3141. Name :GetUserResponse
  3142. Synopsis :This function accepts the user response before going
  3143. ahead, when /INTERACTIVE is specified at the verb
  3144. level
  3145. Type :Member Function
  3146. Input parameter :
  3147. pszMsg - message to be displayed.
  3148. Output parameters :None
  3149. Return Type :INTEROPTION
  3150. Global Variables :None
  3151. Calling Syntax :GetUserResponse(pszMsg)
  3152. Notes :None
  3153. ------------------------------------------------------------------------*/
  3154. INTEROPTION CExecEngine::GetUserResponse(_TCHAR* pszMsg)
  3155. {
  3156. INTEROPTION bRet = YES;
  3157. _TCHAR szResp[BUFFER255] = NULL_STRING;
  3158. _TCHAR *pBuf = NULL;
  3159. if (pszMsg == NULL)
  3160. bRet = NO;
  3161. if(bRet != NO)
  3162. {
  3163. // Get the user response, till 'Y' - yes or 'N' - no
  3164. // is keyed in
  3165. while(TRUE)
  3166. {
  3167. DisplayMessage(pszMsg, CP_OEMCP, TRUE, TRUE);
  3168. pBuf = _fgetts(szResp, BUFFER255-1, stdin);
  3169. if(pBuf != NULL)
  3170. {
  3171. LONG lInStrLen = lstrlen(szResp);
  3172. if(szResp[lInStrLen - 1] == _T('\n'))
  3173. szResp[lInStrLen - 1] = _T('\0');
  3174. }
  3175. else if ( g_wmiCmd.GetBreakEvent() != TRUE )
  3176. {
  3177. lstrcpy(szResp, RESPONSE_NO);
  3178. DisplayMessage(_T("\n"), CP_OEMCP, TRUE, TRUE);
  3179. }
  3180. if ( g_wmiCmd.GetBreakEvent() == TRUE )
  3181. {
  3182. g_wmiCmd.SetBreakEvent(FALSE);
  3183. lstrcpy(szResp, RESPONSE_NO);
  3184. DisplayMessage(_T("\n"), CP_OEMCP, TRUE, TRUE);
  3185. }
  3186. if (CompareTokens(szResp, RESPONSE_YES)
  3187. || CompareTokens(szResp, RESPONSE_NO))
  3188. break;
  3189. }
  3190. if (CompareTokens(szResp, RESPONSE_NO))
  3191. bRet = NO;
  3192. }
  3193. return bRet;
  3194. }
  3195. /*------------------------------------------------------------------------
  3196. Name :ProcessCREATEVerb
  3197. Synopsis :Processes the CREATE verb referring to the information
  3198. available with CParsedInfo object.
  3199. Type :Member Function
  3200. Input parameter :
  3201. rParsedInfo - reference to CParsedInfo class object
  3202. Output parameters :
  3203. rParsedInfo - reference to CParsedInfo class object
  3204. Return Type :BOOL
  3205. Global Variables :None
  3206. Calling Syntax :ProcessCREATEVerb(rParsedInfo)
  3207. Notes :None
  3208. ------------------------------------------------------------------------*/
  3209. BOOL CExecEngine::ProcessCREATEVerb(CParsedInfo& rParsedInfo)
  3210. {
  3211. // CREATE verb processing
  3212. BOOL bRet = TRUE;
  3213. INTEROPTION interCreate = YES;
  3214. HRESULT hr = S_OK;
  3215. try
  3216. {
  3217. _bstr_t bstrClass("");
  3218. // If object PATH expression is specified.
  3219. if (rParsedInfo.GetCmdSwitchesObject().GetPathExpression() != NULL)
  3220. {
  3221. bstrClass = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  3222. .GetClassPath());
  3223. }
  3224. // If CLASS is specified.
  3225. else if (rParsedInfo.GetCmdSwitchesObject().GetClassPath() != NULL)
  3226. {
  3227. bstrClass = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  3228. .GetClassPath());
  3229. }
  3230. // if Alias name is specified
  3231. else
  3232. {
  3233. rParsedInfo.GetCmdSwitchesObject().
  3234. GetClassOfAliasTarget(bstrClass);
  3235. }
  3236. // Check if interactive mode
  3237. if (IsInteractive(rParsedInfo) == TRUE)
  3238. {
  3239. _bstr_t bstrMsg;
  3240. WMIFormatMessage(IDS_I_CREATE_INST_PROMPT, 1,
  3241. bstrMsg, (LPWSTR) bstrClass);
  3242. // Get the user response.
  3243. interCreate = GetUserResponse((LPWSTR)bstrMsg);
  3244. }
  3245. if (interCreate == YES)
  3246. {
  3247. // Connect to WMI namespace
  3248. if (m_pITargetNS == NULL)
  3249. {
  3250. if ( IsFailFastAndNodeExist(rParsedInfo) == TRUE )
  3251. {
  3252. hr = ConnectToTargetNS(rParsedInfo);
  3253. ONFAILTHROWERROR(hr);
  3254. }
  3255. else
  3256. hr = E_FAIL; // Explicitly set error
  3257. }
  3258. if (SUCCEEDED(hr))
  3259. {
  3260. // Validate the property values against the property
  3261. // qualifier information if available.
  3262. if (rParsedInfo.GetCmdSwitchesObject().GetAliasName() != NULL)
  3263. {
  3264. // Validate the input parameters against the alias
  3265. // qualifier information.
  3266. bRet = ValidateAlaisInParams(rParsedInfo);
  3267. }
  3268. else
  3269. {
  3270. // Validate the input parameters against the class
  3271. // qualifier information
  3272. bRet = ValidateInParams(rParsedInfo, bstrClass);
  3273. }
  3274. if (bRet)
  3275. {
  3276. // Set the values passed as input to the appropriate properties.
  3277. bRet = CreateInstance(rParsedInfo, bstrClass);
  3278. }
  3279. }
  3280. else
  3281. bRet = FALSE;
  3282. }
  3283. else
  3284. {
  3285. // Message to be displayed to the user
  3286. rParsedInfo.GetCmdSwitchesObject().
  3287. SetInformationCode(IDS_I_NOCREATE);
  3288. }
  3289. }
  3290. catch(_com_error& e)
  3291. {
  3292. _com_issue_error(e.Error());
  3293. bRet = FALSE;
  3294. }
  3295. return bRet;
  3296. }
  3297. /*------------------------------------------------------------------------
  3298. Name :CreateInstance
  3299. Synopsis :This function creates an instance of the specified
  3300. class
  3301. Type :Member Function
  3302. Input parameter :
  3303. rParsedInfo - reference to the CParsedInfo object
  3304. bstrClass - classname
  3305. Output parameters :
  3306. rParsedInfo - reference to the CParsedInfo object
  3307. Return Type :BOOL
  3308. Global Variables :None
  3309. Calling Syntax :CreateInstance(rParsedInfo, bstrClass)
  3310. Notes :None
  3311. ------------------------------------------------------------------------*/
  3312. BOOL CExecEngine::CreateInstance(CParsedInfo& rParsedInfo, BSTR bstrClass)
  3313. {
  3314. HRESULT hr = S_OK;
  3315. IWbemClassObject *pIWbemObj = NULL;
  3316. IWbemClassObject *pINewInst = NULL;
  3317. IWbemQualifierSet *pIQualSet = NULL;
  3318. BOOL bSuccess = TRUE;
  3319. DWORD dwThreadId = GetCurrentThreadId();
  3320. CHString chsMsg;
  3321. VARIANT varType,
  3322. varSrc,
  3323. varDest;
  3324. VariantInit(&varSrc);
  3325. VariantInit(&varDest);
  3326. VariantInit(&varType);
  3327. // Obtain the list of properties and their associated values
  3328. BSTRMAP theMap = rParsedInfo.GetCmdSwitchesObject().GetParameterMap();
  3329. BSTRMAP::iterator theIterator = NULL;
  3330. theIterator = theMap.begin();
  3331. try
  3332. {
  3333. _bstr_t bstrResult;
  3334. // Get the class definition
  3335. hr = m_pITargetNS->GetObject(bstrClass,
  3336. 0, NULL, &pIWbemObj, NULL);
  3337. if (m_bTrace || m_eloErrLogOpt)
  3338. {
  3339. chsMsg.Format(L"IWbemServices::GetObject(L\"%s\", 0, NULL, -)",
  3340. (LPWSTR) bstrClass);
  3341. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3342. dwThreadId, rParsedInfo, m_bTrace);
  3343. }
  3344. ONFAILTHROWERROR(hr);
  3345. // Create a new instance.
  3346. hr = pIWbemObj->SpawnInstance(0, &pINewInst);
  3347. if ( m_eloErrLogOpt )
  3348. {
  3349. chsMsg.Format(L"IWbemClassObject::SpawnInstance(0, -)");
  3350. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3351. dwThreadId, rParsedInfo, FALSE);
  3352. }
  3353. ONFAILTHROWERROR(hr);
  3354. PROPDETMAP pdmPropDetMap = rParsedInfo.GetCmdSwitchesObject().
  3355. GetPropDetMap();
  3356. PROPDETMAP::iterator itrPropDetMap;
  3357. // Update all properties
  3358. while (theIterator != theMap.end())
  3359. {
  3360. // Get the propert name and the corresponding value
  3361. _bstr_t bstrProp = _bstr_t((_TCHAR*)(*theIterator).first);
  3362. // Get the derivation of property name
  3363. if ( Find(pdmPropDetMap, bstrProp, itrPropDetMap) == TRUE &&
  3364. !((*itrPropDetMap).second.Derivation) == FALSE )
  3365. bstrProp = (*itrPropDetMap).second.Derivation;
  3366. // Obtain the property qualifier set for the property
  3367. hr = pINewInst->GetPropertyQualifierSet(bstrProp, &pIQualSet);
  3368. if (m_bTrace || m_eloErrLogOpt)
  3369. {
  3370. chsMsg.Format(L"IWbemClassObject::GetPropertyQualifierSet"
  3371. L"(L\"%s\", -)", (LPWSTR) bstrProp);
  3372. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3373. dwThreadId, rParsedInfo, m_bTrace);
  3374. }
  3375. ONFAILTHROWERROR(hr);
  3376. VariantInit(&varType);
  3377. if (pIQualSet)
  3378. {
  3379. // Obtain the CIM type of the property
  3380. hr = pIQualSet->Get(_bstr_t(L"CIMTYPE"), 0L, &varType, NULL);
  3381. if (m_bTrace || m_eloErrLogOpt)
  3382. {
  3383. chsMsg.Format(L"IWbemQualifierSet::Get(L\"CIMTYPE\", "
  3384. L"0, -, NULL)");
  3385. GetBstrTFromVariant(varType, bstrResult);
  3386. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3387. dwThreadId, rParsedInfo, m_bTrace, 0, bstrResult);
  3388. }
  3389. ONFAILTHROWERROR(hr);
  3390. if(!IsArrayType(pINewInst, bstrProp))
  3391. {
  3392. varSrc.vt = VT_BSTR;
  3393. varSrc.bstrVal = SysAllocString((_TCHAR*)(*theIterator).second);
  3394. if (varSrc.bstrVal == NULL)
  3395. {
  3396. //Reset the variant, it will be cleaned up by the catch...
  3397. VariantInit(&varSrc);
  3398. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  3399. }
  3400. hr = ConvertCIMTYPEToVarType(varDest, varSrc,
  3401. (_TCHAR*)varType.bstrVal);
  3402. if ( m_eloErrLogOpt )
  3403. {
  3404. chsMsg.Format(L"VariantChangeType(-, -, 0, -)");
  3405. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3406. dwThreadId, rParsedInfo, FALSE);
  3407. }
  3408. ONFAILTHROWERROR(hr);
  3409. }
  3410. else
  3411. {
  3412. BSTRVECTOR vArrayValues;
  3413. _TCHAR* pszValue = (*theIterator).second;
  3414. RemoveParanthesis(pszValue);
  3415. GetArrayFromToken(pszValue,
  3416. vArrayValues);
  3417. hr = CheckForArray( pINewInst, bstrProp,
  3418. varDest, vArrayValues, rParsedInfo);
  3419. ONFAILTHROWERROR(hr);
  3420. }
  3421. VARIANTCLEAR(varType);
  3422. SAFEIRELEASE(pIQualSet);
  3423. }
  3424. // Update the property value
  3425. hr = pINewInst->Put(bstrProp, 0, &varDest, 0);
  3426. if (m_bTrace || m_eloErrLogOpt)
  3427. {
  3428. chsMsg.Format(L"IWbemClassObject::Put(L\"%s\", 0, -, 0)",
  3429. (LPWSTR) bstrProp);
  3430. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3431. dwThreadId, rParsedInfo, m_bTrace);
  3432. }
  3433. ONFAILTHROWERROR(hr);
  3434. VARIANTCLEAR(varSrc);
  3435. VARIANTCLEAR(varDest);
  3436. theIterator++;
  3437. }
  3438. // Update the instance with the changes
  3439. hr = m_pITargetNS->PutInstance(pINewInst, WBEM_FLAG_CREATE_ONLY,
  3440. NULL, NULL);
  3441. if (m_bTrace || m_eloErrLogOpt)
  3442. {
  3443. chsMsg.Format(L"IWbemServices::PutInstance(-, "
  3444. L"WBEM_FLAG_CREATE_ONLY, NULL, NULL)");
  3445. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3446. dwThreadId, rParsedInfo, m_bTrace);
  3447. }
  3448. ONFAILTHROWERROR(hr);
  3449. SAFEIRELEASE(pINewInst);
  3450. rParsedInfo.GetCmdSwitchesObject().
  3451. SetInformationCode(IDS_I_CREATE_SUCCESS);
  3452. }
  3453. catch(_com_error& e)
  3454. {
  3455. VARIANTCLEAR(varSrc);
  3456. VARIANTCLEAR(varDest);
  3457. VARIANTCLEAR(varType);
  3458. SAFEIRELEASE(pIQualSet);
  3459. SAFEIRELEASE(pIWbemObj);
  3460. SAFEIRELEASE(pINewInst);
  3461. // Store the COM error and set the return value to FALSE
  3462. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  3463. bSuccess = FALSE;
  3464. }
  3465. catch(CHeap_Exception)
  3466. {
  3467. VARIANTCLEAR(varSrc);
  3468. VARIANTCLEAR(varDest);
  3469. VARIANTCLEAR(varType);
  3470. SAFEIRELEASE(pIQualSet);
  3471. SAFEIRELEASE(pIWbemObj);
  3472. SAFEIRELEASE(pINewInst);
  3473. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  3474. }
  3475. return bSuccess;
  3476. }
  3477. /*------------------------------------------------------------------------
  3478. Name :ValidateInParams
  3479. Synopsis :Validates the property value specified against the
  3480. property qualifiers for that property (i.e checking
  3481. against the contents of following qualifiers if
  3482. available:
  3483. 1. MaxLen,
  3484. 2. ValueMap
  3485. 3. Values
  3486. Type :Member Function
  3487. Input Parameter(s):
  3488. rParsedinfo - CParsedInfo object.
  3489. bstrClass - Bstr type, class name.
  3490. Output Parameter(s):None
  3491. Return Type :BOOL
  3492. Global Variables :None
  3493. Calling Syntax :ValidateInParams(rParsedInfo, bstrClass)
  3494. Notes :None
  3495. ------------------------------------------------------------------------*/
  3496. BOOL CExecEngine::ValidateInParams(CParsedInfo& rParsedInfo, _bstr_t bstrClass)
  3497. {
  3498. HRESULT hr = S_OK;
  3499. IWbemClassObject *pIObject = NULL;
  3500. IWbemQualifierSet *pIQualSet = NULL;
  3501. BOOL bRet = TRUE;
  3502. CHString chsMsg;
  3503. VARIANT varMap,
  3504. varValue,
  3505. varLen;
  3506. VariantInit(&varMap);
  3507. VariantInit(&varValue);
  3508. VariantInit(&varLen);
  3509. BSTRMAP theMap = rParsedInfo.GetCmdSwitchesObject().GetParameterMap();
  3510. BSTRMAP::iterator theIterator = theMap.begin();
  3511. DWORD dwThreadId = GetCurrentThreadId();
  3512. try
  3513. {
  3514. // Obtain the class schema
  3515. hr = m_pITargetNS->GetObject(bstrClass,
  3516. WBEM_FLAG_RETURN_WBEM_COMPLETE |
  3517. WBEM_FLAG_USE_AMENDED_QUALIFIERS,
  3518. NULL,
  3519. &pIObject,
  3520. NULL);
  3521. if (m_bTrace || m_eloErrLogOpt)
  3522. {
  3523. chsMsg.Format(L"IWbemServices::GetObject(L\"%s\", "
  3524. L"WBEM_FLAG_RETURN_WBEM_COMPLETE|WBEM_FLAG_USE_AMENDED_QUALIFIERS,"
  3525. L" 0, NULL, -)", (LPWSTR) bstrClass);
  3526. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg, dwThreadId,
  3527. rParsedInfo, m_bTrace);
  3528. }
  3529. ONFAILTHROWERROR(hr);
  3530. // Loop through the list of available properties.
  3531. while (theIterator != theMap.end())
  3532. {
  3533. // Get the property name and the corresponding value.
  3534. _bstr_t bstrProp = _bstr_t((_TCHAR*)(*theIterator).first);
  3535. WCHAR* pszValue = (LPWSTR)(*theIterator).second;
  3536. // Check the value against the qualifier information
  3537. bRet = CheckQualifierInfo(rParsedInfo, pIObject,
  3538. bstrProp, pszValue);
  3539. if (bRet)
  3540. {
  3541. // A mapping between 'Values' and 'ValueMaps' is possible,
  3542. // hence update the parameter value to reflect the change.
  3543. rParsedInfo.GetCmdSwitchesObject().
  3544. UpdateParameterValue(bstrProp, _bstr_t(pszValue));
  3545. }
  3546. else
  3547. break;
  3548. theIterator++;
  3549. }
  3550. }
  3551. catch(_com_error& e)
  3552. {
  3553. SAFEIRELEASE(pIObject);
  3554. SAFEIRELEASE(pIQualSet);
  3555. VARIANTCLEAR(varMap);
  3556. VARIANTCLEAR(varValue);
  3557. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  3558. bRet = FALSE;
  3559. }
  3560. catch(CHeap_Exception)
  3561. {
  3562. SAFEIRELEASE(pIObject);
  3563. SAFEIRELEASE(pIQualSet);
  3564. VARIANTCLEAR(varMap);
  3565. VARIANTCLEAR(varValue);
  3566. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  3567. }
  3568. return bRet;
  3569. }
  3570. /*------------------------------------------------------------------------
  3571. Name :IsInteractive()
  3572. Synopsis :Checks whether user has to be prompted for response
  3573. Type :Member Function
  3574. Input Parameter(s):
  3575. rParsedinfo - reference to CParsedInfo class object.
  3576. Output Parameter(s):None
  3577. Return Type :BOOL
  3578. Global Variables :None
  3579. Calling Syntax :IsInteractive(rParsedInfo)
  3580. Notes :None
  3581. ------------------------------------------------------------------------*/
  3582. BOOL CExecEngine::IsInteractive(CParsedInfo& rParsedInfo)
  3583. {
  3584. BOOL bInteractive = FALSE;
  3585. // Get the status of /INTERACTIVE global switch.
  3586. bInteractive = rParsedInfo.GetGlblSwitchesObject().
  3587. GetInteractiveStatus();
  3588. // If /NOINTERACTIVE specified at the verb level.
  3589. if (rParsedInfo.GetCmdSwitchesObject().
  3590. GetInteractiveMode() == NOINTERACTIVE)
  3591. {
  3592. bInteractive = FALSE;
  3593. }
  3594. else if (rParsedInfo.GetCmdSwitchesObject().
  3595. GetInteractiveMode() == INTERACTIVE)
  3596. {
  3597. bInteractive = TRUE;
  3598. }
  3599. return bInteractive;
  3600. }
  3601. /*------------------------------------------------------------------------
  3602. Name :CheckQualifierInfo
  3603. Synopsis :Validates the parameter value specified against the
  3604. parameter qualifiers for that parameter(i.e checking
  3605. against the contents of following qualifiers if
  3606. available:
  3607. 1. MaxLen,
  3608. 2. ValueMap
  3609. 3. Values
  3610. Type :Member Function
  3611. Input Parameter(s):
  3612. rParsedinfo - CParsedInfo object.
  3613. pIMethodSign - input signature of the method.
  3614. bstrParam - parameter name
  3615. pszValue - new value.
  3616. Output Parameter(s):
  3617. pszValue - new value.
  3618. Return Type :BOOL
  3619. Global Variables :None
  3620. Calling Syntax :CheckQualifierInfo(rParsedInfo, pIMethodSign,
  3621. bstrParam, pszValue)
  3622. Notes :None
  3623. ------------------------------------------------------------------------*/
  3624. BOOL CExecEngine::CheckQualifierInfo(CParsedInfo& rParsedInfo,
  3625. IWbemClassObject *pIObject,
  3626. _bstr_t bstrParam,
  3627. WCHAR*& pszValue)
  3628. {
  3629. HRESULT hr = S_OK;
  3630. IWbemQualifierSet *pIQualSet = NULL;
  3631. BOOL bRet = TRUE;
  3632. CHString chsMsg;
  3633. DWORD dwThreadId = GetCurrentThreadId();
  3634. VARIANT varMap,
  3635. varValue,
  3636. varLen;
  3637. VariantInit(&varMap);
  3638. VariantInit(&varValue);
  3639. VariantInit(&varLen);
  3640. try
  3641. {
  3642. BOOL bFound = FALSE;
  3643. // Obtain the property qualifier set for the parameter.
  3644. hr= pIObject->GetPropertyQualifierSet(bstrParam, &pIQualSet);
  3645. if (m_bTrace || m_eloErrLogOpt)
  3646. {
  3647. chsMsg.Format(L"IWbemClassObject::GetPropertyQualifierSet"
  3648. L"(L\"%s\", -)", (LPWSTR) bstrParam);
  3649. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3650. dwThreadId, rParsedInfo, m_bTrace);
  3651. }
  3652. ONFAILTHROWERROR(hr);
  3653. // Check whether the 'Maxlen' qualifier is applicable.
  3654. pIQualSet->Get(_bstr_t(L"MaxLen"), 0, &varLen, NULL);
  3655. if (varLen.vt != VT_EMPTY && varLen.vt != VT_NULL)
  3656. {
  3657. // If the property value length exceeds maximum length
  3658. // allowed set the return value to FALSE
  3659. if (lstrlen(pszValue) > varLen.lVal)
  3660. {
  3661. rParsedInfo.GetCmdSwitchesObject().
  3662. SetErrataCode(IDS_E_VALUE_EXCEEDS_MAXLEN);
  3663. bRet = FALSE;
  3664. }
  3665. }
  3666. VARIANTCLEAR(varLen);
  3667. if (bRet)
  3668. {
  3669. bool bValue = false;
  3670. WMICLIINT iValue = GetNumber ( pszValue );
  3671. if ( iValue && iValue != -1 )
  3672. {
  3673. bValue = true;
  3674. }
  3675. // Obtain the 'ValueMap' qualifier contents if present
  3676. pIQualSet->Get(_bstr_t(L"ValueMap"), 0, &varMap, NULL);
  3677. if (varMap.vt != VT_EMPTY && varMap.vt != VT_NULL)
  3678. {
  3679. // Get lower and upper bounds of Descriptions array
  3680. LONG lUpper = 0, lLower = 0;
  3681. hr = SafeArrayGetLBound(varMap.parray, varMap.parray->cDims,
  3682. &lLower);
  3683. if ( m_eloErrLogOpt )
  3684. {
  3685. chsMsg.Format(L"SafeArrayGetLBound(-, -, -)");
  3686. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3687. dwThreadId, rParsedInfo, FALSE);
  3688. }
  3689. ONFAILTHROWERROR(hr);
  3690. hr = SafeArrayGetUBound(varMap.parray, varMap.parray->cDims,
  3691. &lUpper);
  3692. if ( m_eloErrLogOpt )
  3693. {
  3694. chsMsg.Format(L"SafeArrayGetUBound(-, -, -)");
  3695. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  3696. dwThreadId, rParsedInfo, FALSE);
  3697. }
  3698. ONFAILTHROWERROR(hr);
  3699. for (LONG lIndex = lLower; lIndex <= lUpper; lIndex++)
  3700. {
  3701. void* pv = NULL;
  3702. hr = SafeArrayGetElement(varMap.parray, &lIndex, &pv);
  3703. if ( m_eloErrLogOpt )
  3704. {
  3705. chsMsg.Format(L"SafeArrayGetElement(-, -, -)");
  3706. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  3707. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  3708. }
  3709. ONFAILTHROWERROR(hr);
  3710. if ( bValue )
  3711. {
  3712. WMICLIINT ipv = GetNumber ( ( WCHAR* ) pv );
  3713. if ( ipv != -1 )
  3714. {
  3715. if ( iValue == ipv )
  3716. {
  3717. _itow ( ipv, pszValue, 10);
  3718. bFound = TRUE;
  3719. break;
  3720. }
  3721. }
  3722. }
  3723. else
  3724. {
  3725. // Check whether the property value is available with the
  3726. // value map entries.
  3727. if (CompareTokens(pszValue, (_TCHAR*)pv))
  3728. {
  3729. bFound = TRUE;
  3730. break;
  3731. }
  3732. }
  3733. }
  3734. bRet = bFound;
  3735. }
  3736. // If not found in the ValueMap
  3737. if (!bRet || !bFound)
  3738. {
  3739. // Obtain the 'Values' qualifier contents if present
  3740. pIQualSet->Get(_bstr_t(L"Values"), 0, &varValue, NULL);
  3741. if (varValue.vt != VT_EMPTY && varValue.vt != VT_NULL)
  3742. {
  3743. // Get lower and upper bounds of Descriptions array
  3744. LONG lUpper = 0, lLower = 0;
  3745. hr = SafeArrayGetLBound(varValue.parray,
  3746. varValue.parray->cDims, &lLower);
  3747. if ( m_eloErrLogOpt )
  3748. {
  3749. chsMsg.Format(L"SafeArrayGetLBound(-, -, -)");
  3750. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  3751. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  3752. }
  3753. ONFAILTHROWERROR(hr);
  3754. hr = SafeArrayGetUBound(varValue.parray,
  3755. varValue.parray->cDims, &lUpper);
  3756. if ( m_eloErrLogOpt )
  3757. {
  3758. chsMsg.Format(L"SafeArrayGetUBound(-, -, -)");
  3759. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  3760. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  3761. }
  3762. ONFAILTHROWERROR(hr);
  3763. for (LONG lIndex = lLower; lIndex <= lUpper; lIndex++)
  3764. {
  3765. void *pv = NULL;
  3766. hr = SafeArrayGetElement(varValue.parray,
  3767. &lIndex, &pv);
  3768. if ( m_eloErrLogOpt )
  3769. {
  3770. chsMsg.Format(L"SafeArrayGetElement(-, -, -)");
  3771. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  3772. (LPCWSTR)chsMsg, dwThreadId, rParsedInfo, FALSE);
  3773. }
  3774. ONFAILTHROWERROR(hr);
  3775. // Check for any matching entry.
  3776. if (CompareTokens(pszValue, (_TCHAR*)pv))
  3777. {
  3778. void* pmv = NULL;
  3779. if (varMap.vt != VT_EMPTY && varMap.vt != VT_NULL)
  3780. {
  3781. // obtain the correponding ValueMap entry.
  3782. hr = SafeArrayGetElement(varMap.parray,
  3783. &lIndex, &pmv);
  3784. if ( m_eloErrLogOpt )
  3785. {
  3786. chsMsg.Format(
  3787. L"SafeArrayGetElement(-, -, -)");
  3788. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  3789. (LPCWSTR)chsMsg, dwThreadId,
  3790. rParsedInfo, FALSE);
  3791. }
  3792. ONFAILTHROWERROR(hr);
  3793. if ( bValue )
  3794. {
  3795. WMICLIINT ipmv = GetNumber ( (WCHAR*)pmv );
  3796. _itow ( ipmv, pszValue, 10);
  3797. }
  3798. else
  3799. {
  3800. // Modify the current property value
  3801. // (i.e 'Values' to 'ValueMap' content)
  3802. lstrcpy(pszValue, ((_TCHAR*)pmv));
  3803. }
  3804. }
  3805. // Only 'Values' qualifier available
  3806. else
  3807. {
  3808. _TCHAR szTemp[BUFFER255] = NULL_STRING;
  3809. _itot(lIndex, szTemp, 10);
  3810. // Modify the current property value
  3811. // (i.e 'Values' entry to index)
  3812. lstrcpy(pszValue, szTemp);
  3813. }
  3814. bFound = TRUE;
  3815. break;
  3816. }
  3817. }
  3818. // If not match found in 'ValueMap' and 'Values' qualifier
  3819. // list
  3820. if (!bFound)
  3821. {
  3822. rParsedInfo.GetCmdSwitchesObject().
  3823. SetErrataCode(IDS_E_VALUE_NOTFOUND);
  3824. }
  3825. bRet = bFound;
  3826. }
  3827. }
  3828. VARIANTCLEAR(varValue);
  3829. VARIANTCLEAR(varMap);
  3830. }
  3831. }
  3832. catch(_com_error& e)
  3833. {
  3834. SAFEIRELEASE(pIQualSet);
  3835. VARIANTCLEAR(varMap);
  3836. VARIANTCLEAR(varValue);
  3837. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  3838. bRet = FALSE;
  3839. }
  3840. catch(CHeap_Exception)
  3841. {
  3842. SAFEIRELEASE(pIQualSet);
  3843. VARIANTCLEAR(varMap);
  3844. VARIANTCLEAR(varValue);
  3845. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  3846. }
  3847. return bRet;
  3848. }
  3849. /*------------------------------------------------------------------------
  3850. Name :ValidateAlaisInParams
  3851. Synopsis :Validates the property value specified against the
  3852. property qualifiers available for that property
  3853. from the <alias> definition (i.e checking
  3854. against the contents of following qualifiers if
  3855. available:
  3856. 1. MaxLen,
  3857. 2. ValueMap
  3858. 3. Values
  3859. Type :Member Function
  3860. Input Parameter(s):
  3861. rParsedinfo - reference to CParsedInfo object.
  3862. Output Parameter(s):None
  3863. Return Type :BOOL
  3864. Global Variables :None
  3865. Calling Syntax :ValidateAlaisInParams(rParsedInfo)
  3866. Notes :None
  3867. ------------------------------------------------------------------------*/
  3868. BOOL CExecEngine::ValidateAlaisInParams(CParsedInfo& rParsedInfo)
  3869. {
  3870. BOOL bRet = TRUE;
  3871. //_TCHAR szMsg[BUFFER1024] = NULL_STRING;
  3872. BSTRMAP theParamMap;
  3873. BSTRMAP::iterator theIterator = NULL;
  3874. DWORD dwThreadId = GetCurrentThreadId();
  3875. PROPDETMAP pdmPropDetMap;
  3876. // Get the property details map.
  3877. pdmPropDetMap = rParsedInfo.GetCmdSwitchesObject().GetPropDetMap();
  3878. // If the property details are available
  3879. if (!pdmPropDetMap.empty())
  3880. {
  3881. // Get the parameters map
  3882. theParamMap = rParsedInfo.GetCmdSwitchesObject().GetParameterMap();
  3883. theIterator = theParamMap.begin();
  3884. try
  3885. {
  3886. // Loop through the list of available parameters
  3887. while (theIterator != theParamMap.end())
  3888. {
  3889. // Get the property name and the corresponding value.
  3890. _bstr_t bstrProp = _bstr_t((_TCHAR*)(*theIterator).first);
  3891. WCHAR* pszValue = (LPWSTR)(*theIterator).second;
  3892. // Check the value against the qualifier information
  3893. bRet = CheckAliasQualifierInfo(rParsedInfo, bstrProp,
  3894. pszValue, pdmPropDetMap);
  3895. if (bRet)
  3896. {
  3897. // A mapping between 'Values' and 'ValueMaps' is possible,
  3898. // hence update the parameter value to reflect the change.
  3899. rParsedInfo.GetCmdSwitchesObject().
  3900. UpdateParameterValue(bstrProp, _bstr_t(pszValue));
  3901. }
  3902. else
  3903. break;
  3904. theIterator++;
  3905. }
  3906. }
  3907. catch(_com_error& e)
  3908. {
  3909. _com_issue_error(e.Error());
  3910. bRet = FALSE;
  3911. }
  3912. catch(...)
  3913. {
  3914. bRet = FALSE;
  3915. }
  3916. }
  3917. return bRet;
  3918. }
  3919. /*------------------------------------------------------------------------
  3920. Name :CheckAliasQualifierInfo
  3921. Synopsis :Validates the parameter value specified against the
  3922. parameter qualifiers for that parameter from the
  3923. <alias> definition (i.e checking
  3924. against the contents of following qualifiers if
  3925. available:
  3926. 1. MaxLen,
  3927. 2. ValueMap
  3928. 3. Values
  3929. Type :Member Function
  3930. Input Parameter(s):
  3931. rParsedinfo - CParsedInfo object.
  3932. bstrParam - parameter name
  3933. pszValue - new value.
  3934. pdmPropDetMap - property details map
  3935. Output Parameter(s):
  3936. pszValue - new value
  3937. Return Type :BOOL
  3938. Global Variables :None
  3939. Calling Syntax :CheckAliasQualifierInfo(rParsedInfo, bstrParam,
  3940. pszValue, pdmPropDetMap)
  3941. Notes :None
  3942. ------------------------------------------------------------------------*/
  3943. BOOL CExecEngine::CheckAliasQualifierInfo(CParsedInfo& rParsedInfo,
  3944. _bstr_t bstrParam,
  3945. WCHAR*& pszValue,
  3946. PROPDETMAP pdmPropDetMap)
  3947. {
  3948. BOOL bRet = TRUE,
  3949. bFound = FALSE;
  3950. PROPDETMAP::iterator propItrtrStart = NULL;
  3951. PROPDETMAP::iterator propItrtrEnd = NULL;
  3952. QUALDETMAP qualMap;
  3953. QUALDETMAP::iterator qualDetMap = NULL;
  3954. BSTRVECTOR::iterator qualEntriesStart = NULL;
  3955. BSTRVECTOR::iterator qualEntriesEnd = NULL;
  3956. BSTRVECTOR qualVMEntries;
  3957. BSTRVECTOR qualVEntries;
  3958. BSTRVECTOR qualMEntries;
  3959. propItrtrStart = pdmPropDetMap.begin();
  3960. propItrtrEnd = pdmPropDetMap.end();
  3961. try
  3962. {
  3963. while (propItrtrStart != propItrtrEnd)
  3964. {
  3965. // If the property is found.
  3966. if (CompareTokens( (LPWSTR)bstrParam,
  3967. ((LPWSTR)(*propItrtrStart).first) ))
  3968. {
  3969. // Get the qualifier map
  3970. qualMap = ((*propItrtrStart).second).QualDetMap;
  3971. // Check if the qualifier information is available.
  3972. if (!qualMap.empty())
  3973. {
  3974. // Check for the 'MaxLen' qualifier
  3975. qualDetMap = qualMap.find(_bstr_t(L"MaxLen"));
  3976. // If MaxLen qualifier information is available.
  3977. if (qualDetMap != qualMap.end())
  3978. {
  3979. qualMEntries = (*qualDetMap).second;
  3980. BSTRVECTOR::reference qualRef = qualMEntries.at(0);
  3981. if (lstrlen(pszValue) > _wtoi((LPWSTR)qualRef))
  3982. {
  3983. rParsedInfo.GetCmdSwitchesObject().
  3984. SetErrataCode(IDS_E_VALUE_EXCEEDS_MAXLEN);
  3985. bRet = FALSE;
  3986. }
  3987. }
  3988. if (bRet)
  3989. {
  3990. WMICLIINT iValue = 0;
  3991. bool bValue = false;
  3992. bool bNumber = true;
  3993. VARTYPE vt = ReturnVarType ( (_TCHAR*) ((*propItrtrStart).second).Type );
  3994. if (
  3995. vt == VT_I1 ||
  3996. vt == VT_I2 ||
  3997. vt == VT_I4 ||
  3998. vt == VT_R4 ||
  3999. vt == VT_R8 ||
  4000. vt == VT_UI1 ||
  4001. vt == VT_UI2 ||
  4002. vt == VT_UI4 ||
  4003. vt == VT_INT ||
  4004. vt == VT_UINT
  4005. )
  4006. {
  4007. iValue = GetNumber ( pszValue );
  4008. if ( iValue == -1 )
  4009. {
  4010. bNumber = false;
  4011. }
  4012. bValue = true;
  4013. }
  4014. // Check for the 'ValueMap' qualfiers
  4015. qualDetMap = qualMap.find(_bstr_t(L"ValueMap"));
  4016. // If 'ValueMap' qualifier information is available.
  4017. if (qualDetMap != qualMap.end())
  4018. {
  4019. // Get the qualifier entries vector
  4020. qualVMEntries = (*qualDetMap ).second;
  4021. qualEntriesStart = qualVMEntries.begin();
  4022. qualEntriesEnd = qualVMEntries.end();
  4023. if ( bNumber )
  4024. {
  4025. // Loop thru the available 'ValueMap' entries.
  4026. while (qualEntriesStart != qualEntriesEnd)
  4027. {
  4028. if ( bValue )
  4029. {
  4030. WMICLIINT iqualEntriesStart = GetNumber ( ( WCHAR* ) (*qualEntriesStart) );
  4031. if ( iqualEntriesStart != -1 )
  4032. {
  4033. if ( iValue == iqualEntriesStart )
  4034. {
  4035. _itow ( iqualEntriesStart, pszValue, 10);
  4036. bFound = TRUE;
  4037. break;
  4038. }
  4039. }
  4040. }
  4041. else
  4042. {
  4043. // Check whether the property value is
  4044. // available with the value map entries.
  4045. if (CompareTokens(pszValue,
  4046. (_TCHAR*)(*qualEntriesStart)))
  4047. {
  4048. bFound = TRUE;
  4049. break;
  4050. }
  4051. }
  4052. // Move to next entry
  4053. qualEntriesStart++;
  4054. }
  4055. bRet = bFound;
  4056. }
  4057. }
  4058. // If not found in the 'ValueMap' entries
  4059. if (!bRet || !bFound)
  4060. {
  4061. // Check for the 'Values' qualfiers
  4062. qualDetMap = qualMap.find(_bstr_t(L"Values"));
  4063. // If 'Values' qualifier information is available.
  4064. if (qualDetMap != qualMap.end())
  4065. {
  4066. // Get the qualifier entries vector
  4067. qualVEntries = (*qualDetMap).second;
  4068. qualEntriesStart = qualVEntries.begin();
  4069. qualEntriesEnd = qualVEntries.end();
  4070. WMICLIINT nLoop = 0;
  4071. // Loop thru the available 'Values' entries.
  4072. while (qualEntriesStart != qualEntriesEnd)
  4073. {
  4074. // Check whether the property value is
  4075. // available with the value map entries.
  4076. if (CompareTokens(pszValue,
  4077. (_TCHAR*)(*qualEntriesStart)))
  4078. {
  4079. // If 'ValueMap' entries are available.
  4080. if (!qualVMEntries.empty())
  4081. {
  4082. //Get corresponding entry from
  4083. //'ValueMap'
  4084. BSTRVECTOR::reference qualRef =
  4085. qualVMEntries.at(nLoop);
  4086. if ( bValue )
  4087. {
  4088. WMICLIINT iqualRef = GetNumber ( (WCHAR*)(qualRef) );
  4089. _itow ( iqualRef, pszValue, 10);
  4090. }
  4091. else
  4092. {
  4093. lstrcpy(pszValue, (_TCHAR*)(qualRef));
  4094. }
  4095. }
  4096. bFound = TRUE;
  4097. break;
  4098. }
  4099. // Move to next entry
  4100. qualEntriesStart++;
  4101. nLoop++;
  4102. }
  4103. // If match not found in 'ValueMap' and
  4104. // 'Values' qualifier entries list
  4105. if (!bFound)
  4106. {
  4107. rParsedInfo.GetCmdSwitchesObject().
  4108. SetErrataCode(IDS_E_VALUE_NOTFOUND);
  4109. }
  4110. bRet = bFound;
  4111. }
  4112. }
  4113. }
  4114. }
  4115. break;
  4116. }
  4117. else
  4118. propItrtrStart++;
  4119. }
  4120. }
  4121. catch(_com_error& e)
  4122. {
  4123. _com_issue_error(e.Error());
  4124. bRet = FALSE;
  4125. }
  4126. return bRet;
  4127. }
  4128. /*------------------------------------------------------------------------
  4129. Name :SubstHashAndExecCmdUtility
  4130. Synopsis :Substitute hashes and execute command line utility.
  4131. If pIWbemObj != NULL then utility should be passed
  4132. with appropriate instance values.
  4133. Type :Member Function
  4134. Input Parameter(s):
  4135. rParsedInfo - reference to CParsedInfo object.
  4136. pIWbemObj - pointer to object of type IWbemClassObject.
  4137. Output Parameter(s):None
  4138. Return Type :void
  4139. Global Variables :None
  4140. Calling Syntax :SubstHashAndExecCmdUtility(rParsedInfo, pIWbemObj)
  4141. Notes :None
  4142. ------------------------------------------------------------------------*/
  4143. void CExecEngine::SubstHashAndExecCmdUtility(CParsedInfo& rParsedInfo,
  4144. IWbemClassObject *pIWbemObj)
  4145. {
  4146. size_t nHashPos = 0;
  4147. size_t nAfterVarSpacePos = 0;
  4148. LONG lHashLen = lstrlen(CLI_TOKEN_HASH);
  4149. LONG lSpaceLen = lstrlen(CLI_TOKEN_SPACE);
  4150. CHString chsMsg;
  4151. DWORD dwThreadId = GetCurrentThreadId();
  4152. HRESULT hr = S_OK;
  4153. BOOL bSubstituted = FALSE;
  4154. VARIANT vtInstanceValue;
  4155. VARIANT vtPath;
  4156. try
  4157. {
  4158. _bstr_t bstrResult;
  4159. _bstr_t bstrVerbDerivation =
  4160. _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  4161. GetVerbDerivation());
  4162. if ( bstrVerbDerivation == _bstr_t(CLI_TOKEN_NULL) )
  4163. bstrVerbDerivation = CLI_TOKEN_SPACE;
  4164. STRING strCmdLine(bstrVerbDerivation);
  4165. BOOL bNamedParamList = rParsedInfo.GetCmdSwitchesObject().
  4166. GetNamedParamListFlag();
  4167. // If NamedParamList is specified param=values are in Parameter map
  4168. // Order them as appear in alias verb parameter definition and put
  4169. // them in to cvInParams for further processing.
  4170. CHARVECTOR cvInParams;
  4171. if ( bNamedParamList == TRUE )
  4172. ObtainInParamsFromParameterMap(rParsedInfo, cvInParams);
  4173. else // else params are available in property list.
  4174. cvInParams = rParsedInfo.GetCmdSwitchesObject().GetPropertyList();
  4175. CHARVECTOR::iterator theActParamIterator = NULL;
  4176. try
  4177. {
  4178. // Loop initialization.
  4179. theActParamIterator = cvInParams.begin();
  4180. while( TRUE )
  4181. {
  4182. // Loop condition.
  4183. if (theActParamIterator == cvInParams.end())
  4184. break;
  4185. bSubstituted = FALSE;
  4186. while ( bSubstituted == FALSE )
  4187. {
  4188. nHashPos = strCmdLine.find(CLI_TOKEN_HASH,
  4189. nHashPos, lHashLen);
  4190. if ( nHashPos != STRING::npos )
  4191. {
  4192. // No instance specified.
  4193. if ( pIWbemObj == NULL )
  4194. {
  4195. strCmdLine.replace(nHashPos, lHashLen,
  4196. *theActParamIterator);
  4197. nHashPos = nHashPos + lstrlen(*theActParamIterator);
  4198. bSubstituted = TRUE;
  4199. }
  4200. else
  4201. {
  4202. if ( strCmdLine.compare(nHashPos + 1,
  4203. lSpaceLen,
  4204. CLI_TOKEN_SPACE) == 0 ||
  4205. strCmdLine.compare(nHashPos + 1,
  4206. lstrlen(CLI_TOKEN_SINGLE_QUOTE),
  4207. CLI_TOKEN_SINGLE_QUOTE) == 0 )
  4208. {
  4209. strCmdLine.replace(nHashPos, lHashLen,
  4210. *theActParamIterator);
  4211. nHashPos = nHashPos +
  4212. lstrlen(*theActParamIterator);
  4213. bSubstituted = TRUE;
  4214. }
  4215. else
  4216. {
  4217. nAfterVarSpacePos =
  4218. strCmdLine.find(
  4219. CLI_TOKEN_SPACE, nHashPos + 1,
  4220. lSpaceLen);
  4221. if ( nAfterVarSpacePos == STRING::npos )
  4222. {
  4223. strCmdLine.replace(nHashPos,
  4224. lHashLen, *theActParamIterator);
  4225. nHashPos = nHashPos +
  4226. lstrlen(*theActParamIterator);
  4227. bSubstituted = TRUE;
  4228. }
  4229. }
  4230. }
  4231. }
  4232. else
  4233. {
  4234. strCmdLine.append(_T(" "));
  4235. strCmdLine.append(*theActParamIterator);
  4236. bSubstituted = TRUE;
  4237. }
  4238. if ( bSubstituted == FALSE )
  4239. nHashPos = nHashPos + lHashLen;
  4240. }
  4241. // Loop expression.
  4242. theActParamIterator++;
  4243. }
  4244. if ( pIWbemObj != NULL )
  4245. {
  4246. // Replacing #Variable parameters
  4247. nHashPos = 0;
  4248. while ( TRUE )
  4249. {
  4250. nHashPos = strCmdLine.find(CLI_TOKEN_HASH, nHashPos,
  4251. lHashLen);
  4252. if ( nHashPos == STRING::npos )
  4253. break;
  4254. nAfterVarSpacePos =
  4255. strCmdLine.find(CLI_TOKEN_SPACE,
  4256. nHashPos + 1, lSpaceLen);
  4257. if ( nAfterVarSpacePos == STRING::npos )
  4258. break;
  4259. _bstr_t bstrPropName(strCmdLine.substr(nHashPos + 1,
  4260. nAfterVarSpacePos - (nHashPos + 1)).data());
  4261. VariantInit(&vtInstanceValue);
  4262. hr = pIWbemObj->Get(bstrPropName, 0,
  4263. &vtInstanceValue, 0, 0);
  4264. if (m_bTrace || m_eloErrLogOpt)
  4265. {
  4266. chsMsg.Format(L"IWbemClassObject::Get(%s, 0, "
  4267. L"-, 0, 0)", (LPWSTR)bstrPropName);
  4268. GetBstrTFromVariant(vtInstanceValue, bstrResult);
  4269. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  4270. (LPCWSTR)chsMsg,
  4271. dwThreadId, rParsedInfo, m_bTrace, 0, bstrResult);
  4272. }
  4273. ONFAILTHROWERROR(hr);
  4274. _bstr_t bstrInstanceValue;
  4275. GetBstrTFromVariant(vtInstanceValue, bstrInstanceValue);
  4276. strCmdLine.replace(nHashPos, nAfterVarSpacePos - nHashPos ,
  4277. bstrInstanceValue);
  4278. nHashPos = nHashPos + lstrlen(bstrInstanceValue);
  4279. VARIANTCLEAR(vtInstanceValue);
  4280. }
  4281. VariantInit(&vtPath);
  4282. hr = pIWbemObj->Get(L"__PATH", 0, &vtPath, 0, 0);
  4283. if (m_bTrace || m_eloErrLogOpt)
  4284. {
  4285. chsMsg.Format(L"IWbemClassObject::Get(L\"__PATH\", 0, "
  4286. L"-, 0, 0)");
  4287. GetBstrTFromVariant(vtPath, bstrResult);
  4288. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  4289. dwThreadId, rParsedInfo, m_bTrace, 0, bstrResult);
  4290. }
  4291. ONFAILTHROWERROR(hr);
  4292. }
  4293. INTEROPTION interInvoke = YES;
  4294. _TCHAR szMsg[BUFFER1024] = NULL_STRING;
  4295. // Get the user response if interactive mode is set
  4296. if (IsInteractive(rParsedInfo) == TRUE)
  4297. {
  4298. _bstr_t bstrMsg;
  4299. if ( pIWbemObj != NULL )
  4300. {
  4301. WMIFormatMessage(IDS_I_METH_INVOKE_PROMPT1, 2, bstrMsg,
  4302. (LPWSTR)vtPath.bstrVal,
  4303. (LPWSTR)strCmdLine.data());
  4304. }
  4305. else
  4306. {
  4307. WMIFormatMessage(IDS_I_METH_INVOKE_PROMPT2, 1, bstrMsg,
  4308. (LPWSTR)strCmdLine.data());
  4309. }
  4310. interInvoke = GetUserResponse((LPWSTR)bstrMsg);
  4311. }
  4312. if ( interInvoke == YES )
  4313. {
  4314. DisplayMessage(L"\n", CP_OEMCP, FALSE, TRUE);
  4315. BOOL bResult = _tsystem(strCmdLine.data());
  4316. DisplayMessage(L"\n", CP_OEMCP, FALSE, TRUE);
  4317. }
  4318. VARIANTCLEAR(vtPath);
  4319. }
  4320. catch(_com_error& e)
  4321. {
  4322. VARIANTCLEAR(vtInstanceValue);
  4323. VARIANTCLEAR(vtPath);
  4324. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  4325. }
  4326. }
  4327. catch(_com_error& e)
  4328. {
  4329. _com_issue_error(e.Error());
  4330. }
  4331. catch(CHeap_Exception)
  4332. {
  4333. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  4334. }
  4335. }
  4336. /*------------------------------------------------------------------------
  4337. Name :FormQueryAndExecuteMethodOrUtility
  4338. Synopsis :Forms query and executes method or command line
  4339. utility.
  4340. Type :Member Function
  4341. Input Parameter(s):
  4342. rParsedInfo - reference to CParsedInfo object.
  4343. pIInParam - pointer to object of type IWbemClassObject.
  4344. Output Parameter(s):None
  4345. Return Type :HRESULT
  4346. Global Variables :None
  4347. Calling Syntax :FormQueryAndExecuteMethodOrUtility(rParsedInfo, pIInParam)
  4348. Notes :None
  4349. ------------------------------------------------------------------------*/
  4350. HRESULT CExecEngine::FormQueryAndExecuteMethodOrUtility(
  4351. CParsedInfo& rParsedInfo,
  4352. IWbemClassObject *pIInParam)
  4353. {
  4354. HRESULT hr = S_OK;
  4355. IEnumWbemClassObject *pIEnumObj = NULL;
  4356. IWbemClassObject *pIWbemObj = NULL;
  4357. CHString chsMsg;
  4358. DWORD dwThreadId = GetCurrentThreadId();
  4359. try
  4360. {
  4361. _bstr_t bstrResult("");
  4362. _bstr_t bstrPath("");
  4363. BOOL bWhereExpr = FALSE;
  4364. // If PATH specified
  4365. if (rParsedInfo.GetCmdSwitchesObject().GetPathExpression() != NULL)
  4366. {
  4367. if(rParsedInfo.GetCmdSwitchesObject().
  4368. GetWhereExpression() != NULL)
  4369. {
  4370. bWhereExpr = TRUE;
  4371. }
  4372. else
  4373. bstrPath = _bstr_t(rParsedInfo.GetCmdSwitchesObject()
  4374. .GetPathExpression());
  4375. }
  4376. else
  4377. {
  4378. // If CLASS specfied
  4379. if (rParsedInfo.GetCmdSwitchesObject().GetClassPath() != NULL)
  4380. {
  4381. bstrPath = _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  4382. GetClassPath());
  4383. }
  4384. else if(rParsedInfo.GetCmdSwitchesObject().
  4385. GetWhereExpression() != NULL)
  4386. {
  4387. bWhereExpr = TRUE;
  4388. }
  4389. else
  4390. rParsedInfo.GetCmdSwitchesObject().
  4391. GetClassOfAliasTarget(bstrPath);
  4392. }
  4393. // If bstrPath is not empty
  4394. if ( !bWhereExpr )
  4395. {
  4396. if ( rParsedInfo.GetCmdSwitchesObject().GetVerbType() == CMDLINE )
  4397. {
  4398. SubstHashAndExecCmdUtility(rParsedInfo);
  4399. }
  4400. else
  4401. {
  4402. hr = ExecuteMethodAndDisplayResults(bstrPath, rParsedInfo,
  4403. pIInParam);
  4404. }
  4405. ONFAILTHROWERROR(hr);
  4406. }
  4407. else
  4408. {
  4409. ULONG ulReturned = 0;
  4410. _bstr_t bstrQuery;
  4411. // Frame the WMI query to be executed.
  4412. if (rParsedInfo.GetCmdSwitchesObject().
  4413. GetPathExpression() != NULL)
  4414. {
  4415. bstrPath = _bstr_t(rParsedInfo.
  4416. GetCmdSwitchesObject().GetClassPath());
  4417. }
  4418. else
  4419. {
  4420. rParsedInfo.GetCmdSwitchesObject()
  4421. .GetClassOfAliasTarget(bstrPath);
  4422. }
  4423. bstrQuery = _bstr_t("SELECT * FROM ") + bstrPath;
  4424. if(rParsedInfo.GetCmdSwitchesObject().
  4425. GetWhereExpression() != NULL)
  4426. {
  4427. bstrQuery += _bstr_t(" WHERE ")
  4428. + _bstr_t(rParsedInfo.GetCmdSwitchesObject().
  4429. GetWhereExpression());
  4430. }
  4431. hr = m_pITargetNS->ExecQuery(_bstr_t(L"WQL"), bstrQuery,
  4432. WBEM_FLAG_FORWARD_ONLY |
  4433. WBEM_FLAG_RETURN_IMMEDIATELY,
  4434. NULL, &pIEnumObj);
  4435. if (m_bTrace || m_eloErrLogOpt)
  4436. {
  4437. chsMsg.Format(L"IWbemServices::ExecQuery(L\"WQL\","
  4438. L" L\"%s\", 0, NULL, -)", (LPWSTR)bstrQuery);
  4439. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  4440. dwThreadId, rParsedInfo, m_bTrace);
  4441. }
  4442. ONFAILTHROWERROR(hr);
  4443. // Set the interface security
  4444. hr = SetSecurity(pIEnumObj,
  4445. rParsedInfo.GetAuthorityPrinciple(),
  4446. rParsedInfo.GetNode(),
  4447. rParsedInfo.GetUser(),
  4448. rParsedInfo.GetPassword(),
  4449. rParsedInfo.GetGlblSwitchesObject().
  4450. GetAuthenticationLevel(),
  4451. rParsedInfo.GetGlblSwitchesObject().
  4452. GetImpersonationLevel());
  4453. if (m_bTrace || m_eloErrLogOpt)
  4454. {
  4455. _TCHAR* pszAuthority = rParsedInfo.GetAuthorityPrinciple();
  4456. if( pszAuthority != NULL &&
  4457. _tcslen(pszAuthority) > 9 &&
  4458. _tcsnicmp(pszAuthority, _T("KERBEROS:"), 9) == 0)
  4459. {
  4460. BSTR bstrPrincipalName = ::SysAllocString(&pszAuthority[9]);
  4461. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_GSS_KERBEROS,"
  4462. L"RPC_C_AUTHZ_NONE, %s, %d, %d, -, EOAC_NONE)",
  4463. (LPWSTR)bstrPrincipalName,
  4464. rParsedInfo.GetGlblSwitchesObject().
  4465. GetAuthenticationLevel(),
  4466. rParsedInfo.GetGlblSwitchesObject().
  4467. GetImpersonationLevel());
  4468. SAFEBSTRFREE(bstrPrincipalName);
  4469. }
  4470. else
  4471. {
  4472. chsMsg.Format(L"CoSetProxyBlanket(-, RPC_C_AUTHN_WINNT, "
  4473. L"RPC_C_AUTHZ_NONE, NULL, %d, %d, -, EOAC_NONE)",
  4474. rParsedInfo.GetGlblSwitchesObject().
  4475. GetAuthenticationLevel(),
  4476. rParsedInfo.GetGlblSwitchesObject().
  4477. GetImpersonationLevel());
  4478. }
  4479. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  4480. dwThreadId, rParsedInfo, m_bTrace);
  4481. }
  4482. ONFAILTHROWERROR(hr);
  4483. BOOL bNoInstances = TRUE;
  4484. // Loop thru the available instances
  4485. while (((hr = pIEnumObj->Next( WBEM_INFINITE, 1,
  4486. &pIWbemObj, &ulReturned )) == S_OK)
  4487. && (ulReturned == 1))
  4488. {
  4489. bNoInstances = FALSE;
  4490. VARIANT vtPath;
  4491. VariantInit(&vtPath);
  4492. hr = pIWbemObj->Get(L"__PATH", 0, &vtPath, 0, 0);
  4493. if (m_bTrace || m_eloErrLogOpt)
  4494. {
  4495. chsMsg.Format(L"IWbemClassObject::Get(L\"__PATH\", 0, "
  4496. L"-, 0, 0)");
  4497. GetBstrTFromVariant(vtPath, bstrResult);
  4498. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  4499. dwThreadId, rParsedInfo, m_bTrace, 0, bstrResult);
  4500. }
  4501. ONFAILTHROWERROR(hr);
  4502. if ( vtPath.vt == VT_BSTR )
  4503. {
  4504. if ( rParsedInfo.GetCmdSwitchesObject().GetVerbType()
  4505. == CMDLINE )
  4506. {
  4507. SubstHashAndExecCmdUtility(rParsedInfo, pIWbemObj);
  4508. }
  4509. else
  4510. {
  4511. hr = ExecuteMethodAndDisplayResults(vtPath.bstrVal,
  4512. rParsedInfo, pIInParam);
  4513. }
  4514. ONFAILTHROWERROR(hr);
  4515. }
  4516. VariantClear(&vtPath);
  4517. SAFEIRELEASE(pIWbemObj);
  4518. }
  4519. // If next fails.
  4520. ONFAILTHROWERROR(hr);
  4521. SAFEIRELEASE(pIEnumObj);
  4522. // If no instances are available
  4523. if ( bNoInstances == TRUE )
  4524. {
  4525. rParsedInfo.GetCmdSwitchesObject().
  4526. SetInformationCode(IDS_I_NO_INSTANCES);
  4527. }
  4528. }
  4529. }
  4530. catch(_com_error& e)
  4531. {
  4532. SAFEIRELEASE(pIEnumObj);
  4533. SAFEIRELEASE(pIWbemObj);
  4534. rParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  4535. hr = e.Error();
  4536. }
  4537. catch(CHeap_Exception)
  4538. {
  4539. SAFEIRELEASE(pIEnumObj);
  4540. SAFEIRELEASE(pIWbemObj);
  4541. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  4542. }
  4543. return hr;
  4544. }
  4545. /*----------------------------------------------------------------------------
  4546. Name :ExtractClassNameandWhereExpr
  4547. Synopsis :This function takes the input as a path expression and
  4548. extracts the Class and Where expression part from the
  4549. path expression.
  4550. Type :Member Function
  4551. Input Parameter(s):
  4552. pszPathExpr - the path expression
  4553. rParsedInfo - reference to CParsedInfo class object
  4554. Output Parameter(s):
  4555. rParsedInfo - reference to CParsedInfo class object
  4556. pszWhere - the Where expression
  4557. Return Type :BOOL
  4558. Global Variables :None
  4559. Calling Syntax :ExtractClassNameandWhereExpr(pszPathExpr, rParsedInfo,
  4560. pszWhere)
  4561. Notes :None
  4562. ----------------------------------------------------------------------------*/
  4563. BOOL CExecEngine::ExtractClassNameandWhereExpr(_TCHAR* pszPathExpr,
  4564. CParsedInfo& rParsedInfo,
  4565. _TCHAR* pszWhere)
  4566. {
  4567. // Frame the class name and where expression based on the object path
  4568. BOOL bRet = TRUE;
  4569. _TCHAR* pszToken = NULL;
  4570. BOOL bFirst = TRUE;
  4571. _TCHAR pszPath[MAX_BUFFER] = NULL_STRING;
  4572. if (pszPathExpr == NULL || pszWhere == NULL)
  4573. bRet = FALSE;
  4574. try
  4575. {
  4576. if ( bRet == TRUE )
  4577. {
  4578. lstrcpy(pszPath, pszPathExpr);
  4579. lstrcpy(pszWhere, CLI_TOKEN_NULL);
  4580. pszToken = _tcstok(pszPath, CLI_TOKEN_DOT);
  4581. if (pszToken != NULL)
  4582. {
  4583. if(CompareTokens(pszToken, pszPathExpr))
  4584. bRet = FALSE;
  4585. }
  4586. while (pszToken != NULL)
  4587. {
  4588. pszToken = _tcstok(NULL, CLI_TOKEN_COMMA);
  4589. if (pszToken != NULL)
  4590. {
  4591. if (!bFirst)
  4592. lstrcat(pszWhere, CLI_TOKEN_AND);
  4593. lstrcat(pszWhere, pszToken);
  4594. bFirst = FALSE;
  4595. }
  4596. else
  4597. break;
  4598. }
  4599. }
  4600. }
  4601. catch(...)
  4602. {
  4603. rParsedInfo.GetCmdSwitchesObject().
  4604. SetErrataCode(IDS_E_INVALID_PATH);
  4605. bRet = FALSE;
  4606. }
  4607. return bRet;
  4608. }
  4609. /*------------------------------------------------------------------------
  4610. Name :GetUserResponseEx
  4611. Synopsis :This function accepts the user response before going
  4612. ahead, when /INTERACTIVE is specified at the verb
  4613. level
  4614. Type :Member Function
  4615. Input parameter :
  4616. pszMsg - message to be displayed.
  4617. Output parameters :None
  4618. Return Type :INTEROPTION
  4619. Global Variables :None
  4620. Calling Syntax :GetUserResponseEx(pszMsg)
  4621. Notes :None
  4622. ------------------------------------------------------------------------*/
  4623. INTEROPTION CExecEngine::GetUserResponseEx(_TCHAR* pszMsg)
  4624. {
  4625. INTEROPTION bRet = YES;
  4626. _TCHAR szResp[BUFFER255] = NULL_STRING;
  4627. _TCHAR *pBuf = NULL;
  4628. if (pszMsg == NULL)
  4629. bRet = NO;
  4630. if(bRet != NO)
  4631. {
  4632. // Get the user response, till 'Y' - yes or 'N' - no
  4633. // is keyed in
  4634. while(TRUE)
  4635. {
  4636. DisplayMessage(pszMsg, CP_OEMCP, TRUE, TRUE);
  4637. pBuf = _fgetts(szResp, BUFFER255-1, stdin);
  4638. if(pBuf != NULL)
  4639. {
  4640. LONG lInStrLen = lstrlen(szResp);
  4641. if(szResp[lInStrLen - 1] == _T('\n'))
  4642. szResp[lInStrLen - 1] = _T('\0');
  4643. }
  4644. else if ( g_wmiCmd.GetBreakEvent() != TRUE )
  4645. {
  4646. lstrcpy(szResp, RESPONSE_NO);
  4647. DisplayMessage(_T("\n"), CP_OEMCP, TRUE, TRUE);
  4648. }
  4649. if ( g_wmiCmd.GetBreakEvent() == TRUE )
  4650. {
  4651. g_wmiCmd.SetBreakEvent(FALSE);
  4652. lstrcpy(szResp, RESPONSE_NO);
  4653. DisplayMessage(_T("\n"), CP_OEMCP, TRUE, TRUE);
  4654. }
  4655. if (CompareTokens(szResp, RESPONSE_YES)
  4656. || CompareTokens(szResp, RESPONSE_NO)
  4657. || CompareTokens(szResp, RESPONSE_HELP))
  4658. break;
  4659. }
  4660. if (CompareTokens(szResp, RESPONSE_NO))
  4661. bRet = NO;
  4662. else if (CompareTokens(szResp, RESPONSE_YES))
  4663. bRet = YES;
  4664. else if (CompareTokens(szResp, RESPONSE_HELP))
  4665. bRet = HELP;
  4666. }
  4667. return bRet;
  4668. }
  4669. /*----------------------------------------------------------------------------
  4670. Name :ObtainInParamsFromParameterMap
  4671. Synopsis :This function obtains param values from parameter map
  4672. in the same order as they appear in the alias verb
  4673. definition.
  4674. Type :Member Function
  4675. Input Parameter(s):
  4676. rParsedInfo - reference to CParsedInfo object
  4677. Output Parameter(s):
  4678. cvParamValues - reference to parameter values vector
  4679. Return Type :None
  4680. Global Variables :None
  4681. Calling Syntax :ObtainInParamsFromParameterMap(rParsedInfo, cvParamValues)
  4682. Notes :None
  4683. ----------------------------------------------------------------------------*/
  4684. void CExecEngine::ObtainInParamsFromParameterMap(CParsedInfo& rParsedInfo,
  4685. CHARVECTOR& cvParamValues)
  4686. {
  4687. PROPDETMAP pdmVerbParamsFromAliasDef = (*(rParsedInfo.
  4688. GetCmdSwitchesObject().
  4689. GetMethDetMap().begin())).
  4690. second.Params;
  4691. PROPDETMAP::iterator itrVerbParams;
  4692. BSTRMAP bmNamedParamList = rParsedInfo.GetCmdSwitchesObject().
  4693. GetParameterMap();
  4694. BSTRMAP::iterator itrNamedParamList;
  4695. try
  4696. {
  4697. for ( itrVerbParams = pdmVerbParamsFromAliasDef.begin();
  4698. itrVerbParams != pdmVerbParamsFromAliasDef.end();
  4699. itrVerbParams++ )
  4700. {
  4701. _TCHAR* pszVerbParamName = (*itrVerbParams).first;
  4702. // To remove numbers from Names.
  4703. pszVerbParamName = pszVerbParamName + 5;
  4704. if ( Find(bmNamedParamList, pszVerbParamName, itrNamedParamList)
  4705. == TRUE)
  4706. {
  4707. cvParamValues.push_back(_bstr_t((*itrNamedParamList).second));
  4708. }
  4709. else
  4710. {
  4711. cvParamValues.push_back(
  4712. _bstr_t(((*itrVerbParams).second).Default));
  4713. }
  4714. }
  4715. }
  4716. catch(_com_error& e)
  4717. {
  4718. _com_issue_error(e.Error());
  4719. }
  4720. }
  4721. /*----------------------------------------------------------------------------
  4722. Name :FrameAssocHeader
  4723. Synopsis :This function frames the XML header to be used with
  4724. the ASSOCIATORS output
  4725. Type :Member Function
  4726. Input Parameter(s):
  4727. bstrPath - object/class path
  4728. bClass - TRUE - Indicates class level associators header
  4729. FALSE - Indicates instance level associators header
  4730. Output Parameter(s):
  4731. bstrFrag - fragment string
  4732. Return Type :HRESULT
  4733. Global Variables :None
  4734. Calling Syntax :FrameAssocHeader(bstrPath, bstrFrag, bClass)
  4735. Notes :None
  4736. ----------------------------------------------------------------------------*/
  4737. HRESULT CExecEngine::FrameAssocHeader(_bstr_t bstrPath, _bstr_t& bstrFrag,
  4738. BOOL bClass)
  4739. {
  4740. HRESULT hr = S_OK;
  4741. IWbemClassObject *pIObject = NULL;
  4742. try
  4743. {
  4744. _variant_t vClass, vSClass, vPath,
  4745. vOrigin, vType;
  4746. _bstr_t bstrProp;
  4747. CHString szBuf;
  4748. // Get the Class/instance information.
  4749. hr = m_pITargetNS->GetObject(bstrPath, WBEM_FLAG_USE_AMENDED_QUALIFIERS,
  4750. NULL, &pIObject, NULL);
  4751. ONFAILTHROWERROR(hr);
  4752. // Get the __CLASS property value
  4753. bstrProp = L"__CLASS";
  4754. hr = pIObject->Get(bstrProp, 0, &vClass, 0, 0);
  4755. ONFAILTHROWERROR(hr);
  4756. // Get the __PATH property value
  4757. bstrProp = L"__PATH";
  4758. hr = pIObject->Get(bstrProp, 0, &vPath, 0, 0);
  4759. ONFAILTHROWERROR(hr);
  4760. // If CLASS level associators required
  4761. if (bClass)
  4762. {
  4763. // Get the __SUPERCLASS property value
  4764. bstrProp = L"__SUPERCLASS";
  4765. hr = pIObject->Get(bstrProp, 0, &vSClass, NULL, NULL);
  4766. ONFAILTHROWERROR(hr);
  4767. szBuf.Format(_T("<CLASS NAME=\"%s\" SUPERCLASS=\"%s\"><PROPERTY "
  4768. L"NAME=\"__PATH\" CLASSORIGIN=\"__SYSTEM\" TYPE=\"string\">"
  4769. L"<VALUE><![CDATA[%s]]></VALUE></PROPERTY>"),
  4770. (vClass.vt != VT_NULL && (LPWSTR)vClass.bstrVal)
  4771. ? (LPWSTR)vClass.bstrVal : L"N/A",
  4772. (vSClass.vt != VT_NULL && (LPWSTR)vSClass.bstrVal)
  4773. ? (LPWSTR)vSClass.bstrVal : L"N/A",
  4774. (vPath.vt != VT_NULL && (LPWSTR)vPath.bstrVal)
  4775. ? (LPWSTR)vPath.bstrVal : L"N/A");
  4776. }
  4777. else
  4778. {
  4779. szBuf.Format(
  4780. _T("<INSTANCE CLASSNAME=\"%s\"><PROPERTY NAME=\"__PATH\""
  4781. L" CLASSORIGIN=\"__SYSTEM\" TYPE=\"string\"><VALUE><![CDATA[%s]]>"
  4782. L"</VALUE></PROPERTY>"),
  4783. (vClass.vt != VT_NULL && (LPWSTR)vClass.bstrVal)
  4784. ? (LPWSTR)vClass.bstrVal : L"N/A",
  4785. (vPath.vt != VT_NULL && (LPWSTR)vPath.bstrVal)
  4786. ? (LPWSTR)vPath.bstrVal : L"N/A");
  4787. }
  4788. SAFEIRELEASE(pIObject);
  4789. bstrFrag = _bstr_t(szBuf);
  4790. }
  4791. catch(_com_error& e)
  4792. {
  4793. SAFEIRELEASE(pIObject);
  4794. hr = e.Error();
  4795. }
  4796. // trap for CHeap_Exception
  4797. catch(CHeap_Exception)
  4798. {
  4799. SAFEIRELEASE(pIObject);
  4800. hr = WBEM_E_OUT_OF_MEMORY;
  4801. }
  4802. return hr;
  4803. }