Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2620 lines
74 KiB

  1. /***************************************************************************
  2. Copyright information : Copyright (c) 1998-1999 Microsoft Corporation
  3. File Name : WMICommandLine.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 needed
  9. for synchronization the funtionality of three
  10. functional components identified for the
  11. wmic.exe.Object of this class is created in
  12. the main program and used to handle
  13. functionality of Parsing Engine, Execution
  14. Engine,and Format Engine thorough class members.
  15. Global Functions : CompareTokens(_TCHAR* pszTok1, _TCHAR* pszTok2)
  16. Revision History :
  17. Last Modified By : Ch. Sriramachandramurthy
  18. Last Modified Date : 11th-April-2001
  19. *****************************************************************************/
  20. // WmiCmdLn.cpp : implementation file
  21. #include "Precomp.h"
  22. #include "CommandSwitches.h"
  23. #include "GlobalSwitches.h"
  24. #include "HelpInfo.h"
  25. #include "ErrorLog.h"
  26. #include "ParsedInfo.h"
  27. #include "CmdTokenizer.h"
  28. #include "CmdAlias.h"
  29. #include "ParserEngine.h"
  30. #include "ExecEngine.h"
  31. #include "ErrorInfo.h"
  32. #include "WmiCliXMLLog.h"
  33. #include "FormatEngine.h"
  34. #include "wmicmdln.h"
  35. #include "conio.h"
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CWMICommandLine
  38. /*------------------------------------------------------------------------
  39. Name :CWMICommandLine
  40. Synopsis :This function initializes the member variables when
  41. an object of the class type is instantiated
  42. Type :Constructor
  43. Input parameters :None
  44. Output parameters :None
  45. Return Type :None
  46. Global Variables :None
  47. Calling Syntax :None
  48. Notes :None
  49. ------------------------------------------------------------------------*/
  50. CWMICommandLine::CWMICommandLine()
  51. {
  52. m_uErrLevel = 0;
  53. m_pIWbemLocator = NULL;
  54. m_hKey = NULL;
  55. m_bBreakEvent = FALSE;
  56. m_bAccCmd = TRUE; //To come out of the program when registering mofs
  57. m_bDispRes = TRUE;
  58. m_bInitWinSock = FALSE;
  59. EmptyClipBoardBuffer();
  60. }
  61. /*------------------------------------------------------------------------
  62. Name :~CWMICommandLine
  63. Synopsis :This function uninitializes the member variables
  64. when an object of the class type goes out of scope.
  65. Type :Destructor
  66. Input parameters :None
  67. Output parameters :None
  68. Return Type :None
  69. Global Variables :None
  70. Calling Syntax :None
  71. Notes :None
  72. ------------------------------------------------------------------------*/
  73. CWMICommandLine::~CWMICommandLine()
  74. {
  75. SAFEIRELEASE(m_pIWbemLocator);
  76. }
  77. /*------------------------------------------------------------------------
  78. Name :Uninitialize
  79. Synopsis :This function uninitializes the member variables
  80. when the execution of a command string issued on the
  81. command line is completed.It internally calls
  82. uninitialize for CParsedInfo,CExecEngine,ParserEngine
  83. and CFormatEngine .
  84. Type :Member Function
  85. Input parameters :None
  86. Output parameters :None
  87. Return Type :None
  88. Global Variables :None
  89. Calling Syntax :Uninitialize()
  90. Notes :None
  91. ------------------------------------------------------------------------*/
  92. void CWMICommandLine::Uninitialize()
  93. {
  94. m_ParsedInfo.Uninitialize(TRUE);
  95. m_ExecEngine.Uninitialize(TRUE);
  96. m_ParserEngine.Uninitialize(TRUE);
  97. m_FormatEngine.Uninitialize(TRUE);
  98. SAFEIRELEASE(m_pIWbemLocator);
  99. if (m_hKey != NULL)
  100. {
  101. RegCloseKey(m_hKey);
  102. m_hKey = NULL;
  103. }
  104. // Uninitialize windows socket interface.
  105. if ( m_bInitWinSock == TRUE )
  106. TermWinsock();
  107. CoUninitialize();
  108. m_bmKeyWordtoFileName.clear();
  109. SetScreenBuffer(m_nHeight, m_nWidth);
  110. }
  111. /*------------------------------------------------------------------------
  112. Name :Initialize
  113. Synopsis :This function returns initializes the COM library and
  114. sets the process security, also it creates an instance
  115. of the IWbemLocator object
  116. Type :Member Function
  117. Input parameters :None
  118. Output parameters :None
  119. Return Type :BOOL
  120. Global Variables :None
  121. Calling Syntax :Initialize()
  122. Notes :None
  123. ------------------------------------------------------------------------*/
  124. BOOL CWMICommandLine::Initialize()
  125. {
  126. HRESULT hr = S_OK;
  127. BOOL bRet = TRUE;
  128. m_bBreakEvent = FALSE;
  129. m_bAccCmd = TRUE; //To come out of the program when registering mofs
  130. try
  131. {
  132. GetScreenBuffer(m_nHeight, m_nWidth);
  133. // Set the console scree buffer size.
  134. SetScreenBuffer();
  135. // Initialize the COM library
  136. hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  137. ONFAILTHROWERROR(hr);
  138. // Initialize the security
  139. hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  140. RPC_C_AUTHN_LEVEL_NONE,
  141. RPC_C_IMP_LEVEL_IMPERSONATE,
  142. NULL, EOAC_NONE, 0);
  143. ONFAILTHROWERROR(hr);
  144. // Create an instance of the IWbemLocator interface.
  145. hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  146. IID_IWbemLocator, (LPVOID *) &m_pIWbemLocator);
  147. ONFAILTHROWERROR(hr);
  148. // Enable the security privileges
  149. hr = ModifyPrivileges(TRUE);
  150. ONFAILTHROWERROR(hr);
  151. try
  152. {
  153. hr = RegisterMofs();
  154. ONFAILTHROWERROR(hr);
  155. // Initialize the Globalswitches and Commandswitches.
  156. m_ParsedInfo.GetGlblSwitchesObject().Initialize();
  157. m_ParsedInfo.GetCmdSwitchesObject().Initialize();
  158. }
  159. catch(WMICLIINT nVal)
  160. {
  161. if (nVal == OUT_OF_MEMORY)
  162. {
  163. m_ParsedInfo.GetCmdSwitchesObject().
  164. SetErrataCode(OUT_OF_MEMORY);
  165. }
  166. // If mofcomp error
  167. if (nVal == MOFCOMP_ERROR)
  168. {
  169. m_ParsedInfo.GetCmdSwitchesObject().
  170. SetErrataCode(MOFCOMP_ERROR);
  171. }
  172. bRet = FALSE;
  173. }
  174. catch(DWORD dwError)
  175. {
  176. // If Win32 Error
  177. DisplayString(IDS_E_REGMOF_FAILED, CP_OEMCP,
  178. NULL, TRUE, TRUE);
  179. ::SetLastError(dwError);
  180. DisplayWin32Error();
  181. m_ParsedInfo.GetCmdSwitchesObject().SetErrataCode(dwError);
  182. bRet = FALSE;
  183. }
  184. // Set the console control handler
  185. if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE))
  186. {
  187. m_ParsedInfo.GetCmdSwitchesObject().
  188. SetErrataCode(SET_CONHNDLR_ROUTN_FAIL);
  189. bRet = FALSE;
  190. }
  191. GetFileNameMap();
  192. }
  193. catch (_com_error& e)
  194. {
  195. SAFEIRELEASE(m_pIWbemLocator);
  196. m_ParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  197. bRet = FALSE;
  198. }
  199. return bRet;
  200. }
  201. /*-------------------------------------------------------------------------
  202. Name :GetFormatObject
  203. Synopsis :This function returns a reference to the
  204. CFormatEngine Object
  205. Type :Member Function
  206. Input parameters :None
  207. Output parameters :None
  208. Return Type :CFormatEngine &
  209. Global Variables :None
  210. Calling Syntax :GetFormatObject()
  211. Notes :None
  212. -------------------------------------------------------------------------*/
  213. CFormatEngine& CWMICommandLine::GetFormatObject()
  214. {
  215. return m_FormatEngine;
  216. }
  217. /*-------------------------------------------------------------------------
  218. Name :GetParsedInfoObject
  219. Synopsis :This function returns a reference to the
  220. CParsedInfo Object
  221. Type :Member Function
  222. Input parameters :None
  223. Output parameters :None
  224. Return Type :CParsedInfo &
  225. Global Variables :None
  226. Calling Syntax :GetParsedInfoObject()
  227. Notes :None
  228. -------------------------------------------------------------------------*/
  229. CParsedInfo& CWMICommandLine::GetParsedInfoObject()
  230. {
  231. return m_ParsedInfo;
  232. }
  233. /*-------------------------------------------------------------------------
  234. Name :ProcessCommandAndDisplayResults
  235. Synopsis :It processes the given command string by giving the
  236. the command to CParsedInfo as input, initializing the
  237. CParserEngine,CExecEngine and CFormatEngine and
  238. synchronizes the operation between all the modules.
  239. Type :Member Function
  240. Input parameters :
  241. pszBuffer - input command string
  242. Output parameters :None
  243. Return Type :SESSIONRETCODE
  244. Global Variables :None
  245. Calling Syntax :ProcessCommandAndDisplayResults(pszBuffer)
  246. Notes :None
  247. -------------------------------------------------------------------------*/
  248. SESSIONRETCODE CWMICommandLine::ProcessCommandAndDisplayResults(
  249. _TCHAR* pszBuffer)
  250. {
  251. SESSIONRETCODE ssnRetCode = SESSION_SUCCESS;
  252. try
  253. {
  254. // Initialize the local variables.
  255. ULONG ulRetTime = 0;
  256. RETCODE retCode = PARSER_EXECCOMMAND;
  257. BOOL bExecute = FALSE;
  258. BOOL bFirst = TRUE;
  259. _bstr_t bstrXML = L"";
  260. _bstr_t bstrAggregateXML = L"";
  261. _bstr_t bstrTempXML = L"";
  262. _bstr_t bstrHeader = L"";
  263. _bstr_t bstrRequest;
  264. // Reset the erratacode
  265. m_ParsedInfo.GetCmdSwitchesObject().SetErrataCode(0);
  266. m_ParsedInfo.SetNewCycleStatus(FALSE);
  267. m_ParsedInfo.SetNewCommandStatus(TRUE);
  268. //Store the starttime and increment the commandsequence
  269. // number to one.
  270. if (!m_ParsedInfo.GetGlblSwitchesObject().SetStartTime())
  271. {
  272. m_ParsedInfo.GetCmdSwitchesObject().SetErrataCode(OUT_OF_MEMORY);
  273. ssnRetCode = SESSION_ERROR;
  274. }
  275. //Initialize the command input in CParsedInfo
  276. if(!m_ParsedInfo.GetCmdSwitchesObject().SetCommandInput(pszBuffer))
  277. {
  278. m_ParsedInfo.GetCmdSwitchesObject().SetErrataCode(OUT_OF_MEMORY);
  279. ssnRetCode = SESSION_ERROR;
  280. }
  281. if(ssnRetCode != SESSION_ERROR)
  282. {
  283. //Tokenize the command string as per the pre-defined delimiters
  284. if (m_ParserEngine.GetCmdTokenizer().TokenizeCommand(pszBuffer))
  285. {
  286. // Check whether the input indicates end of session.
  287. // i.e either QUIT or EXIT
  288. if(!IsSessionEnd())
  289. {
  290. //Setting the IWbemLocator object .
  291. m_ParserEngine.SetLocatorObject(m_pIWbemLocator);
  292. // Initilaize ParsedInfo Object to release earlier messages
  293. m_ParsedInfo.Initialize();
  294. //Call CParserEngine ProcessTokens to process the
  295. //tokenized commands .
  296. retCode = m_ParserEngine.ProcessTokens(m_ParsedInfo);
  297. //Checking if the return code indicates Command execution
  298. if (retCode == PARSER_EXECCOMMAND)
  299. {
  300. // Check whether user should be prompted for password
  301. CheckForPassword();
  302. // Obtain the /EVERY interval value
  303. ulRetTime = m_ParsedInfo.GetCmdSwitchesObject()
  304. .GetRetrievalInterval();
  305. // Set the execute flag to TRUE.
  306. bExecute = TRUE;
  307. CHARVECTOR cvNodesList =
  308. m_ParsedInfo.GetGlblSwitchesObject().
  309. GetNodesList();
  310. CHARVECTOR::iterator iNodesIterator;
  311. m_ParsedInfo.SetNewCommandStatus(TRUE);
  312. BOOL bXMLEncoding = FALSE;
  313. _TCHAR *pszVerbName = m_ParsedInfo.
  314. GetCmdSwitchesObject().
  315. GetVerbName();
  316. if(CompareTokens(pszVerbName, CLI_TOKEN_GET)
  317. || CompareTokens(pszVerbName, CLI_TOKEN_LIST)
  318. || CompareTokens(pszVerbName, CLI_TOKEN_ASSOC)
  319. || pszVerbName == NULL)
  320. {
  321. bXMLEncoding = TRUE;
  322. }
  323. BOOL bBreak = TRUE;
  324. BOOL bMsgFlag = FALSE;
  325. LONG lLoopCount = 0;
  326. ULONG ulRepeatCount =
  327. m_ParsedInfo.GetCmdSwitchesObject().
  328. GetRepeatCount();
  329. BOOL bFirstEvery = TRUE;
  330. OUTPUTSPEC opsOutOpt =
  331. m_ParsedInfo.GetGlblSwitchesObject().
  332. GetOutputOrAppendOption(TRUE);
  333. while (TRUE)
  334. {
  335. m_ParsedInfo.SetNewCycleStatus(TRUE);
  336. if(bXMLEncoding)
  337. {
  338. bstrHeader = L"";
  339. }
  340. // Iterate thru the list of nodes
  341. for ( iNodesIterator = cvNodesList.begin();
  342. iNodesIterator <
  343. cvNodesList.end(); iNodesIterator++ )
  344. {
  345. if(bXMLEncoding)
  346. {
  347. bstrTempXML = L"";
  348. }
  349. // Reset the error and information code(s).
  350. m_ParsedInfo.GetCmdSwitchesObject().
  351. SetInformationCode(0);
  352. m_ParsedInfo.GetCmdSwitchesObject().
  353. SetErrataCode(0);
  354. if ( iNodesIterator == cvNodesList.begin() &&
  355. cvNodesList.size() > 1 )
  356. continue;
  357. if(!bXMLEncoding)
  358. {
  359. if ( cvNodesList.size() > 2 )
  360. {
  361. _bstr_t bstrNode;
  362. WMIFormatMessage(IDS_I_NODENAME_MSG, 1,
  363. bstrNode, (LPWSTR)*iNodesIterator);
  364. DisplayMessage((LPWSTR)bstrNode,
  365. CP_OEMCP,
  366. FALSE, FALSE);
  367. }
  368. }
  369. else
  370. {
  371. CHString sBuffer;
  372. _bstr_t bstrRessultsNode =
  373. (*iNodesIterator);
  374. FindAndReplaceEntityReferences(
  375. bstrRessultsNode);
  376. sBuffer.Format(L"<RESULTS NODE=\"%s\">",
  377. (LPWSTR)bstrRessultsNode);
  378. bstrTempXML += _bstr_t(sBuffer);
  379. }
  380. // Setting the locator object .
  381. m_ExecEngine.SetLocatorObject(m_pIWbemLocator);
  382. m_ParsedInfo.GetGlblSwitchesObject().
  383. SetNode(*iNodesIterator);
  384. //Call ExecEngine ExecuteCommand to execute the
  385. // tokenized command
  386. if (m_ExecEngine.ExecuteCommand(m_ParsedInfo))
  387. {
  388. // Set the successflag to TRUE
  389. m_ParsedInfo.GetCmdSwitchesObject().
  390. SetSuccessFlag(TRUE);
  391. if(bXMLEncoding)
  392. {
  393. // Append the XML result set obtained
  394. // to the aggregated output
  395. if (m_ParsedInfo.GetCmdSwitchesObject().
  396. GetXMLResultSet())
  397. {
  398. bstrTempXML += _bstr_t(m_ParsedInfo.
  399. GetCmdSwitchesObject().
  400. GetXMLResultSet());
  401. }
  402. // Free the XML result set.
  403. m_ParsedInfo.GetCmdSwitchesObject().
  404. SetXMLResultSet(NULL);
  405. }
  406. else
  407. {
  408. bBreak = TRUE;
  409. if (!m_FormatEngine.
  410. DisplayResults(m_ParsedInfo))
  411. {
  412. ssnRetCode = SESSION_ERROR;
  413. SetSessionErrorLevel(ssnRetCode);
  414. break;
  415. }
  416. m_ParsedInfo.SetNewCommandStatus(FALSE);
  417. m_ParsedInfo.SetNewCycleStatus(FALSE);
  418. }
  419. }
  420. else
  421. {
  422. //Set the sucess flag to FALSE
  423. m_ParsedInfo.GetCmdSwitchesObject().
  424. SetSuccessFlag(FALSE);
  425. ssnRetCode = SESSION_ERROR;
  426. if(bXMLEncoding)
  427. {
  428. _bstr_t bstrNode, bstrError;
  429. UINT uErrorCode = 0;
  430. WMIFormatMessage(IDS_I_NODENAME_MSG, 1,
  431. bstrNode, (LPWSTR)*iNodesIterator);
  432. DisplayMessage((LPWSTR)bstrNode,
  433. CP_OEMCP,
  434. TRUE, FALSE);
  435. // Retrieve the error code
  436. uErrorCode = m_ParsedInfo.
  437. GetCmdSwitchesObject().
  438. GetErrataCode() ;
  439. if ( uErrorCode != 0 )
  440. {
  441. _bstr_t bstrTemp;
  442. CHString sBuffer;
  443. WMIFormatMessage(uErrorCode,
  444. 0, bstrTemp, NULL);
  445. sBuffer.Format(L"<ERROR><DESCRIPTION>"
  446. L"%s</DESCRIPTION>"
  447. L"</ERROR>",
  448. (LPWSTR)(bstrTemp));
  449. bstrError = _bstr_t(sBuffer);
  450. // Write the error to stderr
  451. DisplayMessage((LPWSTR)bstrTemp,
  452. CP_OEMCP, TRUE, FALSE);
  453. }
  454. else
  455. {
  456. m_FormatEngine.
  457. GetErrorInfoObject().
  458. GetErrorFragment(
  459. m_ParsedInfo.
  460. GetCmdSwitchesObject().
  461. GetCOMError()->Error(),
  462. bstrError);
  463. // Write the error to stderr
  464. m_FormatEngine.
  465. DisplayCOMError(m_ParsedInfo,
  466. TRUE);
  467. }
  468. bstrTempXML += bstrError;
  469. }
  470. else
  471. {
  472. bBreak = TRUE;
  473. if (!m_FormatEngine.
  474. DisplayResults(m_ParsedInfo))
  475. {
  476. // Set the session error level
  477. SetSessionErrorLevel(ssnRetCode);
  478. break;
  479. }
  480. m_ParsedInfo.SetNewCommandStatus(FALSE);
  481. }
  482. // Set the session error level
  483. SetSessionErrorLevel(ssnRetCode);
  484. }
  485. if(bXMLEncoding)
  486. bstrTempXML += L"</RESULTS>";
  487. if(bXMLEncoding && bFirst)
  488. {
  489. bFirst = FALSE;
  490. FrameXMLHeader(bstrHeader, lLoopCount);
  491. if(lLoopCount == 0)
  492. FrameRequestNode(bstrRequest);
  493. bstrXML += bstrHeader;
  494. bstrXML += bstrRequest;
  495. }
  496. m_ExecEngine.Uninitialize();
  497. m_FormatEngine.Uninitialize();
  498. m_ParsedInfo.GetCmdSwitchesObject().
  499. SetCredentialsFlag(FALSE);
  500. m_ParsedInfo.GetCmdSwitchesObject().
  501. FreeCOMError();
  502. m_ParsedInfo.GetCmdSwitchesObject().
  503. SetSuccessFlag(TRUE);
  504. if(bXMLEncoding)
  505. {
  506. if(!m_ParsedInfo.GetGlblSwitchesObject().
  507. GetAggregateFlag())
  508. {
  509. _bstr_t bstrNodeResult = L"";
  510. bstrNodeResult += bstrXML;
  511. bstrNodeResult += bstrTempXML;
  512. bstrNodeResult += L"</COMMAND>";
  513. m_ParsedInfo.GetCmdSwitchesObject().
  514. SetXMLResultSet((LPWSTR)
  515. bstrNodeResult);
  516. if (!m_FormatEngine.
  517. DisplayResults(m_ParsedInfo))
  518. {
  519. bBreak = TRUE;
  520. ssnRetCode = SESSION_ERROR;
  521. SetSessionErrorLevel(ssnRetCode);
  522. m_FormatEngine.Uninitialize();
  523. break;
  524. }
  525. m_ParsedInfo.SetNewCommandStatus(FALSE);
  526. m_ParsedInfo.SetNewCycleStatus(FALSE);
  527. m_FormatEngine.Uninitialize();
  528. }
  529. else
  530. {
  531. bstrAggregateXML += bstrTempXML;
  532. }
  533. }
  534. if (_kbhit())
  535. {
  536. _getch();
  537. bBreak = TRUE;
  538. break;
  539. }
  540. if (GetBreakEvent() == TRUE)
  541. {
  542. bBreak = TRUE;
  543. break;
  544. }
  545. }
  546. if(m_ParsedInfo.GetGlblSwitchesObject().
  547. GetAggregateFlag() && bXMLEncoding)
  548. {
  549. bstrXML += bstrAggregateXML;
  550. bstrXML += L"</COMMAND>";
  551. bFirst = TRUE;
  552. m_ParsedInfo.GetCmdSwitchesObject().
  553. SetXMLResultSet((LPWSTR) bstrXML);
  554. if (!m_FormatEngine.
  555. DisplayResults(m_ParsedInfo))
  556. {
  557. bBreak = TRUE;
  558. ssnRetCode = SESSION_ERROR;
  559. SetSessionErrorLevel(ssnRetCode);
  560. m_FormatEngine.Uninitialize();
  561. break;
  562. }
  563. m_FormatEngine.Uninitialize();
  564. bstrAggregateXML = L"";
  565. bstrXML = L"";
  566. m_ParsedInfo.SetNewCommandStatus(FALSE);
  567. }
  568. //Checking the Sucess flag and the retrievel time .
  569. if (m_ParsedInfo.GetCmdSwitchesObject().
  570. GetSuccessFlag() == TRUE &&
  571. m_ParsedInfo.GetCmdSwitchesObject().
  572. GetEverySwitchFlag() == TRUE )
  573. {
  574. bBreak = FALSE;
  575. lLoopCount++;
  576. if (!IsRedirection() &&
  577. GetBreakEvent() == FALSE)
  578. {
  579. if ( opsOutOpt == STDOUT ||
  580. bFirstEvery == TRUE)
  581. {
  582. DisplayString(IDS_I_HAKTBTC, CP_OEMCP,
  583. NULL, TRUE, TRUE);
  584. bMsgFlag = TRUE;
  585. bFirstEvery = FALSE;
  586. }
  587. }
  588. if ( ulRepeatCount != 0 )
  589. {
  590. if ( lLoopCount >= ulRepeatCount )
  591. {
  592. if (bMsgFlag && !IsRedirection())
  593. EraseMessage(IDS_I_HAKTBTC_ERASE);
  594. bBreak = TRUE;
  595. }
  596. }
  597. //No action till time out is no over
  598. // Or no key is hit.
  599. if(!bBreak)
  600. {
  601. SleepTillTimeoutOrKBhit(ulRetTime * 1000);
  602. if (bMsgFlag && !IsRedirection())
  603. {
  604. if ( opsOutOpt == STDOUT )
  605. {
  606. bMsgFlag = FALSE;
  607. EraseMessage(IDS_I_HAKTBTC_ERASE);
  608. }
  609. }
  610. }
  611. if (_kbhit())
  612. {
  613. _getch();
  614. if (bMsgFlag && !IsRedirection())
  615. EraseMessage(IDS_I_HAKTBTC_ERASE);
  616. bBreak = TRUE;
  617. }
  618. if (GetBreakEvent() == TRUE)
  619. bBreak = TRUE;
  620. }
  621. else
  622. bBreak = TRUE;
  623. if (bBreak)
  624. break;
  625. }
  626. }
  627. else if ((retCode == PARSER_ERRMSG) ||
  628. (retCode == PARSER_ERROR))
  629. {
  630. // Set the success flag to FALSE
  631. m_ParsedInfo.GetCmdSwitchesObject().
  632. SetSuccessFlag(FALSE);
  633. ssnRetCode = SESSION_ERROR;
  634. // Display the error message
  635. if (!m_FormatEngine.DisplayResults(m_ParsedInfo))
  636. ssnRetCode = SESSION_ERROR;
  637. }
  638. else if (retCode == PARSER_OUTOFMEMORY)
  639. {
  640. ssnRetCode = SESSION_ERROR;
  641. }
  642. else
  643. {
  644. CheckForPassword();
  645. // Set the success flag to TRUE
  646. m_ParsedInfo.GetCmdSwitchesObject().
  647. SetSuccessFlag(TRUE);
  648. ssnRetCode = SESSION_SUCCESS;
  649. // Display the information
  650. if (!m_FormatEngine.DisplayResults(m_ParsedInfo))
  651. ssnRetCode = SESSION_ERROR;
  652. }
  653. }
  654. else
  655. {
  656. ssnRetCode = SESSION_QUIT;
  657. }
  658. }
  659. else
  660. {
  661. m_ParsedInfo.GetCmdSwitchesObject().
  662. SetErrataCode(OUT_OF_MEMORY);
  663. ssnRetCode = SESSION_ERROR;
  664. }
  665. }
  666. // Set the session error level to be returned.
  667. if (!bExecute)
  668. SetSessionErrorLevel(ssnRetCode);
  669. }
  670. catch(_com_error& e)
  671. {
  672. m_ParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  673. m_FormatEngine.DisplayResults(m_ParsedInfo);
  674. ssnRetCode = SESSION_ERROR;
  675. SetSessionErrorLevel(ssnRetCode);
  676. }
  677. catch(CHeap_Exception)
  678. {
  679. _com_error e(WBEM_E_OUT_OF_MEMORY);
  680. m_ParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  681. m_FormatEngine.DisplayResults(m_ParsedInfo);
  682. ssnRetCode = SESSION_ERROR;
  683. g_wmiCmd.SetSessionErrorLevel(ssnRetCode);
  684. }
  685. catch(DWORD dwError)
  686. {
  687. ::SetLastError(dwError);
  688. DisplayWin32Error();
  689. ::SetLastError(dwError);
  690. ssnRetCode = SESSION_ERROR;
  691. g_wmiCmd.SetSessionErrorLevel(ssnRetCode);
  692. }
  693. catch(WMICLIINT nVal)
  694. {
  695. if (nVal == OUT_OF_MEMORY)
  696. {
  697. GetParsedInfoObject().GetCmdSwitchesObject().
  698. SetErrataCode(OUT_OF_MEMORY);
  699. m_FormatEngine.DisplayResults(m_ParsedInfo);
  700. ssnRetCode = SESSION_ERROR;
  701. SetSessionErrorLevel(ssnRetCode);
  702. }
  703. }
  704. catch(...)
  705. {
  706. ssnRetCode = SESSION_ERROR;
  707. GetParsedInfoObject().GetCmdSwitchesObject().
  708. SetErrataCode(UNKNOWN_ERROR);
  709. SetSessionErrorLevel(ssnRetCode);
  710. DisplayString(IDS_E_WMIC_UNKNOWN_ERROR, CP_OEMCP,
  711. NULL, TRUE, TRUE);
  712. }
  713. if(ssnRetCode != SESSION_QUIT)
  714. {
  715. //sets the help flag
  716. m_ParsedInfo.GetGlblSwitchesObject().SetHelpFlag(FALSE);
  717. m_ParsedInfo.GetGlblSwitchesObject().SetAskForPassFlag(FALSE);
  718. //Call Uninitialize on Parse Info
  719. m_ParsedInfo.Uninitialize(FALSE);
  720. //Call Uninitialize on Execution Engine
  721. m_ExecEngine.Uninitialize();
  722. //Call Uninitialize on Format Engine
  723. m_FormatEngine.Uninitialize();
  724. //Call Uninitialize Parser Engine
  725. m_ParserEngine.Uninitialize();
  726. }
  727. m_ParsedInfo.SetNewCommandStatus(FALSE);
  728. return ssnRetCode;
  729. }
  730. /*-------------------------------------------------------------------------
  731. Name :PollForKBhit
  732. Synopsis :Polls for keyboard input
  733. Type :Member Function (Thread procedure)
  734. Input parameters :LPVOID lpParam
  735. Output parameters :None
  736. Return Type :void
  737. Global Variables :None
  738. Calling Syntax :PollForKBhit(lpParam)
  739. Notes :None
  740. -------------------------------------------------------------------------*/
  741. DWORD WINAPI CWMICommandLine::PollForKBhit(LPVOID lpParam)
  742. {
  743. HANDLE hEvent = NULL;
  744. hEvent = *((HANDLE*) lpParam );
  745. //Checks the console for keyboard input
  746. while (1 )
  747. {
  748. if ( _kbhit() )
  749. break;
  750. else if ( WaitForSingleObject(hEvent, 500) != WAIT_TIMEOUT )
  751. break;
  752. }
  753. return(0);
  754. }
  755. /*-------------------------------------------------------------------------
  756. Name :SleepTillTimeoutOrKBhit
  757. Synopsis :It causes the process to enter a wait state
  758. by WaitForSingleObject .
  759. It creates a thread and executed PollForKBhit.
  760. Type :Member Function
  761. Input parameters :
  762. dwMilliSeconds - time-out interval in milliseconds
  763. Output parameters :None
  764. Return Type :void
  765. Global Variables :None
  766. Calling Syntax :SleepTillTimeoutOrKBhit( dwMilliSeconds)
  767. Notes :None
  768. -------------------------------------------------------------------------*/
  769. void CWMICommandLine::SleepTillTimeoutOrKBhit(DWORD dwMilliSeconds)
  770. {
  771. DWORD dwThreadId = 0;
  772. HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  773. //Create thread and execute PollForKBhit.
  774. HANDLE hPollForKBhit = CreateThread(0, 0,
  775. (LPTHREAD_START_ROUTINE)PollForKBhit, &hEvent, 0, &dwThreadId);
  776. //waits for the hPollForKBhit state or the time-out interval to elapse.
  777. DWORD dwWait = WaitForSingleObject(hPollForKBhit, dwMilliSeconds);
  778. if ( dwWait == WAIT_TIMEOUT )
  779. {
  780. SetEvent( hEvent );
  781. WaitForSingleObject(hPollForKBhit, INFINITE);
  782. }
  783. CloseHandle(hEvent);
  784. CloseHandle(hPollForKBhit);
  785. }
  786. /*-------------------------------------------------------------------------
  787. Name :IsSessionEnd
  788. Synopsis :It checks whether the keyed-in input indicates end of
  789. the session. i.e "quit" has been specified as the 1st
  790. token.
  791. Type :Member Function
  792. Input parameters :None
  793. Output parameters :None
  794. Return Type :BOOL
  795. Global Variables :None
  796. Calling Syntax :IsSessionEnd()
  797. Notes :None
  798. -------------------------------------------------------------------------*/
  799. BOOL CWMICommandLine::IsSessionEnd()
  800. {
  801. // Obtain the token vector.
  802. CHARVECTOR cvTokens = m_ParserEngine.GetCmdTokenizer().GetTokenVector();
  803. //the iterator to span throuh the vector variable
  804. CHARVECTOR::iterator theIterator;
  805. BOOL bRet=FALSE;
  806. // Check for the presence of tokens. Absence of tokens indicates
  807. // no command string is fed as input.
  808. if (cvTokens.size())
  809. {
  810. // Obtain the pointer to the beginning of the token vector.
  811. theIterator = cvTokens.begin();
  812. // Check for the presence of the keyword 'quit'
  813. if (CompareTokens(*theIterator, CLI_TOKEN_QUIT)
  814. || CompareTokens(*theIterator, CLI_TOKEN_EXIT))
  815. {
  816. bRet=TRUE;
  817. }
  818. }
  819. return bRet;
  820. }
  821. /*-------------------------------------------------------------------------
  822. Name :SetSessionErrorLevel
  823. Synopsis :Set the session error level value
  824. Type :Member Function
  825. Input parameters :
  826. ssnRetCode - session return code
  827. Output parameters :None
  828. Return Type :None
  829. Global Variables :None
  830. Calling Syntax :SetSessionErrorLevel()
  831. -------------------------------------------------------------------------*/
  832. //Set the session error value
  833. void CWMICommandLine::SetSessionErrorLevel(SESSIONRETCODE ssnRetCode)
  834. {
  835. try
  836. {
  837. if (ssnRetCode == SESSION_ERROR)
  838. {
  839. // If the COM error is not NULL , then display the error
  840. if (m_ParsedInfo.GetCmdSwitchesObject().GetCOMError())
  841. {
  842. // Getting the _com_error data.
  843. _com_error* pComError = m_ParsedInfo.GetCmdSwitchesObject().
  844. GetCOMError();
  845. m_uErrLevel = pComError->Error();
  846. }
  847. CHString chsMsg(_T("command: "));
  848. chsMsg += m_ParsedInfo.GetCmdSwitchesObject().GetCommandInput();
  849. DWORD dwThreadId = GetCurrentThreadId();
  850. if (m_ParsedInfo.GetCmdSwitchesObject().GetErrataCode())
  851. {
  852. m_uErrLevel = m_ParsedInfo.GetCmdSwitchesObject().GetErrataCode();
  853. if (m_uErrLevel == OUT_OF_MEMORY)
  854. {
  855. DisplayString(IDS_E_MEMALLOCFAILED, CP_OEMCP,
  856. NULL, TRUE, TRUE);
  857. }
  858. if (m_uErrLevel == SET_CONHNDLR_ROUTN_FAIL)
  859. {
  860. DisplayString(IDS_E_CONCTRL_HNDLRSET, CP_OEMCP,
  861. NULL, TRUE, TRUE);
  862. }
  863. if ( m_uErrLevel == OUT_OF_MEMORY ||
  864. m_uErrLevel == SET_CONHNDLR_ROUTN_FAIL )
  865. {
  866. if (m_ParsedInfo.GetErrorLogObject().GetErrLogOption())
  867. {
  868. chsMsg += _T(", Utility returned error ID.");
  869. // explicit error -1 to specify errata code.
  870. WMITRACEORERRORLOG(-1, __LINE__, __FILE__,
  871. (LPCWSTR)chsMsg,
  872. dwThreadId, m_ParsedInfo, FALSE,
  873. m_ParsedInfo.GetCmdSwitchesObject()
  874. .GetErrataCode());
  875. }
  876. }
  877. if ( m_uErrLevel == ::GetLastError() )
  878. {
  879. if (m_ParsedInfo.GetErrorLogObject().GetErrLogOption())
  880. {
  881. WMITRACEORERRORLOG(-1, __LINE__, __FILE__,
  882. _T("Win32Error"), dwThreadId,
  883. m_ParsedInfo, FALSE,
  884. m_uErrLevel);
  885. }
  886. }
  887. if (m_uErrLevel == MOFCOMP_ERROR)
  888. {
  889. if (m_ParsedInfo.GetErrorLogObject().GetErrLogOption())
  890. {
  891. WMITRACEORERRORLOG(-1, __LINE__, __FILE__,
  892. _T("MOF Compilation Error (the errorlevel "
  893. L"is utility specific)"), dwThreadId,
  894. m_ParsedInfo, FALSE, m_uErrLevel);
  895. }
  896. }
  897. }
  898. }
  899. else
  900. {
  901. // Set the error level to 0
  902. m_uErrLevel = 0;
  903. }
  904. }
  905. catch(CHeap_Exception)
  906. {
  907. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  908. }
  909. }
  910. /*-------------------------------------------------------------------------
  911. Name :GetSessionErrorLevel
  912. Synopsis :Get the session error value
  913. Type :Member Function
  914. Input parameters :None
  915. Output parameters :None
  916. Return Type :WMICLIUINT
  917. Global Variables :None
  918. Calling Syntax :GetSessionErrorLevel()
  919. -------------------------------------------------------------------------*/
  920. WMICLIUINT CWMICommandLine::GetSessionErrorLevel()
  921. {
  922. return m_uErrLevel;
  923. }
  924. /*-------------------------------------------------------------------------
  925. Name :CheckForPassword
  926. Synopsis :Prompt for user password, in case user is specified
  927. without password.
  928. Type :Member Function
  929. Input parameters :None
  930. Output parameters :None
  931. Return Type :None
  932. Global Variables :None
  933. Calling Syntax :CheckForPassword()
  934. -------------------------------------------------------------------------*/
  935. void CWMICommandLine::CheckForPassword()
  936. {
  937. if ( m_ParsedInfo.GetGlblSwitchesObject().GetAskForPassFlag() == TRUE &&
  938. m_ParsedInfo.GetGlblSwitchesObject().GetUser() != NULL )
  939. {
  940. _TCHAR szPassword[BUFFER64] = NULL_STRING;
  941. DisplayString(IDS_I_PWD_PROMPT, CP_OEMCP, NULL, TRUE);
  942. AcceptPassword(szPassword);
  943. m_ParsedInfo.GetGlblSwitchesObject().SetPassword(
  944. szPassword);
  945. }
  946. }
  947. /*-------------------------------------------------------------------------
  948. Name :RegisterMofs
  949. Synopsis :Register the mof file(s) if not registered earlier.
  950. Type :Member Function
  951. Input parameters :None
  952. Output parameters :
  953. Return Type :HRESULT
  954. Global Variables :None
  955. Calling Syntax :RegisterMofs()
  956. -------------------------------------------------------------------------*/
  957. HRESULT CWMICommandLine::RegisterMofs()
  958. {
  959. HRESULT hr = S_OK;
  960. IMofCompiler *pIMofCompiler = NULL;
  961. BOOL bCompile = FALSE;
  962. BOOL bFirst = FALSE;
  963. DWORD dwBufSize = BUFFER32;
  964. _TCHAR pszLocale[BUFFER32] = NULL_STRING;
  965. _TCHAR szKeyValue[BUFFER32] = NULL_STRING;
  966. _TCHAR* pszBuffer = NULL;
  967. WMICLIINT nError = 0;
  968. HRESULT hRes = S_OK;
  969. LONG lRetVal = 0;
  970. try
  971. {
  972. _bstr_t bstrMofPath, bstrNS;
  973. // Check|Create the registry entry.
  974. bFirst = IsFirstTime();
  975. // Check whether the "mofcompstatus" key value is 0
  976. if (!bFirst)
  977. {
  978. // Query the "mofcompstatus" mode
  979. lRetVal = RegQueryValueEx(m_hKey, L"mofcompstatus", NULL, NULL,
  980. (LPBYTE)szKeyValue, &dwBufSize);
  981. if (lRetVal == ERROR_SUCCESS)
  982. {
  983. // If the value is not "1", then set the bFirst to TRUE
  984. if (!CompareTokens(szKeyValue, CLI_TOKEN_ONE))
  985. bFirst = TRUE;
  986. }
  987. else
  988. {
  989. ::SetLastError(lRetVal);
  990. throw (::GetLastError());
  991. }
  992. }
  993. // If the WMIC is being used for the first time.
  994. if (bFirst)
  995. DisplayString(IDS_I_WMIC_INST, CP_OEMCP);
  996. // Create an instance of the IMofCompiler interface.
  997. hr = CoCreateInstance(CLSID_MofCompiler,
  998. NULL,
  999. CLSCTX_INPROC_SERVER,
  1000. IID_IMofCompiler,
  1001. (LPVOID *) &pIMofCompiler);
  1002. ONFAILTHROWERROR(hr);
  1003. UINT nSize = 0;
  1004. pszBuffer = new _TCHAR [MAX_PATH];
  1005. if(pszBuffer == NULL)
  1006. throw OUT_OF_MEMORY;
  1007. // Obtain the system directory path
  1008. nSize = GetSystemDirectory(pszBuffer, MAX_PATH);
  1009. if(nSize)
  1010. {
  1011. if (nSize > MAX_PATH)
  1012. {
  1013. SAFEDELETE(pszBuffer);
  1014. pszBuffer = new _TCHAR [nSize + 1];
  1015. if(pszBuffer == NULL)
  1016. throw OUT_OF_MEMORY;
  1017. }
  1018. if (!GetSystemDirectory(pszBuffer, nSize))
  1019. throw (::GetLastError());
  1020. }
  1021. else
  1022. {
  1023. throw(::GetLastError());
  1024. }
  1025. /* Frame the location of the mof file(s) %systemdir%\\wbem\\ */
  1026. bstrMofPath = _bstr_t(pszBuffer) + _bstr_t(L"\\wbem\\");
  1027. SAFEDELETE(pszBuffer);
  1028. // Frame the namespace
  1029. bstrNS = L"\\\\.\\root\\cli";
  1030. // If first time (or) the namespace "root\cli" is not
  1031. // available then mofcomp cli.mof.
  1032. if (bFirst || !IsNSAvailable(bstrNS))
  1033. {
  1034. bCompile = TRUE;
  1035. // Register the Cli.mof
  1036. hr = CompileMOFFile(pIMofCompiler,
  1037. bstrMofPath + _bstr_t(L"Cli.mof"),
  1038. nError);
  1039. ONFAILTHROWERROR(hr);
  1040. if (nError)
  1041. throw MOFCOMP_ERROR;
  1042. }
  1043. // Frame the localized namespace
  1044. _stprintf(pszLocale, _T("ms_%x"), GetSystemDefaultLangID());
  1045. bstrNS = _bstr_t(L"\\\\.\\root\\cli\\") + _bstr_t(pszLocale);
  1046. // If first time (or) the namespace "root\cli\ms_xxx" is not
  1047. // available then mofcomp cliegaliases.mfl.
  1048. if (bFirst || !IsNSAvailable(bstrNS))
  1049. {
  1050. bCompile = TRUE;
  1051. // Register the CliEgAliases.mfl
  1052. hr = CompileMOFFile(pIMofCompiler,
  1053. bstrMofPath + _bstr_t(L"CliEgAliases.mfl"),
  1054. nError);
  1055. ONFAILTHROWERROR(hr);
  1056. if (nError)
  1057. throw MOFCOMP_ERROR;
  1058. }
  1059. // If compiled either cli.mof (or) cliegaliases.mfl
  1060. if (bCompile == TRUE)
  1061. {
  1062. // Register the CliEgAliases.mof
  1063. hr = CompileMOFFile(pIMofCompiler,
  1064. bstrMofPath + _bstr_t(L"CliEgAliases.mof"),
  1065. nError);
  1066. ONFAILTHROWERROR(hr);
  1067. if (nError)
  1068. throw MOFCOMP_ERROR;
  1069. }
  1070. SAFEIRELEASE(pIMofCompiler);
  1071. // Set the default value.
  1072. lRetVal = RegSetValueEx(m_hKey, L"mofcompstatus", 0,
  1073. REG_SZ, (LPBYTE) CLI_TOKEN_ONE,
  1074. lstrlen(CLI_TOKEN_ONE) + 1);
  1075. if (lRetVal != ERROR_SUCCESS)
  1076. {
  1077. ::SetLastError(lRetVal);
  1078. // failed to set the default value
  1079. throw (::GetLastError());
  1080. }
  1081. if (m_hKey != NULL)
  1082. {
  1083. RegCloseKey(m_hKey);
  1084. m_hKey = NULL;
  1085. }
  1086. }
  1087. catch(WMICLIINT nErr)
  1088. {
  1089. SAFEIRELEASE(pIMofCompiler);
  1090. SAFEDELETE(pszBuffer);
  1091. throw nErr;
  1092. }
  1093. catch(_com_error& e)
  1094. {
  1095. SAFEIRELEASE(pIMofCompiler);
  1096. SAFEDELETE(pszBuffer);
  1097. hr = e.Error();
  1098. }
  1099. catch (DWORD dwError)
  1100. {
  1101. SAFEIRELEASE(pIMofCompiler);
  1102. SAFEDELETE(pszBuffer);
  1103. throw dwError;
  1104. }
  1105. if (bFirst)
  1106. EraseMessage(IDS_I_WMIC_INST_ERASE);
  1107. return hr;
  1108. }
  1109. /*-------------------------------------------------------------------------
  1110. Name :IsFirstTime
  1111. Synopsis :Checks for the availability of the registry location
  1112. "HKLM\SOFTWARE\Microsoft\Wbem\WMIC", creates one if does
  1113. not exist.
  1114. Type :Member Function
  1115. Input parameters :None
  1116. Output parameters :
  1117. Return Type :
  1118. BOOL: TRUE - registry entry created
  1119. FALSE - registry entry already available.
  1120. Global Variables :None
  1121. Calling Syntax :IsFirstTime()
  1122. -------------------------------------------------------------------------*/
  1123. BOOL CWMICommandLine::IsFirstTime()
  1124. {
  1125. BOOL bFirst = FALSE;
  1126. DWORD dwDisposition = 0;
  1127. TCHAR szKeyValue[BUFFER32] = NULL_STRING;
  1128. LONG lRetVal = 0;
  1129. // Open|Create the registry key
  1130. lRetVal = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
  1131. L"SOFTWARE\\\\Microsoft\\\\Wbem\\\\WMIC",
  1132. 0, NULL_STRING, REG_OPTION_NON_VOLATILE,
  1133. KEY_ALL_ACCESS, NULL, &m_hKey, &dwDisposition);
  1134. if (lRetVal == ERROR_SUCCESS)
  1135. {
  1136. // If the registry entry is not available
  1137. if (dwDisposition == REG_CREATED_NEW_KEY)
  1138. {
  1139. bFirst = TRUE;
  1140. lstrcpy(szKeyValue, _T("0"));
  1141. // Set the default value i.e '0'.
  1142. lRetVal = RegSetValueEx(m_hKey, L"mofcompstatus", 0,
  1143. REG_SZ, (LPBYTE) szKeyValue,
  1144. lstrlen(szKeyValue) + 1);
  1145. if (lRetVal != ERROR_SUCCESS)
  1146. {
  1147. // failed to set the default value
  1148. ::SetLastError(lRetVal);
  1149. throw (::GetLastError());
  1150. }
  1151. }
  1152. }
  1153. else
  1154. {
  1155. ::SetLastError(lRetVal);
  1156. throw (::GetLastError());
  1157. }
  1158. return bFirst;
  1159. }
  1160. /*-------------------------------------------------------------------------
  1161. Name :IsNSAvailable
  1162. Synopsis :Checks whether the namespace specified exists
  1163. Type :Member Function
  1164. Input parameters :
  1165. bstrNS - namespace
  1166. Output parameters :None
  1167. Return Type :
  1168. BOOL: TRUE - namespace exists
  1169. FALSE - namespace does not exist
  1170. Global Variables :None
  1171. Calling Syntax :IsNSAvailable()
  1172. -------------------------------------------------------------------------*/
  1173. BOOL CWMICommandLine::IsNSAvailable(const _bstr_t& bstrNS)
  1174. {
  1175. HRESULT hr = S_OK;
  1176. IWbemServices *pISvc = NULL;
  1177. BOOL bNSExist = TRUE;
  1178. hr = m_pIWbemLocator->ConnectServer(bstrNS, NULL, NULL, NULL, 0,
  1179. NULL, NULL, &pISvc);
  1180. // If the namespace does not exist.
  1181. if (FAILED(hr) && (hr == WBEM_E_INVALID_PARAMETER
  1182. || hr == WBEM_E_INVALID_NAMESPACE))
  1183. {
  1184. bNSExist = FALSE;
  1185. }
  1186. SAFEIRELEASE(pISvc);
  1187. return bNSExist;
  1188. }
  1189. /*-------------------------------------------------------------------------
  1190. Name :CompileMOFFile
  1191. Synopsis :mofcomp's the file specified as input parameter thru
  1192. bstrFile
  1193. Type :Member Function
  1194. Input parameters :
  1195. pIMofComp - IMofCompiler interface pointer.
  1196. bstrFile - filename.
  1197. nError - parsing phase error.
  1198. Output parameters :None
  1199. Return Type :HRESULT
  1200. Global Variables :None
  1201. Calling Syntax :CompileMOFFile()
  1202. -------------------------------------------------------------------------*/
  1203. HRESULT CWMICommandLine::CompileMOFFile(IMofCompiler* pIMofComp,
  1204. const _bstr_t& bstrFile,
  1205. WMICLIINT& nError)
  1206. {
  1207. HRESULT hr = S_OK;
  1208. WBEM_COMPILE_STATUS_INFO wcsInfo;
  1209. try
  1210. {
  1211. // Register the moffile
  1212. hr = pIMofComp->CompileFile(bstrFile, NULL, NULL, NULL,
  1213. NULL, 0, 0, 0, &wcsInfo);
  1214. // If the compilation is not successful
  1215. if (hr == WBEM_S_FALSE)
  1216. {
  1217. _TCHAR szPhaseErr[BUFFER32] = NULL_STRING,
  1218. szComplErr[BUFFER32] = NULL_STRING;
  1219. _bstr_t bstrMsg;
  1220. _stprintf(szPhaseErr, _T("%d"), wcsInfo.lPhaseError);
  1221. _stprintf(szComplErr, _T("0x%x"), wcsInfo.hRes);
  1222. WMIFormatMessage(IDS_I_MOF_PARSE_ERROR, 3, bstrMsg,
  1223. (WCHAR*) bstrFile, szPhaseErr,
  1224. szComplErr);
  1225. DisplayMessage((WCHAR*) bstrMsg, CP_OEMCP, TRUE, FALSE);
  1226. nError = wcsInfo.lPhaseError;
  1227. }
  1228. }
  1229. catch(_com_error& e)
  1230. {
  1231. hr = e.Error();
  1232. }
  1233. return hr;
  1234. }
  1235. /*-------------------------------------------------------------------------
  1236. Name :SetBreakEvent
  1237. Synopsis :This function sets the CTRC+C (break) event flag
  1238. Type :Member Function
  1239. Input parameters :
  1240. bFlag - TRUE or FALSE
  1241. Output parameters :None
  1242. Return Type :None
  1243. Global Variables :None
  1244. Calling Syntax :SetBreakEvent()
  1245. -------------------------------------------------------------------------*/
  1246. void CWMICommandLine::SetBreakEvent(BOOL bFlag)
  1247. {
  1248. m_bBreakEvent = bFlag;
  1249. }
  1250. /*-------------------------------------------------------------------------
  1251. Name :GetBreakEvent
  1252. Synopsis :This function returns the break event status.
  1253. Type :Member Function
  1254. Input parameters :None
  1255. Output parameters :None
  1256. Return Type :BOOL
  1257. Global Variables :None
  1258. Calling Syntax :GetBreakEvent()
  1259. -------------------------------------------------------------------------*/
  1260. BOOL CWMICommandLine::GetBreakEvent()
  1261. {
  1262. return m_bBreakEvent;
  1263. }
  1264. /*-------------------------------------------------------------------------
  1265. Name :SetAcceptCommand
  1266. Synopsis :This function sets the accept command flag
  1267. Type :Member Function
  1268. Input parameters :
  1269. bFlag - TRUE or FALSE
  1270. Output parameters :None
  1271. Return Type :None
  1272. Global Variables :None
  1273. Calling Syntax :SetAcceptComamnd()
  1274. -------------------------------------------------------------------------*/
  1275. void CWMICommandLine::SetAcceptCommand(BOOL bFlag)
  1276. {
  1277. m_bAccCmd = bFlag;
  1278. }
  1279. /*-------------------------------------------------------------------------
  1280. Name :GetAcceptCommand
  1281. Synopsis :This function returns accept command flag status.
  1282. Type :Member Function
  1283. Input parameters :None
  1284. Output parameters :None
  1285. Return Type :BOOL
  1286. Global Variables :None
  1287. Calling Syntax :GetAcceptCommand()
  1288. -------------------------------------------------------------------------*/
  1289. BOOL CWMICommandLine::GetAcceptCommand()
  1290. {
  1291. return m_bAccCmd;
  1292. }
  1293. /*-------------------------------------------------------------------------
  1294. Name :GetDisplayResultsFlag
  1295. Synopsis :This function returns Display Results flag status.
  1296. Type :Member Function
  1297. Input parameters :None
  1298. Output parameters :None
  1299. Return Type :BOOL
  1300. Global Variables :None
  1301. Calling Syntax :GetDisplayResultsFlag()
  1302. -------------------------------------------------------------------------*/
  1303. BOOL CWMICommandLine::GetDisplayResultsFlag()
  1304. {
  1305. return m_bDispRes;
  1306. }
  1307. /*-------------------------------------------------------------------------
  1308. Name :SetDisplayResultsFlag
  1309. Synopsis :This function sets the display results flag status
  1310. Type :Member Function
  1311. Input parameters :
  1312. bFlag - TRUE or FALSE
  1313. Output parameters :None
  1314. Return Type :None
  1315. Global Variables :None
  1316. Calling Syntax :SetDisplayResultsFlag()
  1317. -------------------------------------------------------------------------*/
  1318. void CWMICommandLine::SetDisplayResultsFlag(BOOL bFlag)
  1319. {
  1320. m_bDispRes = bFlag;
  1321. }
  1322. /*-------------------------------------------------------------------------
  1323. Name :SetInitWinSock
  1324. Synopsis :This function sets the windows socket library
  1325. initialization status
  1326. Type :Member Function
  1327. Input parameters :
  1328. bFlag - TRUE or FALSE
  1329. Output parameters :None
  1330. Return Type :None
  1331. Global Variables :None
  1332. Calling Syntax :SetInitWinSock()
  1333. -------------------------------------------------------------------------*/
  1334. void CWMICommandLine::SetInitWinSock(BOOL bFlag)
  1335. {
  1336. m_bInitWinSock = bFlag;
  1337. }
  1338. /*-------------------------------------------------------------------------
  1339. Name :GetInitWinSock
  1340. Synopsis :This function returns the socket library initialization
  1341. status
  1342. Type :Member Function
  1343. Input parameters :None
  1344. Output parameters :None
  1345. Return Type :BOOL
  1346. Global Variables :None
  1347. Calling Syntax :GetInitWinSock()
  1348. -------------------------------------------------------------------------*/
  1349. BOOL CWMICommandLine::GetInitWinSock()
  1350. {
  1351. return m_bInitWinSock;
  1352. }
  1353. /*-------------------------------------------------------------------------
  1354. Name :AddToClipBoardBuffer
  1355. Synopsis :This function buffers the data to be added to the clip
  1356. board.
  1357. Type :Member Function
  1358. Input parameters :pszOutput - string to be buffered
  1359. Output parameters :None
  1360. Return Type :void
  1361. Global Variables :None
  1362. Calling Syntax :AddToClipBoardBuffer(pszOutput)
  1363. -------------------------------------------------------------------------*/
  1364. void CWMICommandLine::AddToClipBoardBuffer(LPSTR pszOutput)
  1365. {
  1366. try
  1367. {
  1368. if ( pszOutput != NULL )
  1369. {
  1370. if ( m_bstrClipBoardBuffer == _bstr_t("") )
  1371. m_bstrClipBoardBuffer = _bstr_t(pszOutput);
  1372. else
  1373. m_bstrClipBoardBuffer += _bstr_t(pszOutput);
  1374. }
  1375. }
  1376. catch(_com_error& e)
  1377. {
  1378. _com_issue_error(e.Error());
  1379. }
  1380. }
  1381. /*-------------------------------------------------------------------------
  1382. Name :GetClipBoardBuffer
  1383. Synopsis :This function return the buffered data for the
  1384. clipboard
  1385. Type :Member Function
  1386. Input parameters :None
  1387. Output parameters :None
  1388. Return Type :_bstr_t&
  1389. Global Variables :None
  1390. Calling Syntax :GetClipBoardBuffer()
  1391. -------------------------------------------------------------------------*/
  1392. _bstr_t& CWMICommandLine::GetClipBoardBuffer()
  1393. {
  1394. return m_bstrClipBoardBuffer;
  1395. }
  1396. // Clear Clip Board Buffer.
  1397. /*-------------------------------------------------------------------------
  1398. Name :EmptyClipBoardBuffer
  1399. Synopsis :This function clears the clipboard buffer.
  1400. Type :Member Function
  1401. Input parameters :None
  1402. Output parameters :None
  1403. Return Type :void
  1404. Global Variables :None
  1405. Calling Syntax :EmptyClipBoardBuffer()
  1406. -------------------------------------------------------------------------*/
  1407. void CWMICommandLine::EmptyClipBoardBuffer()
  1408. {
  1409. try
  1410. {
  1411. m_bstrClipBoardBuffer = _bstr_t("");
  1412. }
  1413. catch(_com_error& e)
  1414. {
  1415. _com_issue_error(e.Error());
  1416. }
  1417. }
  1418. /*-------------------------------------------------------------------------
  1419. Name :ReadXMLOrBatchFile
  1420. Synopsis :Check if the file is xml or batch file. If it is batch file
  1421. then parse it, get commands and write the commands into
  1422. batch file.
  1423. Type :Member Function
  1424. Input parameters :
  1425. hInFile - Handle to XML or Batch file
  1426. Output parameters :None
  1427. Return Type :BOOL
  1428. Global Variables :None
  1429. Calling Syntax :ReadXMLOrBatchFile()
  1430. -------------------------------------------------------------------------*/
  1431. BOOL CWMICommandLine::ReadXMLOrBatchFile(HANDLE hInFile)
  1432. {
  1433. // Check if the file is xml, if yes store its contents in buffer, parse
  1434. // it and then store parsed info in another file, if not then copy all
  1435. // contents in another file as it is.
  1436. HRESULT hr = 0;
  1437. BOOL bRetValue = TRUE;
  1438. HANDLE hOutFile = NULL;
  1439. IXMLDOMDocument *pIXMLDOMDocument = NULL;
  1440. IXMLDOMElement *pIXMLDOMElement = NULL;
  1441. IXMLDOMNode *pIXMLDOMNode = NULL;
  1442. IXMLDOMNodeList *pIXMLDOMNodeList = NULL;
  1443. BSTR bstrItemText = NULL;
  1444. DWORD dwThreadId = GetCurrentThreadId();
  1445. // Get the TRACE status
  1446. BOOL bTrace = m_ParsedInfo.GetGlblSwitchesObject().GetTraceStatus();
  1447. // Get the Logging mode (VERBOSE | ERRORONLY | NOLOGGING)
  1448. ERRLOGOPT eloErrLogOpt = m_ParsedInfo.GetErrorLogObject().GetErrLogOption();
  1449. try
  1450. {
  1451. //Read all input bytes
  1452. DWORD dwNumberOfBytes = 0;
  1453. _bstr_t bstrInput;
  1454. _TCHAR* pszBuffer = NULL;
  1455. while(TRUE)
  1456. {
  1457. pszBuffer = new _TCHAR[MAX_BUFFER];
  1458. if (pszBuffer)
  1459. {
  1460. TCHAR *pBuf = NULL;
  1461. pBuf = _fgetts(pszBuffer, MAX_BUFFER-1, stdin);
  1462. // Indicates end of file
  1463. if (pBuf == NULL)
  1464. {
  1465. SAFEDELETE(pszBuffer);
  1466. break;
  1467. }
  1468. bstrInput += _bstr_t(pszBuffer) + _bstr_t("\n");
  1469. }
  1470. else
  1471. break;
  1472. SAFEDELETE(pszBuffer);
  1473. }
  1474. //Create a file and returns the handle
  1475. hOutFile = CreateFile(TEMP_BATCH_FILE, GENERIC_WRITE, 0,
  1476. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
  1477. NULL);
  1478. if (hOutFile == INVALID_HANDLE_VALUE)
  1479. {
  1480. throw (::GetLastError());;
  1481. }
  1482. hr=CoCreateInstance(CLSID_DOMDocument, NULL,
  1483. CLSCTX_INPROC_SERVER,
  1484. IID_IXMLDOMDocument2,
  1485. (LPVOID*)&pIXMLDOMDocument);
  1486. if (bTrace || eloErrLogOpt)
  1487. {
  1488. CHString chsMsg;
  1489. chsMsg.Format(L"CoCreateInstance(CLSID_DOMDocument, "
  1490. L"NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument2,"
  1491. L" -)");
  1492. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  1493. dwThreadId, m_ParsedInfo, bTrace);
  1494. }
  1495. ONFAILTHROWERROR(hr);
  1496. VARIANT_BOOL bSuccess = VARIANT_FALSE;
  1497. hr = pIXMLDOMDocument->loadXML(bstrInput,&bSuccess);
  1498. if(FAILED(hr) || bSuccess == VARIANT_FALSE)
  1499. {
  1500. //writes data to a file
  1501. if (!WriteFile(hOutFile, (LPSTR)bstrInput, bstrInput.length(),
  1502. &dwNumberOfBytes, NULL))
  1503. {
  1504. throw(::GetLastError());
  1505. }
  1506. }
  1507. else
  1508. {
  1509. // Traverse the XML node and get the content of COMMANDLINE nodes
  1510. // Get the document element.
  1511. hr = pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement);
  1512. if (bTrace || eloErrLogOpt)
  1513. {
  1514. WMITRACEORERRORLOG(hr, __LINE__,
  1515. __FILE__, _T("IXMLDOMDocument::get_documentElement(-)"),
  1516. dwThreadId, m_ParsedInfo, bTrace);
  1517. }
  1518. ONFAILTHROWERROR(hr);
  1519. if (pIXMLDOMElement != NULL)
  1520. {
  1521. hr = pIXMLDOMElement->getElementsByTagName(
  1522. _bstr_t(L"COMMANDLINE"), &pIXMLDOMNodeList);
  1523. if (bTrace || eloErrLogOpt)
  1524. {
  1525. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  1526. _T("IXMLDOMElement::getElementsByTagName"
  1527. L"(L\"COMMANDLINE\", -)"),dwThreadId,m_ParsedInfo, bTrace);
  1528. }
  1529. ONFAILTHROWERROR(hr);
  1530. LONG value = 0;
  1531. hr = pIXMLDOMNodeList->get_length(&value);
  1532. if (bTrace || eloErrLogOpt)
  1533. {
  1534. WMITRACEORERRORLOG(hr, __LINE__,
  1535. __FILE__, _T("IXMLDOMNodeList::get_length(-)"),
  1536. dwThreadId, m_ParsedInfo, bTrace);
  1537. }
  1538. ONFAILTHROWERROR(hr);
  1539. for(WMICLIINT i = 0; i < value; i++)
  1540. {
  1541. hr = pIXMLDOMNodeList->get_item(i, &pIXMLDOMNode);
  1542. if (bTrace || eloErrLogOpt)
  1543. {
  1544. WMITRACEORERRORLOG(hr, __LINE__,
  1545. __FILE__, _T("IXMLDOMNodeList::get_item(-,-)"),
  1546. dwThreadId, m_ParsedInfo, bTrace);
  1547. }
  1548. ONFAILTHROWERROR(hr);
  1549. if (pIXMLDOMNode == NULL)
  1550. continue;
  1551. hr = pIXMLDOMNode->get_text(&bstrItemText);
  1552. if (bTrace || eloErrLogOpt)
  1553. {
  1554. WMITRACEORERRORLOG(hr, __LINE__,
  1555. __FILE__, _T("IXMLDOMNode::get_text(-)"),
  1556. dwThreadId, m_ParsedInfo, bTrace);
  1557. }
  1558. ONFAILTHROWERROR(hr);
  1559. //write in the file
  1560. _bstr_t bstrItem = _bstr_t(bstrItemText);
  1561. BOOL bRetCode = WriteFile(hOutFile, (LPSTR)bstrItem,
  1562. bstrItem.length(),
  1563. &dwNumberOfBytes, NULL);
  1564. if(bRetCode == 0)
  1565. {
  1566. throw (::GetLastError());
  1567. }
  1568. bRetCode = WriteFile(hOutFile, "\n", 1,
  1569. &dwNumberOfBytes, NULL);
  1570. if(bRetCode == 0)
  1571. {
  1572. throw (::GetLastError());
  1573. }
  1574. SAFEBSTRFREE(bstrItemText);
  1575. SAFEIRELEASE(pIXMLDOMNode);
  1576. }
  1577. SAFEIRELEASE(pIXMLDOMNodeList);
  1578. SAFEIRELEASE(pIXMLDOMElement);
  1579. }
  1580. SAFEIRELEASE(pIXMLDOMDocument);
  1581. }
  1582. if(hInFile)
  1583. CloseHandle(hInFile);
  1584. if(hOutFile)
  1585. CloseHandle(hOutFile);
  1586. _tfreopen(_T("CONIN$"),_T("r"),stdin);
  1587. bRetValue=TRUE;
  1588. }
  1589. catch(_com_error& e)
  1590. {
  1591. SAFEIRELEASE(pIXMLDOMDocument);
  1592. SAFEIRELEASE(pIXMLDOMElement);
  1593. SAFEIRELEASE(pIXMLDOMNode);
  1594. SAFEIRELEASE(pIXMLDOMNodeList);
  1595. SAFEBSTRFREE(bstrItemText);
  1596. if(hInFile)
  1597. CloseHandle(hInFile);
  1598. if(hOutFile)
  1599. CloseHandle(hOutFile);
  1600. m_ParsedInfo.GetCmdSwitchesObject().SetCOMError(e);
  1601. bRetValue = FALSE;
  1602. GetFormatObject().DisplayResults(m_ParsedInfo, CP_OEMCP);
  1603. }
  1604. catch(DWORD dwError)
  1605. {
  1606. SAFEIRELEASE(pIXMLDOMDocument);
  1607. SAFEIRELEASE(pIXMLDOMElement);
  1608. SAFEIRELEASE(pIXMLDOMNode);
  1609. SAFEIRELEASE(pIXMLDOMNodeList);
  1610. SAFEBSTRFREE(bstrItemText);
  1611. if(hInFile)
  1612. CloseHandle(hInFile);
  1613. if(hOutFile)
  1614. CloseHandle(hOutFile);
  1615. ::SetLastError(dwError);
  1616. DisplayWin32Error();
  1617. m_ParsedInfo.GetCmdSwitchesObject().SetErrataCode(dwError);
  1618. ::SetLastError(dwError);
  1619. bRetValue=FALSE;
  1620. GetFormatObject().DisplayResults(m_ParsedInfo, CP_OEMCP);
  1621. }
  1622. catch(CHeap_Exception)
  1623. {
  1624. SAFEIRELEASE(pIXMLDOMDocument);
  1625. SAFEIRELEASE(pIXMLDOMElement);
  1626. SAFEIRELEASE(pIXMLDOMNode);
  1627. SAFEIRELEASE(pIXMLDOMNodeList);
  1628. SAFEBSTRFREE(bstrItemText);
  1629. if(hInFile)
  1630. CloseHandle(hInFile);
  1631. if(hOutFile)
  1632. CloseHandle(hOutFile);
  1633. bRetValue=FALSE;
  1634. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1635. }
  1636. return bRetValue;
  1637. }
  1638. /*-------------------------------------------------------------------------
  1639. Name :FrameXMLHeader
  1640. Synopsis :Frames the XML header info
  1641. Type :Member Function
  1642. Input parameters :
  1643. nIter - Every count
  1644. Output parameters :
  1645. bstrHeader - String to containg XML header info
  1646. Return Type :void
  1647. Global Variables :None
  1648. Calling Syntax :FrameXMLHeader()
  1649. -------------------------------------------------------------------------*/
  1650. void CWMICommandLine::FrameXMLHeader(_bstr_t& bstrHeader, WMICLIINT nIter)
  1651. {
  1652. try
  1653. {
  1654. CHString strTemp;
  1655. _bstr_t bstrString = m_ParsedInfo.GetGlblSwitchesObject().
  1656. GetMgmtStationName();
  1657. FindAndReplaceEntityReferences(bstrString);
  1658. strTemp.Format(L"<COMMAND SEQUENCENUM=\"%d\" ISSUEDFROM=\"%s\" "
  1659. L"STARTTIME=\"%s\" EVERYCOUNT=\"%d\">",
  1660. m_ParsedInfo.GetGlblSwitchesObject().GetSequenceNumber(),
  1661. (TCHAR*)bstrString,
  1662. m_ParsedInfo.GetGlblSwitchesObject().GetStartTime(),
  1663. nIter);
  1664. bstrHeader = _bstr_t(strTemp);
  1665. }
  1666. catch(_com_error& e)
  1667. {
  1668. _com_issue_error(e.Error());
  1669. }
  1670. //trap to catch CHeap_Exception
  1671. catch(CHeap_Exception)
  1672. {
  1673. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1674. }
  1675. }
  1676. /*-------------------------------------------------------------------------
  1677. Name :FrameRequestNode
  1678. Synopsis :Frames the XML string for Request info
  1679. Type :Member Function
  1680. Input parameters :None
  1681. Output parameters :
  1682. bstrRequest - String to containg Request info in XML form
  1683. Return Type :void
  1684. Global Variables :None
  1685. Calling Syntax :FrameRequestNode()
  1686. -------------------------------------------------------------------------*/
  1687. void CWMICommandLine::FrameRequestNode(_bstr_t& bstrRequest)
  1688. {
  1689. try
  1690. {
  1691. CHString strTemp;
  1692. _bstr_t bstrContext;
  1693. _bstr_t bstrCommandComponent;
  1694. bstrRequest = L"<REQUEST>";
  1695. _bstr_t bstrString = m_ParsedInfo.GetCmdSwitchesObject().GetCommandInput();
  1696. FindAndReplaceEntityReferences(bstrString);
  1697. strTemp.Format(L"<COMMANDLINE>%s</COMMANDLINE>",
  1698. (TCHAR*)bstrString);
  1699. bstrRequest += _bstr_t(strTemp);
  1700. FrameCommandLineComponents(bstrCommandComponent);
  1701. bstrRequest += bstrCommandComponent;
  1702. FrameContextInfoFragment(bstrContext);
  1703. bstrRequest += bstrContext;
  1704. bstrRequest += L"</REQUEST>";
  1705. }
  1706. catch(_com_error& e)
  1707. {
  1708. _com_issue_error(e.Error());
  1709. }
  1710. //trap to catch CHeap_Exception
  1711. catch(CHeap_Exception)
  1712. {
  1713. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1714. }
  1715. }
  1716. /*-------------------------------------------------------------------------
  1717. Name :FrameContextInfoFragment
  1718. Synopsis :Frames the XML string for context info
  1719. Type :Member Function
  1720. Input parameters :None
  1721. Output parameters :
  1722. bstrContext - String to containg context info in XML form
  1723. Return Type :void
  1724. Global Variables :None
  1725. Calling Syntax :FrameContextInfoFragment()
  1726. -------------------------------------------------------------------------*/
  1727. void CWMICommandLine::FrameContextInfoFragment(_bstr_t& bstrContext)
  1728. {
  1729. try
  1730. {
  1731. _bstr_t bstrAuthLevel, bstrImpLevel, bstrPrivileges,
  1732. bstrTrace, bstrRecordPath, bstrInteractive,
  1733. bstrFailFast, bstrAppend, bstrOutput,
  1734. bstrUser, bstrAggregate, bstrNamespace,
  1735. bstrRole, bstrLocale;
  1736. CHString strTemp;
  1737. m_ParsedInfo.GetGlblSwitchesObject().GetImpLevelTextDesc(bstrImpLevel);
  1738. m_ParsedInfo.GetGlblSwitchesObject().GetAuthLevelTextDesc(bstrAuthLevel);
  1739. m_ParsedInfo.GetGlblSwitchesObject().GetPrivilegesTextDesc(bstrPrivileges);
  1740. m_ParsedInfo.GetGlblSwitchesObject().GetTraceTextDesc(bstrTrace);
  1741. m_ParsedInfo.GetGlblSwitchesObject().GetInteractiveTextDesc(bstrInteractive);
  1742. m_ParsedInfo.GetGlblSwitchesObject().GetFailFastTextDesc(bstrFailFast);
  1743. m_ParsedInfo.GetGlblSwitchesObject().GetAggregateTextDesc(bstrAggregate);
  1744. m_ParsedInfo.GetGlblSwitchesObject().GetOutputOrAppendTextDesc(bstrOutput,
  1745. TRUE);
  1746. FindAndReplaceEntityReferences(bstrOutput);
  1747. m_ParsedInfo.GetGlblSwitchesObject().GetOutputOrAppendTextDesc(bstrAppend,
  1748. FALSE);
  1749. FindAndReplaceEntityReferences(bstrAppend);
  1750. m_ParsedInfo.GetGlblSwitchesObject().GetRecordPathDesc(bstrRecordPath);
  1751. FindAndReplaceEntityReferences(bstrRecordPath);
  1752. m_ParsedInfo.GetUserDesc(bstrUser);
  1753. FindAndReplaceEntityReferences(bstrUser);
  1754. bstrNamespace = m_ParsedInfo.GetGlblSwitchesObject().GetNameSpace();
  1755. FindAndReplaceEntityReferences(bstrNamespace);
  1756. bstrRole = m_ParsedInfo.GetGlblSwitchesObject().GetRole();
  1757. FindAndReplaceEntityReferences(bstrRole);
  1758. bstrLocale = m_ParsedInfo.GetGlblSwitchesObject().GetLocale();
  1759. FindAndReplaceEntityReferences(bstrLocale);
  1760. strTemp.Format(L"<CONTEXT><NAMESPACE>%s</NAMESPACE><ROLE>%s</ROLE>"
  1761. L"<IMPLEVEL>%s</IMPLEVEL><AUTHLEVEL>%s</AUTHLEVEL>"
  1762. L"<LOCALE>%s</LOCALE><PRIVILEGES>%s</PRIVILEGES>"
  1763. L"<TRACE>%s</TRACE><RECORD>%s</RECORD>"
  1764. L"<INTERACTIVE>%s</INTERACTIVE>"
  1765. L"<FAILFAST>%s</FAILFAST><OUTPUT>%s</OUTPUT>"
  1766. L"<APPEND>%s</APPEND><USER>%s</USER>"
  1767. L"<AGGREGATE>%s</AGGREGATE></CONTEXT>",
  1768. (LPWSTR)bstrNamespace, (LPWSTR)bstrRole,
  1769. (LPWSTR)bstrImpLevel, (LPWSTR) bstrAuthLevel,
  1770. (LPWSTR)bstrLocale,
  1771. (LPWSTR)bstrPrivileges, (LPWSTR)bstrTrace,
  1772. (LPWSTR)bstrRecordPath, (LPWSTR) bstrInteractive,
  1773. (LPWSTR)bstrFailFast, (LPWSTR) bstrOutput,
  1774. (LPWSTR)bstrAppend, (LPWSTR) bstrUser,
  1775. (LPWSTR)bstrAggregate);
  1776. bstrContext = strTemp;
  1777. }
  1778. catch(_com_error& e)
  1779. {
  1780. _com_issue_error(e.Error());
  1781. }
  1782. //trap to catch CHeap_Exception
  1783. catch(CHeap_Exception)
  1784. {
  1785. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1786. }
  1787. }
  1788. /*-------------------------------------------------------------------------
  1789. Name :FrameCommandLineComponents
  1790. Synopsis :Frames the XML string for commandline info
  1791. Type :Member Function
  1792. Input parameters :None
  1793. Output parameters :
  1794. bstrContext - String to containg commandline info in XML form
  1795. Return Type :void
  1796. Global Variables :None
  1797. Calling Syntax :FrameCommandLineComponents()
  1798. -------------------------------------------------------------------------*/
  1799. void CWMICommandLine::FrameCommandLineComponents(_bstr_t& bstrCommandComponent)
  1800. {
  1801. try
  1802. {
  1803. CHString strTemp;
  1804. _bstr_t bstrNodeList;
  1805. bstrCommandComponent = L"<COMMANDLINECOMPONENTS>";
  1806. FrameNodeListFragment(bstrNodeList);
  1807. bstrCommandComponent += bstrNodeList;
  1808. _TCHAR *pszVerbName = m_ParsedInfo.GetCmdSwitchesObject().
  1809. GetVerbName();
  1810. if(CompareTokens(pszVerbName, CLI_TOKEN_LIST))
  1811. {
  1812. _bstr_t bstrString = m_ParsedInfo.GetCmdSwitchesObject().
  1813. GetAliasName();
  1814. if (!bstrString)
  1815. bstrString = L"N/A";
  1816. FindAndReplaceEntityReferences(bstrString);
  1817. strTemp.Format(L"<FRIENDLYNAME>%s</FRIENDLYNAME>",
  1818. (TCHAR*)bstrString);
  1819. bstrCommandComponent += _bstr_t(strTemp);
  1820. bstrString = m_ParsedInfo.GetCmdSwitchesObject().GetAliasTarget();
  1821. if (!bstrString)
  1822. bstrString = L"N/A";
  1823. FindAndReplaceEntityReferences(bstrString);
  1824. strTemp.Format(L"<TARGET>%s</TARGET>",
  1825. (TCHAR*)bstrString);
  1826. bstrCommandComponent += _bstr_t(strTemp);
  1827. _bstr_t bstrClassName;
  1828. m_ParsedInfo.GetCmdSwitchesObject().GetClassOfAliasTarget(bstrClassName);
  1829. if (!bstrClassName)
  1830. bstrClassName = L"N/A";
  1831. bstrCommandComponent += L"<ALIASTARGET>";
  1832. bstrCommandComponent += bstrClassName;
  1833. bstrCommandComponent += L"</ALIASTARGET>";
  1834. bstrString = m_ParsedInfo.GetCmdSwitchesObject().GetPWhereExpr();
  1835. if (!bstrString)
  1836. bstrString = L"N/A";
  1837. FindAndReplaceEntityReferences(bstrString);
  1838. strTemp.Format(L"<PWHERE>%s</PWHERE>",
  1839. (TCHAR*)bstrString);
  1840. bstrCommandComponent += _bstr_t(strTemp);
  1841. bstrString = m_ParsedInfo.GetCmdSwitchesObject().GetAliasNamespace();
  1842. if (!bstrString)
  1843. bstrString = L"N/A";
  1844. FindAndReplaceEntityReferences(bstrString);
  1845. strTemp.Format(L"<NAMESPACE>%s</NAMESPACE>",
  1846. (TCHAR*)bstrString);
  1847. bstrCommandComponent += _bstr_t(strTemp);
  1848. bstrString = m_ParsedInfo.GetCmdSwitchesObject().GetAliasDesc();
  1849. if (!bstrString)
  1850. bstrString = L"N/A";
  1851. FindAndReplaceEntityReferences(bstrString);
  1852. strTemp.Format(L"<DESCRIPTION>%s</DESCRIPTION>",
  1853. (TCHAR*)bstrString);
  1854. bstrCommandComponent += _bstr_t(strTemp);
  1855. bstrString = m_ParsedInfo.GetCmdSwitchesObject().GetFormedQuery();
  1856. if (!bstrString)
  1857. bstrString = L"N/A";
  1858. FindAndReplaceEntityReferences(bstrString);
  1859. strTemp.Format(L"<RESULTANTQUERY>%s</RESULTANTQUERY>",
  1860. (TCHAR*)bstrString);
  1861. bstrCommandComponent += _bstr_t(strTemp);
  1862. _bstr_t bstrFormats;
  1863. FrameFormats(bstrFormats);
  1864. bstrCommandComponent += bstrFormats;
  1865. _bstr_t bstrProperties;
  1866. FramePropertiesInfo(bstrProperties);
  1867. bstrCommandComponent += bstrProperties;
  1868. }
  1869. bstrCommandComponent += L"</COMMANDLINECOMPONENTS>";
  1870. }
  1871. catch(_com_error& e)
  1872. {
  1873. _com_issue_error(e.Error());
  1874. }
  1875. //trap to catch CHeap_Exception
  1876. catch(CHeap_Exception)
  1877. {
  1878. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1879. }
  1880. }
  1881. /*-------------------------------------------------------------------------
  1882. Name :FrameNodeListFragment
  1883. Synopsis :Frames the XML string for NodeList info
  1884. Type :Member Function
  1885. Input parameters :None
  1886. Output parameters :
  1887. bstrContext - String to containg NodeList info in XML form
  1888. Return Type :void
  1889. Global Variables :None
  1890. Calling Syntax :FrameNodeListFragment()
  1891. -------------------------------------------------------------------------*/
  1892. void CWMICommandLine::FrameNodeListFragment(_bstr_t& bstrNodeList)
  1893. {
  1894. try
  1895. {
  1896. CHString strTemp;
  1897. CHARVECTOR::iterator itrStart,
  1898. itrEnd;
  1899. CHARVECTOR cvNodes;
  1900. _bstr_t bstrString ;
  1901. bstrNodeList = L"<NODELIST>";
  1902. cvNodes = m_ParsedInfo.GetGlblSwitchesObject().GetNodesList();
  1903. if (cvNodes.size() > 1)
  1904. {
  1905. itrStart = cvNodes.begin();
  1906. itrEnd = cvNodes.end();
  1907. // Move to next node
  1908. itrStart++;
  1909. while (itrStart != itrEnd)
  1910. {
  1911. bstrString = _bstr_t(*itrStart);
  1912. if (!bstrString)
  1913. bstrString = L"N/A";
  1914. FindAndReplaceEntityReferences(bstrString);
  1915. strTemp.Format(L"<NODE>%s</NODE>", (LPWSTR)bstrString);
  1916. bstrNodeList += _bstr_t(strTemp);
  1917. itrStart++;
  1918. }
  1919. }
  1920. else
  1921. {
  1922. bstrString = _bstr_t(m_ParsedInfo.GetGlblSwitchesObject().
  1923. GetNode());
  1924. if (!bstrString)
  1925. bstrString = L"N/A";
  1926. FindAndReplaceEntityReferences(bstrString);
  1927. strTemp.Format(L"<NODE>%s</NODE>", (LPWSTR)bstrString);
  1928. bstrNodeList += _bstr_t(strTemp);
  1929. }
  1930. bstrNodeList += L"</NODELIST>";
  1931. }
  1932. catch(_com_error& e)
  1933. {
  1934. _com_issue_error(e.Error());
  1935. }
  1936. //trap to catch CHeap_Exception
  1937. catch(CHeap_Exception)
  1938. {
  1939. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  1940. }
  1941. }
  1942. /*-------------------------------------------------------------------------
  1943. Name :FrameFormats
  1944. Synopsis :Frames the XML string for formats info
  1945. Type :Member Function
  1946. Input parameters :None
  1947. Output parameters :
  1948. bstrContext - String to containg formats info in XML form
  1949. Return Type :void
  1950. Global Variables :None
  1951. Calling Syntax :FrameFormats()
  1952. -------------------------------------------------------------------------*/
  1953. void CWMICommandLine::FrameFormats(_bstr_t& bstrFormats)
  1954. {
  1955. try
  1956. {
  1957. CHString strTemp;
  1958. XSLTDETVECTOR::iterator theIterator = NULL,
  1959. theEndIterator = NULL;
  1960. BSTRMAP::iterator theMapIterator = NULL,
  1961. theMapEndIterator = NULL;
  1962. _bstr_t bstrString;
  1963. if(!m_ParsedInfo.GetCmdSwitchesObject().GetXSLTDetailsVector().empty())
  1964. {
  1965. bstrFormats = L"<FORMATS>";
  1966. bstrFormats += L"<FORMAT>";
  1967. theIterator = m_ParsedInfo.GetCmdSwitchesObject().
  1968. GetXSLTDetailsVector().begin();
  1969. theEndIterator = m_ParsedInfo.GetCmdSwitchesObject().
  1970. GetXSLTDetailsVector().end();
  1971. while (theIterator != theEndIterator)
  1972. {
  1973. bstrString = _bstr_t((*theIterator).FileName);
  1974. FindAndReplaceEntityReferences(bstrString);
  1975. strTemp.Format(L"<NAME>%s</NAME>",
  1976. (_TCHAR*)bstrString);
  1977. bstrFormats += _bstr_t(strTemp);
  1978. theMapIterator = (*theIterator).ParamMap.begin();
  1979. theMapEndIterator = (*theIterator).ParamMap.end();
  1980. while (theMapIterator != theMapEndIterator)
  1981. {
  1982. bstrString = _bstr_t((*theMapIterator).first);
  1983. FindAndReplaceEntityReferences(bstrString);
  1984. strTemp.Format(L"<PARAM><NAME>%s</NAME>",
  1985. (_TCHAR*)bstrString);
  1986. bstrFormats += _bstr_t(strTemp);
  1987. bstrString = _bstr_t((*theMapIterator).second);
  1988. FindAndReplaceEntityReferences(bstrString);
  1989. strTemp.Format(L"<VALUE>%s</VALUE>",
  1990. (_TCHAR*)bstrString);
  1991. bstrFormats += _bstr_t(strTemp);
  1992. bstrFormats += L"</PARAM>";
  1993. theMapIterator++;
  1994. }
  1995. theIterator++;
  1996. }
  1997. bstrFormats += L"</FORMAT>";
  1998. bstrFormats += L"</FORMATS>";
  1999. }
  2000. }
  2001. catch(_com_error& e)
  2002. {
  2003. _com_issue_error(e.Error());
  2004. }
  2005. //trap to catch CHeap_Exception
  2006. catch(CHeap_Exception)
  2007. {
  2008. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2009. }
  2010. }
  2011. /*-------------------------------------------------------------------------
  2012. Name :FramePropertiesInfo
  2013. Synopsis :Frames the XML string for properties info
  2014. Type :Member Function
  2015. Input parameters :None
  2016. Output parameters :
  2017. bstrContext - String to containg properties info in XML form
  2018. Return Type :void
  2019. Global Variables :None
  2020. Calling Syntax :FramePropertiesInfo()
  2021. -------------------------------------------------------------------------*/
  2022. void CWMICommandLine::FramePropertiesInfo(_bstr_t& bstrProperties)
  2023. {
  2024. try
  2025. {
  2026. CHString strTemp;
  2027. PROPDETMAP::iterator theIterator = NULL,
  2028. theEndIterator = NULL;
  2029. if(!m_ParsedInfo.GetCmdSwitchesObject().GetPropDetMap().empty())
  2030. {
  2031. bstrProperties = L"<PROPERTIES>";
  2032. theIterator = m_ParsedInfo.GetCmdSwitchesObject().
  2033. GetPropDetMap().begin();
  2034. theEndIterator = m_ParsedInfo.GetCmdSwitchesObject().
  2035. GetPropDetMap().end();
  2036. while (theIterator != theEndIterator)
  2037. {
  2038. bstrProperties += L"<PROPERTY>";
  2039. strTemp.Format(L"<NAME>%s</NAME>",
  2040. (_TCHAR*)(*theIterator).first);
  2041. bstrProperties += _bstr_t(strTemp);
  2042. strTemp.Format(L"<DERIVATION>%s</DERIVATION>",
  2043. (_TCHAR*)(*theIterator).second.Derivation);
  2044. bstrProperties += _bstr_t(strTemp);
  2045. bstrProperties += L"</PROPERTY>";
  2046. theIterator++;
  2047. }
  2048. bstrProperties += L"</PROPERTIES>";
  2049. }
  2050. }
  2051. catch(_com_error& e)
  2052. {
  2053. _com_issue_error(e.Error());
  2054. }
  2055. //trap to catch CHeap_Exception
  2056. catch(CHeap_Exception)
  2057. {
  2058. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2059. }
  2060. }
  2061. /*-------------------------------------------------------------------------
  2062. Name :GetFileNameMap
  2063. Synopsis :Frames the BSTR Map contains the key words and
  2064. corresponding files from the XSL mapping file
  2065. Type :Member Function
  2066. Input parameters :None
  2067. Output parameters :None
  2068. Return Type :void
  2069. Global Variables :None
  2070. Calling Syntax :GetFileNameMap()
  2071. -------------------------------------------------------------------------*/
  2072. void CWMICommandLine::GetFileNameMap()
  2073. {
  2074. _TCHAR* pszFilePath = new _TCHAR[MAX_PATH];
  2075. BSTRMAP::iterator theMapIterator = NULL;
  2076. UINT nSize = 0;
  2077. try
  2078. {
  2079. _bstr_t bstrFilePath;
  2080. if(pszFilePath == NULL)
  2081. throw OUT_OF_MEMORY;
  2082. nSize = GetSystemDirectory(pszFilePath, MAX_PATH);
  2083. if(nSize)
  2084. {
  2085. if(nSize > MAX_PATH)
  2086. {
  2087. SAFEDELETE(pszFilePath);
  2088. pszFilePath = new _TCHAR[nSize + 1];
  2089. if(pszFilePath == NULL)
  2090. throw OUT_OF_MEMORY;
  2091. }
  2092. if(!GetSystemDirectory(pszFilePath, nSize))
  2093. {
  2094. SAFEDELETE(pszFilePath);
  2095. throw (::GetLastError());
  2096. }
  2097. else
  2098. {
  2099. bstrFilePath = _bstr_t(pszFilePath);
  2100. bstrFilePath += _bstr_t(WBEM_LOCATION) + _bstr_t(CLI_XSLMAPPINGS_FILE);
  2101. SAFEDELETE(pszFilePath);
  2102. }
  2103. }
  2104. else
  2105. {
  2106. SAFEDELETE(pszFilePath);
  2107. throw (::GetLastError());
  2108. }
  2109. GetXSLMappings(bstrFilePath);
  2110. //forming the BSTRMAP containing key words and filenames
  2111. if (Find(m_bmKeyWordtoFileName,CLI_TOKEN_TABLE,theMapIterator) == FALSE)
  2112. {
  2113. m_bmKeyWordtoFileName.insert(BSTRMAP::value_type(CLI_TOKEN_TABLE,
  2114. XSL_FORMAT_TABLE));
  2115. }
  2116. if (Find(m_bmKeyWordtoFileName,CLI_TOKEN_MOF,theMapIterator) == FALSE)
  2117. {
  2118. m_bmKeyWordtoFileName.insert(BSTRMAP::value_type(CLI_TOKEN_MOF,
  2119. XSL_FORMAT_MOF));
  2120. }
  2121. if (Find(m_bmKeyWordtoFileName,CLI_TOKEN_TEXTVALUE,theMapIterator) == FALSE)
  2122. {
  2123. m_bmKeyWordtoFileName.insert(BSTRMAP::value_type(CLI_TOKEN_TEXTVALUE,
  2124. XSL_FORMAT_TEXTVALUE));
  2125. }
  2126. if (Find(m_bmKeyWordtoFileName,CLI_TOKEN_LIST,theMapIterator) == FALSE)
  2127. {
  2128. m_bmKeyWordtoFileName.insert(BSTRMAP::value_type(CLI_TOKEN_LIST,
  2129. XSL_FORMAT_VALUE));
  2130. }
  2131. if (Find(m_bmKeyWordtoFileName,CLI_TOKEN_VALUE,theMapIterator) == FALSE)
  2132. {
  2133. m_bmKeyWordtoFileName.insert(BSTRMAP::value_type(CLI_TOKEN_VALUE,
  2134. XSL_FORMAT_VALUE));
  2135. }
  2136. }
  2137. catch(_com_error &e)
  2138. {
  2139. SAFEDELETE(pszFilePath);
  2140. _com_issue_error(e.Error());
  2141. }
  2142. }
  2143. /*-------------------------------------------------------------------------
  2144. Name :GetFileFromKey
  2145. Synopsis :Gets the xslfile name corrsponding to the keyword passed
  2146. from the BSTRMAP
  2147. Type :Member Function
  2148. Input parameters :bstrkeyName - key word
  2149. Output parameters :bstrFileName - the xsl filename
  2150. Return Type :BOOL
  2151. Global Variables :None
  2152. Calling Syntax :GetFileFromKey(bstrkeyName, bstrFileName)
  2153. -------------------------------------------------------------------------*/
  2154. BOOL CWMICommandLine::GetFileFromKey(_bstr_t bstrkeyName, _bstr_t& bstrFileName)
  2155. {
  2156. BOOL bFound = TRUE;
  2157. BSTRMAP::iterator theMapIterator = NULL;
  2158. if (Find(m_bmKeyWordtoFileName,bstrkeyName,theMapIterator) == TRUE)
  2159. bstrFileName = (*theMapIterator).second;
  2160. else
  2161. bFound = FALSE;
  2162. return bFound;
  2163. }
  2164. /*-------------------------------------------------------------------------
  2165. Name :GetXSLMappings
  2166. Synopsis :Get the XSL file names for keywords
  2167. Type :Member Function
  2168. Input parameters :
  2169. pszFilePath - XSL mappings file path
  2170. Output parameters :None
  2171. Return Type :void
  2172. Global Variables :None
  2173. Calling Syntax :GetXSLMappings(pszFilePath)
  2174. -------------------------------------------------------------------------*/
  2175. void CWMICommandLine::GetXSLMappings(_TCHAR *pszFilePath)
  2176. {
  2177. HRESULT hr = 0;
  2178. BOOL bContinue = TRUE;
  2179. IXMLDOMDocument *pIXMLDOMDocument = NULL;
  2180. IXMLDOMElement *pIXMLDOMElement = NULL;
  2181. IXMLDOMNode *pIXMLDOMNode = NULL;
  2182. IXMLDOMNodeList *pIXMLDOMNodeList = NULL;
  2183. IXMLDOMNamedNodeMap *pIXMLDOMNamedNodeMap = NULL;
  2184. IXMLDOMNode *pIXMLDOMNodeKeyName = NULL;
  2185. DWORD dwThreadId = GetCurrentThreadId();
  2186. BSTR bstrItemText = NULL;
  2187. VARIANT varValue, varObject;
  2188. VariantInit(&varValue);
  2189. VariantInit(&varObject);
  2190. // Get the TRACE status
  2191. BOOL bTrace = m_ParsedInfo.GetGlblSwitchesObject().GetTraceStatus();
  2192. // Get the Logging mode (VERBOSE | ERRORONLY | NOLOGGING)
  2193. ERRLOGOPT eloErrLogOpt = m_ParsedInfo.GetErrorLogObject().GetErrLogOption();
  2194. try
  2195. {
  2196. hr=CoCreateInstance(CLSID_DOMDocument, NULL,
  2197. CLSCTX_INPROC_SERVER,
  2198. IID_IXMLDOMDocument2,
  2199. (LPVOID*)&pIXMLDOMDocument);
  2200. if (bTrace || eloErrLogOpt)
  2201. {
  2202. CHString chsMsg;
  2203. chsMsg.Format(L"CoCreateInstance(CLSID_DOMDocument, "
  2204. L"NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument2,"
  2205. L" -)");
  2206. WMITRACEORERRORLOG(hr, __LINE__, __FILE__, (LPCWSTR)chsMsg,
  2207. dwThreadId, m_ParsedInfo, bTrace);
  2208. }
  2209. ONFAILTHROWERROR(hr);
  2210. VARIANT_BOOL bSuccess = VARIANT_FALSE;
  2211. VariantInit(&varObject);
  2212. varObject.vt = VT_BSTR;
  2213. varObject.bstrVal = SysAllocString(pszFilePath);
  2214. if (varObject.bstrVal == NULL)
  2215. {
  2216. //Reset the variant, it will be cleaned up by the catch...
  2217. VariantInit(&varObject);
  2218. throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  2219. }
  2220. hr = pIXMLDOMDocument->load(varObject,&bSuccess);
  2221. if(FAILED(hr) || bSuccess == VARIANT_FALSE)
  2222. {
  2223. bContinue = FALSE;
  2224. }
  2225. if (bContinue)
  2226. {
  2227. // Traverse the XML node and get the text of nodes named XSLFORMAT
  2228. // Get the document element.
  2229. hr = pIXMLDOMDocument->get_documentElement(&pIXMLDOMElement);
  2230. if (bTrace || eloErrLogOpt)
  2231. {
  2232. WMITRACEORERRORLOG(hr, __LINE__,
  2233. __FILE__, _T("IXMLDOMDocument::get_documentElement(-)"),
  2234. dwThreadId, m_ParsedInfo, bTrace);
  2235. }
  2236. ONFAILTHROWERROR(hr);
  2237. if (pIXMLDOMElement != NULL)
  2238. {
  2239. hr = pIXMLDOMElement->getElementsByTagName(
  2240. _bstr_t(L"XSLFORMAT"), &pIXMLDOMNodeList);
  2241. if (bTrace || eloErrLogOpt)
  2242. {
  2243. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2244. _T("IXMLDOMElement::getElementsByTagName"
  2245. L"(L\"XSLFORMAT\", -)"), dwThreadId, m_ParsedInfo, bTrace);
  2246. }
  2247. ONFAILTHROWERROR(hr);
  2248. LONG value = 0;
  2249. hr = pIXMLDOMNodeList->get_length(&value);
  2250. if (bTrace || eloErrLogOpt)
  2251. {
  2252. WMITRACEORERRORLOG(hr, __LINE__,
  2253. __FILE__, _T("IXMLDOMNodeList::get_length(-)"),
  2254. dwThreadId, m_ParsedInfo, bTrace);
  2255. }
  2256. ONFAILTHROWERROR(hr);
  2257. for(WMICLIINT i = 0; i < value; i++)
  2258. {
  2259. hr = pIXMLDOMNodeList->get_item(i, &pIXMLDOMNode);
  2260. if (bTrace || eloErrLogOpt)
  2261. {
  2262. WMITRACEORERRORLOG(hr, __LINE__,
  2263. __FILE__, _T("IXMLDOMNodeList::get_item(-,-)"),
  2264. dwThreadId, m_ParsedInfo, bTrace);
  2265. }
  2266. ONFAILTHROWERROR(hr);
  2267. if(pIXMLDOMNode == NULL)
  2268. continue;
  2269. hr = pIXMLDOMNode->get_text(&bstrItemText);
  2270. if (bTrace || eloErrLogOpt)
  2271. {
  2272. WMITRACEORERRORLOG(hr, __LINE__,
  2273. __FILE__, _T("IXMLDOMNode::get_text(-)"),
  2274. dwThreadId, m_ParsedInfo, bTrace);
  2275. }
  2276. ONFAILTHROWERROR(hr);
  2277. hr = pIXMLDOMNode->get_attributes(&pIXMLDOMNamedNodeMap);
  2278. if (bTrace || eloErrLogOpt)
  2279. {
  2280. WMITRACEORERRORLOG(hr, __LINE__,
  2281. __FILE__, _T("IXMLDOMNode::get_attributes(-)"),
  2282. dwThreadId, m_ParsedInfo, bTrace);
  2283. }
  2284. ONFAILTHROWERROR(hr);
  2285. if(pIXMLDOMNamedNodeMap == NULL)
  2286. {
  2287. SAFEBSTRFREE(bstrItemText);
  2288. SAFEIRELEASE(pIXMLDOMNode);
  2289. continue;
  2290. }
  2291. hr = pIXMLDOMNamedNodeMap->getNamedItem(
  2292. _bstr_t(L"KEYWORD"), &pIXMLDOMNodeKeyName);
  2293. if (bTrace || eloErrLogOpt)
  2294. {
  2295. WMITRACEORERRORLOG(hr, __LINE__, __FILE__,
  2296. _T("IXMLDOMNamedNodeMap::getNamedItem(L\"KEYWORD\", -)"),
  2297. dwThreadId, m_ParsedInfo, bTrace);
  2298. }
  2299. ONFAILTHROWERROR(hr);
  2300. if(pIXMLDOMNodeKeyName == NULL)
  2301. {
  2302. SAFEBSTRFREE(bstrItemText);
  2303. SAFEIRELEASE(pIXMLDOMNode);
  2304. SAFEIRELEASE(pIXMLDOMNamedNodeMap);
  2305. continue;
  2306. }
  2307. VariantInit(&varValue);
  2308. hr = pIXMLDOMNodeKeyName->get_nodeValue(&varValue);
  2309. if (bTrace || eloErrLogOpt)
  2310. {
  2311. WMITRACEORERRORLOG(hr, __LINE__,
  2312. __FILE__, _T("IXMLDOMNode::get_nodeValue(-)"),
  2313. dwThreadId, m_ParsedInfo, bTrace);
  2314. }
  2315. ONFAILTHROWERROR(hr);
  2316. if(varValue.vt == VT_NULL || varValue.vt == VT_EMPTY)
  2317. {
  2318. VARIANTCLEAR(varValue);
  2319. SAFEBSTRFREE(bstrItemText);
  2320. SAFEIRELEASE(pIXMLDOMNode);
  2321. SAFEIRELEASE(pIXMLDOMNamedNodeMap);
  2322. SAFEIRELEASE(pIXMLDOMNodeKeyName);
  2323. continue;
  2324. }
  2325. //forming the BSTRMAP containing key words and filenames
  2326. m_bmKeyWordtoFileName.insert(BSTRMAP::value_type(
  2327. varValue.bstrVal, bstrItemText));
  2328. VARIANTCLEAR(varValue);
  2329. SAFEBSTRFREE(bstrItemText);
  2330. SAFEIRELEASE(pIXMLDOMNode);
  2331. SAFEIRELEASE(pIXMLDOMNamedNodeMap);
  2332. SAFEIRELEASE(pIXMLDOMNodeKeyName);
  2333. }
  2334. VARIANTCLEAR(varValue);
  2335. SAFEIRELEASE(pIXMLDOMNodeList);
  2336. SAFEIRELEASE(pIXMLDOMElement);
  2337. }
  2338. SAFEIRELEASE(pIXMLDOMDocument);
  2339. }
  2340. VARIANTCLEAR(varObject);
  2341. }
  2342. catch(_com_error)
  2343. {
  2344. VARIANTCLEAR(varValue);
  2345. VARIANTCLEAR(varObject);
  2346. SAFEIRELEASE(pIXMLDOMDocument);
  2347. SAFEIRELEASE(pIXMLDOMElement);
  2348. SAFEIRELEASE(pIXMLDOMNode);
  2349. SAFEIRELEASE(pIXMLDOMNodeList);
  2350. SAFEIRELEASE(pIXMLDOMNamedNodeMap);
  2351. SAFEIRELEASE(pIXMLDOMNodeKeyName);
  2352. SAFEBSTRFREE(bstrItemText);
  2353. }
  2354. catch(CHeap_Exception)
  2355. {
  2356. VARIANTCLEAR(varValue);
  2357. VARIANTCLEAR(varObject);
  2358. SAFEIRELEASE(pIXMLDOMDocument);
  2359. SAFEIRELEASE(pIXMLDOMElement);
  2360. SAFEIRELEASE(pIXMLDOMNode);
  2361. SAFEIRELEASE(pIXMLDOMNodeList);
  2362. SAFEIRELEASE(pIXMLDOMNamedNodeMap);
  2363. SAFEIRELEASE(pIXMLDOMNodeKeyName);
  2364. SAFEBSTRFREE(bstrItemText);
  2365. _com_issue_error(WBEM_E_OUT_OF_MEMORY);
  2366. }
  2367. }