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.

1194 lines
39 KiB

  1. /*****************************************************************************
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. ETDelete.CPP
  5. Abstract:
  6. This module is intended to have the functionality for EVENTTRIGGERS.EXE
  7. with -delete parameter.
  8. This will delete an Event Trigger From local / remote System
  9. Author:
  10. Akhil Gokhale 03-Oct.-2000 (Created it)
  11. Revision History:
  12. ******************************************************************************/
  13. #include "pch.h"
  14. #include "ETCommon.h"
  15. #include "resource.h"
  16. #include "ShowError.h"
  17. #include "ETDelete.h"
  18. #include "WMI.h"
  19. CETDelete::CETDelete()
  20. /*++
  21. Routine Description:
  22. Class constructor
  23. Arguments:
  24. None
  25. Return Value:
  26. None
  27. --*/
  28. {
  29. m_bDelete = FALSE;
  30. m_pszServerName = NULL;
  31. m_pszUserName = NULL;
  32. m_pszPassword = NULL;
  33. m_arrID = NULL;
  34. m_bIsCOMInitialize = FALSE;
  35. m_lMinMemoryReq = 0;
  36. m_bNeedPassword = FALSE;
  37. m_pWbemLocator = NULL;
  38. m_pWbemServices = NULL;
  39. m_pEnumObjects = NULL;
  40. m_pAuthIdentity = NULL;
  41. m_pClass = NULL;
  42. m_pInClass = NULL;
  43. m_pInInst = NULL;
  44. m_pOutInst = NULL;
  45. }
  46. CETDelete::CETDelete(
  47. LONG lMinMemoryReq,
  48. BOOL bNeedPassword
  49. )
  50. /*++
  51. Routine Description:
  52. Class constructor
  53. Arguments:
  54. None
  55. Return Value:
  56. None
  57. --*/
  58. {
  59. m_pszServerName = NULL;
  60. m_pszUserName = NULL;
  61. m_pszPassword = NULL;
  62. m_arrID = NULL;
  63. m_bIsCOMInitialize = FALSE;
  64. m_lMinMemoryReq = lMinMemoryReq;
  65. m_bNeedPassword = bNeedPassword;
  66. m_pWbemLocator = NULL;
  67. m_pWbemServices = NULL;
  68. m_pEnumObjects = NULL;
  69. m_pAuthIdentity = NULL;
  70. m_pClass = NULL;
  71. m_pInClass = NULL;
  72. m_pInInst = NULL;
  73. m_pOutInst = NULL;
  74. }
  75. CETDelete::~CETDelete()
  76. /*++
  77. Routine Description:
  78. Class destructor
  79. Arguments:
  80. None
  81. Return Value:
  82. None
  83. --*/
  84. {
  85. FreeMemory((LPVOID*)& m_pszServerName);
  86. FreeMemory((LPVOID*)& m_pszUserName);
  87. FreeMemory((LPVOID*)& m_pszPassword);
  88. DESTROY_ARRAY(m_arrID);
  89. SAFE_RELEASE_INTERFACE(m_pWbemLocator);
  90. SAFE_RELEASE_INTERFACE(m_pWbemServices);
  91. SAFE_RELEASE_INTERFACE(m_pEnumObjects);
  92. SAFE_RELEASE_INTERFACE(m_pClass);
  93. SAFE_RELEASE_INTERFACE(m_pInClass);
  94. SAFE_RELEASE_INTERFACE(m_pInInst);
  95. SAFE_RELEASE_INTERFACE(m_pOutInst);
  96. WbemFreeAuthIdentity(&m_pAuthIdentity);
  97. // Uninitialize COM only when it is initialized.
  98. if( TRUE == m_bIsCOMInitialize)
  99. {
  100. CoUninitialize();
  101. }
  102. }
  103. void
  104. CETDelete::Initialize()
  105. /*++
  106. Routine Description:
  107. This function allocates and initializes variables.
  108. Arguments:
  109. None
  110. Return Value:
  111. None
  112. --*/
  113. {
  114. // if at all any occurs, we know that is 'coz of the
  115. // failure in memory allocation ... so set the error
  116. DEBUG_INFO;
  117. SetLastError( ERROR_OUTOFMEMORY );
  118. SaveLastError();
  119. SecureZeroMemory(m_szTemp,sizeof(m_szTemp));
  120. m_arrID = CreateDynamicArray();
  121. if( NULL == m_arrID )
  122. {
  123. throw CShowError(E_OUTOFMEMORY);
  124. }
  125. SecureZeroMemory(cmdOptions,sizeof(TCMDPARSER2)* MAX_COMMANDLINE_D_OPTION);
  126. // initialization is successful
  127. SetLastError( NOERROR ); // clear the error
  128. SetReason( L"" ); // clear the reason
  129. DEBUG_INFO;
  130. return;
  131. }
  132. void
  133. CETDelete::PrepareCMDStruct()
  134. /*++
  135. Routine Description:
  136. This function will prepare column structure for DoParseParam Function.
  137. Arguments:
  138. none
  139. Return Value:
  140. none
  141. --*/
  142. {
  143. // Filling cmdOptions structure
  144. DEBUG_INFO;
  145. // -delete
  146. StringCopyA( cmdOptions[ ID_D_DELETE ].szSignature, "PARSER2\0", 8 );
  147. cmdOptions[ ID_D_DELETE ].dwType = CP_TYPE_BOOLEAN;
  148. cmdOptions[ ID_D_DELETE ].pwszOptions = szDeleteOption;
  149. cmdOptions[ ID_D_DELETE ].dwCount = 1;
  150. cmdOptions[ ID_D_DELETE ].dwActuals = 0;
  151. cmdOptions[ ID_D_DELETE ].dwFlags = 0;
  152. cmdOptions[ ID_D_DELETE ].pValue = &m_bDelete;
  153. cmdOptions[ ID_D_DELETE ].dwLength = 0;
  154. // -s (servername)
  155. StringCopyA( cmdOptions[ ID_D_SERVER ].szSignature, "PARSER2\0", 8 );
  156. cmdOptions[ ID_D_SERVER ].dwType = CP_TYPE_TEXT;
  157. cmdOptions[ ID_D_SERVER ].pwszOptions = szServerNameOption;
  158. cmdOptions[ ID_D_SERVER ].dwCount = 1;
  159. cmdOptions[ ID_D_SERVER ].dwActuals = 0;
  160. cmdOptions[ ID_D_SERVER ].dwFlags = CP2_ALLOCMEMORY|CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  161. cmdOptions[ ID_D_SERVER ].pValue = NULL; //m_pszServerName
  162. cmdOptions[ ID_D_SERVER ].dwLength = 0;
  163. // -u (username)
  164. StringCopyA( cmdOptions[ ID_D_USERNAME ].szSignature, "PARSER2\0", 8 );
  165. cmdOptions[ ID_D_USERNAME ].dwType = CP_TYPE_TEXT;
  166. cmdOptions[ ID_D_USERNAME ].pwszOptions = szUserNameOption;
  167. cmdOptions[ ID_D_USERNAME ].dwCount = 1;
  168. cmdOptions[ ID_D_USERNAME ].dwActuals = 0;
  169. cmdOptions[ ID_D_USERNAME ].dwFlags = CP2_ALLOCMEMORY|CP2_VALUE_TRIMINPUT|CP2_VALUE_NONULL;
  170. cmdOptions[ ID_D_USERNAME ].pValue = NULL; //m_pszUserName
  171. cmdOptions[ ID_D_USERNAME ].dwLength = 0;
  172. // -p (password)
  173. StringCopyA( cmdOptions[ ID_D_PASSWORD ].szSignature, "PARSER2\0", 8 );
  174. cmdOptions[ ID_D_PASSWORD ].dwType = CP_TYPE_TEXT;
  175. cmdOptions[ ID_D_PASSWORD ].pwszOptions = szPasswordOption;
  176. cmdOptions[ ID_D_PASSWORD ].dwCount = 1;
  177. cmdOptions[ ID_D_PASSWORD ].dwActuals = 0;
  178. cmdOptions[ ID_D_PASSWORD ].dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL;
  179. cmdOptions[ ID_D_PASSWORD ].pValue = NULL; //m_pszPassword
  180. cmdOptions[ ID_D_PASSWORD ].dwLength = 0;
  181. // -tid
  182. StringCopyA( cmdOptions[ ID_D_ID ].szSignature, "PARSER2\0", 8 );
  183. cmdOptions[ ID_D_ID ].dwType = CP_TYPE_TEXT;
  184. cmdOptions[ ID_D_ID ].pwszOptions = szTIDOption;
  185. cmdOptions[ ID_D_ID ].dwCount = 0;
  186. cmdOptions[ ID_D_ID ].dwActuals = 0;
  187. cmdOptions[ ID_D_ID ].dwFlags = CP2_MODE_ARRAY|CP2_VALUE_TRIMINPUT|
  188. CP2_VALUE_NONULL|CP_VALUE_NODUPLICATES;
  189. cmdOptions[ ID_D_ID ].pValue = &m_arrID;
  190. cmdOptions[ ID_D_ID ].dwLength = 0;
  191. DEBUG_INFO;
  192. return;
  193. }
  194. void
  195. CETDelete::ProcessOption(
  196. IN DWORD argc,
  197. IN LPCTSTR argv[]
  198. ) throw (CShowError)
  199. /*++
  200. Routine Description:
  201. This function will process/pace the command line options.
  202. NOTE: This function throws 'CShowError' type exception. Caller to this
  203. function should handle the exception.
  204. Arguments:
  205. [ in ] argc : argument(s) count specified at the command prompt
  206. [ in ] argv : argument(s) specified at the command prompt
  207. Return Value:
  208. none
  209. --*/
  210. {
  211. // local variable
  212. BOOL bReturn = TRUE;
  213. CHString szTempString;
  214. PrepareCMDStruct();
  215. DEBUG_INFO;
  216. // do the actual parsing of the command line arguments and check the result
  217. bReturn = DoParseParam2( argc, argv, ID_D_DELETE, MAX_COMMANDLINE_D_OPTION,
  218. cmdOptions, 0);
  219. // Get option values from 'cmdOptions' structure.
  220. m_pszServerName = (LPWSTR) cmdOptions[ ID_D_SERVER ].pValue;
  221. m_pszUserName = (LPWSTR) cmdOptions[ ID_D_USERNAME ].pValue;
  222. m_pszPassword = (LPWSTR) cmdOptions[ ID_D_PASSWORD ].pValue;
  223. DEBUG_INFO;
  224. if( FALSE == bReturn)
  225. {
  226. throw CShowError(MK_E_SYNTAX);
  227. }
  228. // check the remote connectivity information
  229. if ( m_pszServerName != NULL )
  230. {
  231. //
  232. // if -u is not specified, we need to allocate memory
  233. // in order to be able to retrive the current user name
  234. //
  235. // case 1: -p is not at all specified
  236. // as the value for this switch is optional, we have to rely
  237. // on the dwActuals to determine whether the switch is specified or not
  238. // in this case utility needs to try to connect first and if it fails
  239. // then prompt for the password -- in fact, we need not check for this
  240. // condition explicitly except for noting that we need to prompt for the
  241. // password
  242. //
  243. // case 2: -p is specified
  244. // but we need to check whether the value is specified or not
  245. // in this case user wants the utility to prompt for the password
  246. // before trying to connect
  247. //
  248. // case 3: -p * is specified
  249. DEBUG_INFO;
  250. // user name
  251. if ( m_pszUserName == NULL )
  252. {
  253. m_pszUserName = (LPTSTR) AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  254. if ( m_pszUserName == NULL )
  255. {
  256. SaveLastError();
  257. throw CShowError(E_OUTOFMEMORY);
  258. }
  259. }
  260. // password
  261. if ( m_pszPassword == NULL )
  262. {
  263. m_bNeedPassword = TRUE;
  264. m_pszPassword = (LPTSTR)AllocateMemory( MAX_STRING_LENGTH * sizeof( WCHAR ) );
  265. if ( m_pszPassword == NULL )
  266. {
  267. SaveLastError();
  268. throw CShowError(E_OUTOFMEMORY);
  269. }
  270. }
  271. // case 1
  272. if ( cmdOptions[ ID_D_PASSWORD ].dwActuals == 0 )
  273. {
  274. // we need not do anything special here
  275. }
  276. // case 2
  277. else if ( cmdOptions[ ID_D_PASSWORD ].pValue == NULL )
  278. {
  279. StringCopy( m_pszPassword, L"*", SIZE_OF_DYN_ARRAY(m_pszPassword));
  280. }
  281. // case 3
  282. else if ( StringCompare( m_pszPassword, L"*", TRUE, 0 ) == 0 )
  283. {
  284. if ( ReallocateMemory( (LPVOID*)&m_pszPassword,
  285. MAX_STRING_LENGTH * sizeof( WCHAR ) ) == FALSE )
  286. {
  287. SaveLastError();
  288. throw CShowError(E_OUTOFMEMORY);
  289. }
  290. // ...
  291. m_bNeedPassword = TRUE;
  292. }
  293. }
  294. DEBUG_INFO;
  295. if( 0 == cmdOptions[ ID_D_ID ].dwActuals)
  296. {
  297. throw CShowError(IDS_ID_REQUIRED);
  298. }
  299. }
  300. BOOL
  301. CETDelete::ExecuteDelete()
  302. /*++
  303. Routine Description:
  304. This routine will delete EventTriggers from WMI.
  305. Arguments:
  306. None
  307. Return Value:
  308. None
  309. --*/
  310. {
  311. // Stores functins return status.
  312. BOOL bResult = FALSE;
  313. LONG lTriggerID = 0;
  314. DEBUG_INFO;
  315. // Total Number of Event Trigger Ids...
  316. DWORD dNoOfIds = 0;
  317. DWORD dwIndx = 0;
  318. BOOL bIsValidCommandLine = TRUE;
  319. BOOL bIsWildcard = FALSE;
  320. // Used to reecive result form COM functions.
  321. HRESULT hr = S_OK;
  322. // variable used to get/set values from/to COM functions.
  323. VARIANT vVariant;
  324. BSTR bstrTemp = NULL;
  325. TCHAR szEventTriggerName[MAX_RES_STRING];
  326. // Stores Message String
  327. TCHAR szMsgString[MAX_RES_STRING*4];
  328. TCHAR szMsgFormat[MAX_RES_STRING];
  329. BOOL bIsAtLeastOne = FALSE;
  330. try
  331. {
  332. // Analyze the default argument for ID
  333. DEBUG_INFO;
  334. dNoOfIds = DynArrayGetCount( m_arrID );
  335. for(dwIndx = 0;dwIndx<dNoOfIds;dwIndx++)
  336. {
  337. StringCopy(m_szTemp,
  338. DynArrayItemAsString(m_arrID,dwIndx),SIZE_OF_ARRAY(m_szTemp));
  339. if( 0 == StringCompare(m_szTemp,ASTERIX,TRUE,0))
  340. {
  341. // Wildcard "*" cannot be clubed with other ids
  342. bIsWildcard = TRUE;
  343. if(dNoOfIds > 1)
  344. {
  345. bIsValidCommandLine = FALSE;
  346. break;
  347. }
  348. }
  349. else if( FALSE == IsNumeric(m_szTemp,10,FALSE))
  350. {
  351. // Other than "*" are not excepted
  352. throw CShowError(IDS_ID_NON_NUMERIC);
  353. }
  354. else if(( 0 == AsLong(m_szTemp,10))||
  355. (AsLong(m_szTemp,10)>ID_MAX_RANGE))
  356. {
  357. throw CShowError(IDS_INVALID_ID);
  358. }
  359. }
  360. DEBUG_INFO;
  361. InitializeCom(&m_pWbemLocator);
  362. m_bIsCOMInitialize = TRUE;
  363. // Connect Server.....
  364. // Brackets below used just to limit scope of following defined
  365. // variables.
  366. {
  367. CHString szTempUser = m_pszUserName;
  368. CHString szTempPassword = m_pszPassword;
  369. BOOL bLocalSystem = TRUE;
  370. bResult = ConnectWmiEx( m_pWbemLocator,
  371. &m_pWbemServices,
  372. m_pszServerName,
  373. szTempUser,
  374. szTempPassword,
  375. &m_pAuthIdentity,
  376. m_bNeedPassword,
  377. WMI_NAMESPACE_CIMV2,
  378. &bLocalSystem);
  379. // Password is not needed , better to free it
  380. FreeMemory((LPVOID*)& m_pszPassword);
  381. if( FALSE == bResult)
  382. {
  383. DEBUG_INFO;
  384. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  385. return FALSE;
  386. }
  387. DEBUG_INFO;
  388. // check the remote system version and its compatiblity
  389. if ( FALSE == bLocalSystem )
  390. {
  391. DWORD dwVersion = 0;
  392. dwVersion = GetTargetVersionEx( m_pWbemServices,
  393. m_pAuthIdentity );
  394. if ( dwVersion <= 5000 )// to block win2k versions
  395. {
  396. SetReason( E_REMOTE_INCOMPATIBLE );
  397. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  398. return FALSE;
  399. }
  400. // For XP systems.
  401. if( 5001 == dwVersion )
  402. {
  403. if( TRUE == DeleteXPResults( bIsWildcard, dNoOfIds ) )
  404. {
  405. // Displayed triggers present.
  406. return TRUE;
  407. }
  408. else
  409. {
  410. // Failed to display results .
  411. // Error message is already displayed.
  412. return FALSE;
  413. }
  414. }
  415. }
  416. // check the local credentials and if need display warning
  417. if ( bLocalSystem && ( 0 != StringLength(m_pszUserName,0)))
  418. {
  419. DEBUG_INFO;
  420. WMISaveError( WBEM_E_LOCAL_CREDENTIALS );
  421. ShowLastErrorEx(stderr,SLE_TYPE_WARNING|SLE_INTERNAL);
  422. }
  423. }
  424. // retrieves TriggerEventConsumer class
  425. DEBUG_INFO;
  426. bstrTemp = SysAllocString(CLS_TRIGGER_EVENT_CONSUMER);
  427. hr = m_pWbemServices->GetObject(bstrTemp,
  428. 0, NULL, &m_pClass, NULL);
  429. SAFE_RELEASE_BSTR(bstrTemp);
  430. ON_ERROR_THROW_EXCEPTION(hr);
  431. DEBUG_INFO;
  432. // Gets information about the "DeleteETrigger" method of
  433. // "TriggerEventConsumer" class
  434. bstrTemp = SysAllocString(FN_DELETE_ETRIGGER);
  435. hr = m_pClass->GetMethod(bstrTemp,
  436. 0, &m_pInClass, NULL);
  437. SAFE_RELEASE_BSTR(bstrTemp);
  438. ON_ERROR_THROW_EXCEPTION(hr);
  439. DEBUG_INFO;
  440. // create a new instance of a class "TriggerEventConsumer ".
  441. hr = m_pInClass->SpawnInstance(0, &m_pInInst);
  442. ON_ERROR_THROW_EXCEPTION(hr);
  443. DEBUG_INFO;
  444. //Following method will creates an enumerator that returns the instances of
  445. // a specified TriggerEventConsumer class
  446. bstrTemp = SysAllocString(CLS_TRIGGER_EVENT_CONSUMER);
  447. hr = m_pWbemServices->CreateInstanceEnum(bstrTemp,
  448. WBEM_FLAG_SHALLOW,
  449. NULL,
  450. &m_pEnumObjects);
  451. SAFE_RELEASE_BSTR(bstrTemp);
  452. ON_ERROR_THROW_EXCEPTION(hr);
  453. DEBUG_INFO;
  454. VariantInit(&vVariant);
  455. // set the security at the interface level also
  456. hr = SetInterfaceSecurity( m_pEnumObjects, m_pAuthIdentity );
  457. ON_ERROR_THROW_EXCEPTION(hr);
  458. DEBUG_INFO;
  459. if( TRUE == bIsWildcard) // means * is choosen
  460. {
  461. // instance of NTEventLogConsumer is cretated and now check
  462. // for available TriggerID
  463. DEBUG_INFO;
  464. while( TRUE == GiveTriggerID(&lTriggerID,szEventTriggerName))
  465. {
  466. DEBUG_INFO;
  467. hr = VariantClear(&vVariant);
  468. ON_ERROR_THROW_EXCEPTION(hr);
  469. // Set the TriggerName property .
  470. hr = PropertyPut( m_pInInst, FPR_TRIGGER_NAME,
  471. _bstr_t(szEventTriggerName));
  472. ON_ERROR_THROW_EXCEPTION(hr);
  473. DEBUG_INFO;
  474. // All The required properties sets, so
  475. // executes DeleteETrigger method to delete eventtrigger
  476. hr = m_pWbemServices->
  477. ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  478. _bstr_t(FN_DELETE_ETRIGGER),
  479. 0, NULL, m_pInInst,&m_pOutInst,NULL);
  480. ON_ERROR_THROW_EXCEPTION(hr);
  481. DEBUG_INFO;
  482. VARIANT vtValue;
  483. // initialize the variant and then get the value of the specified property
  484. VariantInit( &vtValue );
  485. hr = m_pOutInst->Get( _bstr_t( FPR_RETURN_VALUE ), 0, &vtValue, NULL, NULL );
  486. ON_ERROR_THROW_EXCEPTION( hr );
  487. //Get Output paramters.
  488. hr = vtValue.lVal;
  489. // Clear the variant variable
  490. VariantClear( &vtValue );
  491. if( FAILED(hr))
  492. {
  493. if ( !(( ERROR_TRIGGER_NOT_FOUND == hr) ||
  494. ( ERROR_TRIGGER_NOT_DELETED == hr) ||
  495. ( ERROR_TRIGGER_NOT_FOUND == hr)))
  496. {
  497. ON_ERROR_THROW_EXCEPTION(hr);
  498. }
  499. }
  500. DEBUG_INFO;
  501. if(SUCCEEDED(hr)) // Means deletion is successful......
  502. {
  503. DEBUG_INFO;
  504. StringCopy( szMsgFormat, GetResString(IDS_DELETE_SUCCESS),
  505. SIZE_OF_ARRAY(szMsgFormat));
  506. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  507. szMsgFormat, _X(szEventTriggerName), lTriggerID);
  508. ShowMessage(stdout,szMsgString);
  509. bIsAtLeastOne = TRUE;
  510. }
  511. // Means unable to delete trigger due to some problem like someone
  512. // renamed Schedule task name etc.
  513. else if ( ERROR_TRIGGER_NOT_DELETED == hr)
  514. {
  515. // This error will come if logged on user has no
  516. // right on attached schedule task.
  517. continue;
  518. }
  519. // This error will come only if multiple instances are running.
  520. // This is due to sending a non existance Trigger Name.
  521. else if (ERROR_TRIGGER_NOT_FOUND == hr)
  522. {
  523. DEBUG_INFO;
  524. // Just ignore this error.
  525. continue;
  526. }
  527. else
  528. {
  529. DEBUG_INFO;
  530. ON_ERROR_THROW_EXCEPTION(hr);
  531. }
  532. } // End of while loop
  533. if( FALSE == bIsAtLeastOne)
  534. {
  535. DEBUG_INFO;
  536. ShowMessage(stdout,GetResString(IDS_NO_EVENTID));
  537. }
  538. else
  539. {
  540. // Display one balnk line.
  541. ShowMessage(stdout,L"\n");
  542. }
  543. } // end of if
  544. else // Idividual trigger is specified
  545. {
  546. DEBUG_INFO;
  547. bIsAtLeastOne = FALSE;
  548. for(dwIndx=0;dwIndx<dNoOfIds;dwIndx++)
  549. {
  550. lTriggerID = AsLong(DynArrayItemAsString(m_arrID,dwIndx),10);
  551. DEBUG_INFO;
  552. if( TRUE == GiveTriggerName(lTriggerID,szEventTriggerName))
  553. {
  554. DEBUG_INFO;
  555. hr = VariantClear(&vVariant);
  556. ON_ERROR_THROW_EXCEPTION(hr);
  557. // Set the TriggerName property .
  558. hr = PropertyPut( m_pInInst, FPR_TRIGGER_NAME,
  559. _bstr_t(szEventTriggerName));
  560. ON_ERROR_THROW_EXCEPTION(hr);
  561. DEBUG_INFO;
  562. // All The required properties sets, so
  563. // executes DeleteETrigger method to delete eventtrigger
  564. hr = m_pWbemServices->
  565. ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  566. _bstr_t(FN_DELETE_ETRIGGER),
  567. 0, NULL, m_pInInst, &m_pOutInst, NULL);
  568. ON_ERROR_THROW_EXCEPTION(hr);
  569. DEBUG_INFO;
  570. VARIANT vtValue;
  571. // initialize the variant and then get the value of the specified property
  572. VariantInit( &vtValue );
  573. hr = m_pOutInst->Get( _bstr_t( FPR_RETURN_VALUE ), 0, &vtValue, NULL, NULL );
  574. ON_ERROR_THROW_EXCEPTION( hr );
  575. //Get Output paramters.
  576. hr = vtValue.lVal;
  577. // Clear the variant variable
  578. VariantClear( &vtValue );
  579. if( FAILED(hr))
  580. {
  581. if ( !(( ERROR_TRIGGER_NOT_FOUND == hr) ||
  582. ( ERROR_TRIGGER_NOT_DELETED == hr) ||
  583. ( ERROR_TRIGGER_NOT_FOUND == hr)))
  584. {
  585. ON_ERROR_THROW_EXCEPTION(hr);
  586. }
  587. }
  588. if( SUCCEEDED(hr)) // Means deletion is successful......
  589. {
  590. DEBUG_INFO;
  591. bIsAtLeastOne = TRUE;
  592. StringCopy( szMsgFormat, GetResString(IDS_DELETE_SUCCESS),
  593. SIZE_OF_ARRAY(szMsgFormat));
  594. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  595. szMsgFormat, _X(szEventTriggerName), lTriggerID);
  596. ShowMessage(stdout,szMsgString);
  597. }
  598. // Provider sends this if if failed to delete eventrigger of
  599. // given ID.
  600. else if (ERROR_TRIGGER_NOT_FOUND == hr)
  601. {
  602. DEBUG_INFO;
  603. bIsAtLeastOne = TRUE;
  604. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  605. SIZE_OF_ARRAY(szMsgFormat));
  606. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  607. szMsgFormat, lTriggerID);
  608. // Message shown on screen will be...
  609. // FAILURE: "EventID" is not a Valid Event ID
  610. ShowMessage(stdout,szMsgString);
  611. }
  612. // Means unable to delete trigger due to some problem like
  613. // someone renamed Schedule task name etc.
  614. else if ( ERROR_TRIGGER_NOT_DELETED == hr)
  615. {
  616. DEBUG_INFO;
  617. StringCopy( szMsgFormat, GetResString(IDS_UNABLE_DELETE) ,
  618. SIZE_OF_ARRAY(szMsgFormat));
  619. StringCchPrintfW(szMsgString, SIZE_OF_ARRAY(szMsgString),
  620. szMsgFormat,lTriggerID);
  621. // Message shown on screen will be...
  622. // Info: Unable to delete event trigger id "EventID".
  623. ShowMessage( stdout, szMsgString);
  624. }
  625. else
  626. {
  627. DEBUG_INFO;
  628. ON_ERROR_THROW_EXCEPTION(hr);
  629. }
  630. } // End if
  631. else
  632. {
  633. DEBUG_INFO;
  634. bIsAtLeastOne = TRUE;
  635. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  636. SIZE_OF_ARRAY(szMsgFormat));
  637. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  638. szMsgFormat,lTriggerID);
  639. // Message shown on screen will be...
  640. // FAILURE: "EventID" is not a Valid Event ID
  641. ShowMessage(stdout,szMsgString);
  642. }
  643. }// End for
  644. if (TRUE == bIsAtLeastOne)
  645. {
  646. ShowMessage(stdout,L"\n");
  647. }
  648. } // End else
  649. }
  650. catch(_com_error)
  651. {
  652. DEBUG_INFO;
  653. if( 0x80041002 == hr )// WMI returns string for this hr value is
  654. // "Not Found." which is not user friendly. So
  655. // changing the message text.
  656. {
  657. ShowMessage( stderr,GetResString(IDS_CLASS_NOT_REG));
  658. }
  659. else
  660. {
  661. DEBUG_INFO;
  662. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  663. }
  664. DEBUG_INFO;
  665. return FALSE;
  666. }
  667. DEBUG_INFO;
  668. return TRUE;
  669. }
  670. BOOL
  671. CETDelete::GiveTriggerName(
  672. IN LONG lTriggerID,
  673. OUT LPTSTR pszTriggerName
  674. )
  675. /*++
  676. Routine Descripton:
  677. This function Will return Event Trigger Name for lTriggerID
  678. Arguments:
  679. [in] lTriggerID : Will Have Event Trigger ID
  680. [out] pszTriggerName : Will return Event Trigger Name
  681. Return Value:
  682. TRUE - if Successfully Gets EventTrigger ID and Event Trigger Name
  683. FALSE - if ERROR
  684. --*/
  685. {
  686. BOOL bReturn = TRUE; // holds status of return value of this function
  687. LONG lTriggerID1; // Holds trigger id
  688. IWbemClassObject *pObj1 = NULL;
  689. ULONG uReturned1 = 0;
  690. HRESULT hRes = S_OK; // used to reecive result form COM functions
  691. BOOL bAlwaysTrue = TRUE;
  692. DEBUG_INFO;
  693. // Resets it as It may be previouly pointing to other than first instance
  694. m_pEnumObjects->Reset();
  695. while(bAlwaysTrue)
  696. {
  697. hRes = m_pEnumObjects->Next(0,1,&pObj1,&uReturned1);
  698. if(FAILED(hRes))
  699. {
  700. DEBUG_INFO;
  701. ON_ERROR_THROW_EXCEPTION( hRes );
  702. break;
  703. }
  704. DEBUG_INFO;
  705. if( 0 == uReturned1 )
  706. {
  707. SAFE_RELEASE_INTERFACE(pObj1);
  708. bReturn = FALSE;
  709. return bReturn;
  710. }
  711. // Get Trigger ID
  712. hRes = PropertyGet1(pObj1,FPR_TRIGGER_ID,&lTriggerID1,sizeof(LONG));
  713. if(FAILED(hRes))
  714. {
  715. DEBUG_INFO;
  716. SAFE_RELEASE_INTERFACE(pObj1);
  717. ON_ERROR_THROW_EXCEPTION( hRes );
  718. bReturn = FALSE;
  719. return bReturn;
  720. }
  721. DEBUG_INFO;
  722. if(lTriggerID == lTriggerID1)
  723. {
  724. DEBUG_INFO;
  725. // Get Trigger Name
  726. hRes = PropertyGet1(pObj1,FPR_TRIGGER_NAME,pszTriggerName,
  727. MAX_RES_STRING);
  728. if(FAILED(hRes))
  729. {
  730. DEBUG_INFO;
  731. SAFE_RELEASE_INTERFACE(pObj1);
  732. ON_ERROR_THROW_EXCEPTION( hRes );
  733. bReturn = FALSE;
  734. return bReturn;
  735. }
  736. DEBUG_INFO;
  737. bReturn = TRUE;
  738. break;
  739. }
  740. }
  741. SAFE_RELEASE_INTERFACE(pObj1);
  742. DEBUG_INFO;
  743. return bReturn;
  744. }
  745. BOOL
  746. CETDelete::GiveTriggerID(
  747. OUT LONG *pTriggerID,
  748. OUT LPTSTR pszTriggerName
  749. )
  750. /*++
  751. Routine Description:
  752. This function Will return Trigger Id and Trigger Name of class pointed
  753. by IEnumWbemClassObject pointer
  754. Arguments:
  755. [out] pTriggerID : Will return Event Trigger ID
  756. [out] pszTriggerName : Will return Event Trigger Name
  757. Return Value:
  758. TRUE - if Successfully Gets EventTrigger ID and Event Trigger Name
  759. FALSE - if ERROR
  760. --*/
  761. {
  762. BOOL bReturn = TRUE; // status of return value of this function
  763. IWbemClassObject *pObj1 = NULL;
  764. ULONG uReturned1 = 0;
  765. DEBUG_INFO;
  766. HRESULT hRes = m_pEnumObjects->Next(0,1,&pObj1,&uReturned1);
  767. if(FAILED(hRes))
  768. {
  769. DEBUG_INFO;
  770. ON_ERROR_THROW_EXCEPTION( hRes );
  771. bReturn = FALSE;
  772. return bReturn;
  773. }
  774. if( 0 == uReturned1)
  775. {
  776. DEBUG_INFO;
  777. SAFE_RELEASE_INTERFACE(pObj1);
  778. bReturn = FALSE;
  779. return bReturn;
  780. }
  781. DEBUG_INFO;
  782. // Get Trigger ID
  783. hRes = PropertyGet1(pObj1,FPR_TRIGGER_ID,pTriggerID,sizeof(LONG));
  784. if(FAILED(hRes))
  785. {
  786. DEBUG_INFO;
  787. SAFE_RELEASE_INTERFACE(pObj1);
  788. ON_ERROR_THROW_EXCEPTION( hRes );
  789. bReturn = FALSE;
  790. return bReturn;
  791. }
  792. // Get Trigger Name
  793. hRes = PropertyGet1( pObj1, FPR_TRIGGER_NAME, pszTriggerName,
  794. MAX_RES_STRING);
  795. if(FAILED(hRes))
  796. {
  797. DEBUG_INFO;
  798. SAFE_RELEASE_INTERFACE(pObj1);
  799. ON_ERROR_THROW_EXCEPTION( hRes );
  800. bReturn = FALSE;
  801. return bReturn;
  802. }
  803. DEBUG_INFO;
  804. SAFE_RELEASE_INTERFACE(pObj1);
  805. return bReturn;
  806. }
  807. BOOL
  808. CETDelete::DeleteXPResults(
  809. IN BOOL bIsWildcard,
  810. IN DWORD dNoOfIds
  811. )
  812. /*++
  813. Routine Description:
  814. This function deletes the triggers present on a remote XP machine.
  815. This function is for compatibility of .NET ot XP machine only.
  816. Arguments:
  817. [in] bIsWildCard - If TRUE then all triggers needs to be deleted.
  818. [in] dNoOfIds - Contains the number of triggers present.
  819. Return Value:
  820. BOOL: TRUE - If succedded in deleting results.
  821. FALSE- otherwise
  822. --*/
  823. {
  824. HRESULT hr = S_OK;
  825. VARIANT vVariant;
  826. LONG lTriggerID = 0;
  827. TCHAR szEventTriggerName[MAX_RES_STRING];
  828. TCHAR szMsgFormat[MAX_RES_STRING];
  829. TCHAR szMsgString[MAX_RES_STRING*4];
  830. BOOL bIsAtLeastOne = FALSE;
  831. try
  832. {
  833. // retrieves TriggerEventConsumer class
  834. DEBUG_INFO;
  835. hr = m_pWbemServices->GetObject(_bstr_t( CLS_TRIGGER_EVENT_CONSUMER ),
  836. 0, NULL, &m_pClass, NULL);
  837. ON_ERROR_THROW_EXCEPTION(hr);
  838. DEBUG_INFO;
  839. // Gets information about the "DeleteETrigger" method of
  840. // "TriggerEventConsumer" class
  841. hr = m_pClass->GetMethod(_bstr_t( FN_DELETE_ETRIGGER_XP ),
  842. 0, &m_pInClass, NULL);
  843. ON_ERROR_THROW_EXCEPTION(hr);
  844. DEBUG_INFO;
  845. // create a new instance of a class "TriggerEventConsumer ".
  846. hr = m_pInClass->SpawnInstance(0, &m_pInInst);
  847. ON_ERROR_THROW_EXCEPTION(hr);
  848. DEBUG_INFO;
  849. //Following method will creates an enumerator that returns the instances of
  850. // a specified TriggerEventConsumer class
  851. hr = m_pWbemServices->CreateInstanceEnum(_bstr_t( CLS_TRIGGER_EVENT_CONSUMER ),
  852. WBEM_FLAG_SHALLOW,
  853. NULL,
  854. &m_pEnumObjects);
  855. ON_ERROR_THROW_EXCEPTION(hr);
  856. DEBUG_INFO;
  857. VariantInit(&vVariant);
  858. // set the security at the interface level also
  859. hr = SetInterfaceSecurity( m_pEnumObjects, m_pAuthIdentity );
  860. ON_ERROR_THROW_EXCEPTION(hr);
  861. DEBUG_INFO;
  862. if( TRUE == bIsWildcard) // means * is choosen
  863. {
  864. // instance of NTEventLogConsumer is cretated and now check
  865. // for available TriggerID
  866. DEBUG_INFO;
  867. while( TRUE == GiveTriggerID(&lTriggerID,szEventTriggerName))
  868. {
  869. DEBUG_INFO;
  870. VariantClear(&vVariant);
  871. // Set the TriggerName property .
  872. hr = PropertyPut( m_pInInst, FPR_TRIGGER_NAME,
  873. _bstr_t(szEventTriggerName));
  874. ON_ERROR_THROW_EXCEPTION(hr);
  875. DEBUG_INFO;
  876. // All The required properties sets, so
  877. // executes DeleteETrigger method to delete eventtrigger
  878. hr = m_pWbemServices->
  879. ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  880. _bstr_t(FN_DELETE_ETRIGGER_XP),
  881. 0, NULL, m_pInInst,&m_pOutInst,NULL);
  882. ON_ERROR_THROW_EXCEPTION(hr);
  883. DEBUG_INFO;
  884. // Get Return Value from DeleteETrigger function
  885. DWORD dwTemp;
  886. if( FALSE == PropertyGet(m_pOutInst,FPR_RETURN_VALUE,dwTemp))
  887. {
  888. return FALSE;
  889. }
  890. DEBUG_INFO;
  891. switch( (LONG)dwTemp )
  892. {
  893. case 0: // Means deletion is successful......
  894. DEBUG_INFO;
  895. bIsAtLeastOne = TRUE;
  896. StringCopy( szMsgFormat, GetResString(IDS_DELETE_SUCCESS),
  897. SIZE_OF_ARRAY(szMsgFormat));
  898. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  899. szMsgFormat, _X(szEventTriggerName), lTriggerID);
  900. ShowMessage(stdout,szMsgString);
  901. break;
  902. case 1: // Provider returns if failed to delete eventrigger of given ID.
  903. DEBUG_INFO;
  904. bIsAtLeastOne = TRUE;
  905. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  906. SIZE_OF_ARRAY(szMsgFormat));
  907. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  908. szMsgFormat, lTriggerID);
  909. // Message shown on screen will be...
  910. // FAILURE: "EventID" is not a Valid Event ID
  911. ShowMessage(stdout,szMsgString);
  912. break;
  913. default:
  914. DEBUG_INFO;
  915. bIsAtLeastOne = TRUE;
  916. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  917. SIZE_OF_ARRAY(szMsgFormat));
  918. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  919. szMsgFormat,lTriggerID);
  920. // Message shown on screen will be...
  921. // FAILURE: "EventID" is not a Valid Event ID
  922. ShowMessage(stderr,szMsgString);
  923. break;
  924. }
  925. } // End of while loop
  926. if( FALSE == bIsAtLeastOne)
  927. {
  928. DEBUG_INFO;
  929. ShowMessage(stdout,GetResString(IDS_NO_EVENTID));
  930. }
  931. else
  932. {
  933. // Display one balnk line.
  934. ShowMessage(stdout,L"\n");
  935. }
  936. } // end of if
  937. else
  938. {
  939. DEBUG_INFO;
  940. for( DWORD dwIndx=0;dwIndx<dNoOfIds;dwIndx++)
  941. {
  942. lTriggerID = AsLong(DynArrayItemAsString(m_arrID,dwIndx),10);
  943. DEBUG_INFO;
  944. if( TRUE == GiveTriggerName(lTriggerID,szEventTriggerName))
  945. {
  946. DEBUG_INFO;
  947. hr = VariantClear(&vVariant);
  948. ON_ERROR_THROW_EXCEPTION(hr);
  949. // Set the TriggerName property .
  950. hr = PropertyPut( m_pInInst, FPR_TRIGGER_NAME,
  951. _bstr_t(szEventTriggerName));
  952. ON_ERROR_THROW_EXCEPTION(hr);
  953. DEBUG_INFO;
  954. // All The required properties sets, so
  955. // executes DeleteETrigger method to delete eventtrigger
  956. hr = m_pWbemServices->
  957. ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  958. _bstr_t(FN_DELETE_ETRIGGER_XP),
  959. 0, NULL, m_pInInst, &m_pOutInst, NULL);
  960. ON_ERROR_THROW_EXCEPTION(hr);
  961. DEBUG_INFO;
  962. // Get Return Value from DeleteETrigger function
  963. DWORD dwTemp;
  964. if( FALSE == PropertyGet(m_pOutInst,FPR_RETURN_VALUE,dwTemp))
  965. {
  966. return FALSE;
  967. }
  968. switch( (LONG)dwTemp )
  969. {
  970. case 0: // Means deletion is successful......
  971. DEBUG_INFO;
  972. StringCopy( szMsgFormat, GetResString(IDS_DELETE_SUCCESS),
  973. SIZE_OF_ARRAY(szMsgFormat));
  974. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  975. szMsgFormat, _X(szEventTriggerName), lTriggerID);
  976. ShowMessage(stdout,szMsgString);
  977. break;
  978. case 1: // Provider returns if failed to delete eventrigger of given ID.
  979. DEBUG_INFO;
  980. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  981. SIZE_OF_ARRAY(szMsgFormat));
  982. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  983. szMsgFormat, lTriggerID);
  984. // Message shown on screen will be...
  985. // FAILURE: "EventID" is not a Valid Event ID
  986. ShowMessage(stdout,szMsgString);
  987. break;
  988. default:
  989. DEBUG_INFO;
  990. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  991. SIZE_OF_ARRAY(szMsgFormat));
  992. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  993. szMsgFormat,lTriggerID);
  994. // Message shown on screen will be...
  995. // FAILURE: "EventID" is not a Valid Event ID
  996. ShowMessage(stderr,szMsgString);
  997. break;
  998. }
  999. } // End if
  1000. else
  1001. {
  1002. DEBUG_INFO;
  1003. StringCopy( szMsgFormat, GetResString(IDS_DELETE_ERROR),
  1004. SIZE_OF_ARRAY(szMsgFormat));
  1005. StringCchPrintfW( szMsgString, SIZE_OF_ARRAY(szMsgString),
  1006. szMsgFormat,lTriggerID);
  1007. // Message shown on screen will be...
  1008. // FAILURE: "EventID" is not a Valid Event ID
  1009. ShowMessage(stderr,szMsgString);
  1010. }
  1011. }// End for
  1012. // Display one balnk line.
  1013. ShowMessage(stdout,L"\n");
  1014. } // End else
  1015. }
  1016. catch( _com_error e )
  1017. {
  1018. DEBUG_INFO;
  1019. // WMI returns string for this hr value is "Not Found." which is not
  1020. // user friendly. So changing the message text.
  1021. if( 0x80041002 == hr )
  1022. {
  1023. ShowMessage( stderr,GetResString(IDS_CLASS_NOT_REG));
  1024. }
  1025. else
  1026. {
  1027. DEBUG_INFO;
  1028. WMISaveError( e );
  1029. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  1030. }
  1031. return FALSE;
  1032. }
  1033. catch( CHeap_Exception )
  1034. {
  1035. WMISaveError( WBEM_E_OUT_OF_MEMORY );
  1036. ShowLastErrorEx(stderr,SLE_TYPE_ERROR|SLE_INTERNAL);
  1037. return FALSE;
  1038. }
  1039. // Operation successful.
  1040. return TRUE;
  1041. }