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.

2857 lines
82 KiB

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