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

853 lines
29 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. //***************************************************************************
  20. // Routine Description:
  21. // Class constructor
  22. //
  23. // Arguments:
  24. // None
  25. // Return Value:
  26. // None
  27. //
  28. //***************************************************************************
  29. CETDelete::CETDelete()
  30. {
  31. m_bDelete = FALSE;
  32. m_pszServerName = NULL;
  33. m_pszUserName = NULL;
  34. m_pszPassword = NULL;
  35. m_arrID = NULL;
  36. m_bIsCOMInitialize = FALSE;
  37. m_lMinMemoryReq = 0;
  38. m_bNeedPassword = FALSE;
  39. m_pszTemp = NULL;
  40. m_pWbemLocator = NULL;
  41. m_pWbemServices = NULL;
  42. m_pEnumObjects = NULL;
  43. m_pAuthIdentity = NULL;
  44. m_pClass = NULL;
  45. m_pInClass = NULL;
  46. m_pInInst = NULL;
  47. m_pOutInst = NULL;
  48. m_hStdHandle = NULL;
  49. }
  50. //***************************************************************************
  51. // Routine Description:
  52. // Class constructor
  53. //
  54. // Arguments:
  55. // None
  56. // Return Value:
  57. // None
  58. //
  59. //***************************************************************************
  60. CETDelete::CETDelete(LONG lMinMemoryReq,BOOL bNeedPassword)
  61. {
  62. m_pszServerName = NULL;
  63. m_pszUserName = NULL;
  64. m_pszPassword = NULL;
  65. m_arrID = NULL;
  66. m_bIsCOMInitialize = FALSE;
  67. m_lMinMemoryReq = lMinMemoryReq;
  68. m_bNeedPassword = bNeedPassword;
  69. m_pszTemp = NULL;
  70. m_pWbemLocator = NULL;
  71. m_pWbemServices = NULL;
  72. m_pEnumObjects = NULL;
  73. m_pAuthIdentity = NULL;
  74. m_pClass = NULL;
  75. m_pInClass = NULL;
  76. m_pInInst = NULL;
  77. m_pOutInst = NULL;
  78. m_hStdHandle = NULL;
  79. }
  80. //***************************************************************************
  81. // Routine Description:
  82. // Class destructor
  83. //
  84. // Arguments:
  85. // None
  86. // Return Value:
  87. // None
  88. //
  89. //***************************************************************************
  90. CETDelete::~CETDelete()
  91. {
  92. RELEASE_MEMORY_EX(m_pszServerName);
  93. RELEASE_MEMORY_EX(m_pszUserName);
  94. RELEASE_MEMORY_EX(m_pszPassword);
  95. RELEASE_MEMORY_EX(m_pszTemp);
  96. DESTROY_ARRAY(m_arrID);
  97. SAFE_RELEASE_INTERFACE(m_pWbemLocator);
  98. SAFE_RELEASE_INTERFACE(m_pWbemServices);
  99. SAFE_RELEASE_INTERFACE(m_pEnumObjects);
  100. SAFE_RELEASE_INTERFACE(m_pClass);
  101. SAFE_RELEASE_INTERFACE(m_pInClass);
  102. SAFE_RELEASE_INTERFACE(m_pInInst);
  103. SAFE_RELEASE_INTERFACE(m_pOutInst);
  104. // Uninitialize COM only when it is initialized.
  105. if(m_bIsCOMInitialize == TRUE)
  106. {
  107. CoUninitialize();
  108. }
  109. }
  110. //***************************************************************************
  111. // Routine Description:
  112. // This function allocates and initializes variables.
  113. //
  114. // Arguments:
  115. // None
  116. // Return Value:
  117. // None
  118. //
  119. //***************************************************************************
  120. void
  121. CETDelete::Initialize()
  122. {
  123. // local variable
  124. LONG lTemp = 0;
  125. // if at all any occurs, we know that is 'coz of the
  126. // failure in memory allocation ... so set the error
  127. SetLastError( E_OUTOFMEMORY );
  128. SaveLastError();
  129. // allocate memory at least MAX_COMPUTER_NAME_LENGTH+1 (its windows )
  130. // constant
  131. lTemp = (m_lMinMemoryReq>MAX_COMPUTERNAME_LENGTH)?
  132. m_lMinMemoryReq:MAX_COMPUTERNAME_LENGTH+1;
  133. m_pszServerName = new TCHAR[lTemp+1];
  134. CheckAndSetMemoryAllocation (m_pszServerName,lTemp);
  135. // allocate memory at least MAX_USERNAME_LENGTH+1 (its windows )
  136. // constant
  137. lTemp = (m_lMinMemoryReq>MAX_USERNAME_LENGTH)?
  138. m_lMinMemoryReq:MAX_USERNAME_LENGTH+1;
  139. m_pszUserName = new TCHAR[lTemp+1];
  140. CheckAndSetMemoryAllocation (m_pszUserName,lTemp);
  141. // allocate memory at least MAX_PASSWORD_LENGTH+1 (its windows )
  142. // constant
  143. lTemp = (m_lMinMemoryReq>MAX_PASSWORD_LENGTH)?
  144. m_lMinMemoryReq:MAX_PASSWORD_LENGTH+1;
  145. m_pszPassword = new TCHAR[lTemp+1];
  146. CheckAndSetMemoryAllocation (m_pszPassword,lTemp);
  147. m_pszTemp = new TCHAR[MAX_RES_STRING+1];
  148. CheckAndSetMemoryAllocation (m_pszTemp,MAX_RES_STRING);
  149. m_arrID = CreateDynamicArray();
  150. if(m_arrID == NULL)
  151. {
  152. throw CShowError(E_OUTOFMEMORY);
  153. }
  154. // initialization is successful
  155. SetLastError( NOERROR ); // clear the error
  156. SetReason( NULL_STRING ); // clear the reason
  157. return;
  158. }
  159. // ***************************************************************************
  160. // Routine Description:
  161. // Function will allocate memory to a string
  162. //
  163. // Arguments:
  164. // [in][out] pszStr : String variable to which memory to be allocated
  165. // [in] : Number of bytes to be allocated.
  166. // Return Value:
  167. // NONE
  168. //
  169. //***************************************************************************
  170. void
  171. CETDelete::CheckAndSetMemoryAllocation(LPTSTR pszStr, LONG lSize)
  172. {
  173. if(pszStr == NULL)
  174. {
  175. throw CShowError(E_OUTOFMEMORY);
  176. }
  177. // init to ZERO's
  178. ZeroMemory( pszStr, lSize * sizeof( TCHAR ) );
  179. return;
  180. }
  181. // ***************************************************************************
  182. // Routine Description:
  183. // This function will prepare column structure for DoParseParam Function.
  184. //
  185. // Arguments:
  186. // none
  187. // Return Value:
  188. // none
  189. // ***************************************************************************
  190. void
  191. CETDelete::PrepareCMDStruct()
  192. {
  193. // Filling cmdOptions structure
  194. // -delete
  195. lstrcpy(cmdOptions[ ID_D_DELETE ].szOption,OPTION_DELETE);
  196. cmdOptions[ ID_D_DELETE ].dwFlags = CP_MAIN_OPTION;
  197. cmdOptions[ ID_D_DELETE ].dwCount = 1;
  198. cmdOptions[ ID_D_DELETE ].dwActuals = 0;
  199. cmdOptions[ ID_D_DELETE ].pValue = &m_bDelete;
  200. lstrcpy(cmdOptions[ ID_D_DELETE ].szValues,NULL_STRING);
  201. cmdOptions[ ID_D_DELETE ].pFunction = NULL;
  202. cmdOptions[ ID_D_DELETE ].pFunctionData = NULL;
  203. cmdOptions[ ID_D_DELETE ].pFunctionData = NULL;
  204. // -s (servername)
  205. lstrcpy(cmdOptions[ ID_D_SERVER ].szOption,OPTION_SERVER);
  206. cmdOptions[ ID_D_SERVER ].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  207. cmdOptions[ ID_D_SERVER ].dwCount = 1;
  208. cmdOptions[ ID_D_SERVER ].dwActuals = 0;
  209. cmdOptions[ ID_D_SERVER ].pValue = m_pszServerName;
  210. lstrcpy(cmdOptions[ ID_D_SERVER ].szValues,NULL_STRING);
  211. cmdOptions[ ID_D_SERVER ].pFunction = NULL;
  212. cmdOptions[ ID_D_SERVER ].pFunctionData = NULL;
  213. // -u (username)
  214. lstrcpy(cmdOptions[ ID_D_USERNAME ].szOption,OPTION_USERNAME);
  215. cmdOptions[ ID_D_USERNAME ].dwFlags = CP_TYPE_TEXT|CP_VALUE_MANDATORY;
  216. cmdOptions[ ID_D_USERNAME ].dwCount = 1;
  217. cmdOptions[ ID_D_USERNAME ].dwActuals = 0;
  218. cmdOptions[ ID_D_USERNAME ].pValue = m_pszUserName;
  219. lstrcpy(cmdOptions[ ID_D_USERNAME ].szValues,NULL_STRING);
  220. cmdOptions[ ID_D_USERNAME ].pFunction = NULL;
  221. cmdOptions[ ID_D_USERNAME ].pFunctionData = NULL;
  222. // -p (password)
  223. lstrcpy(cmdOptions[ ID_D_PASSWORD ].szOption,OPTION_PASSWORD);
  224. cmdOptions[ ID_D_PASSWORD ].dwFlags = CP_TYPE_TEXT|CP_VALUE_OPTIONAL;
  225. cmdOptions[ ID_D_PASSWORD ].dwCount = 1;
  226. cmdOptions[ ID_D_PASSWORD ].dwActuals = 0;
  227. cmdOptions[ ID_D_PASSWORD ].pValue = m_pszPassword;
  228. lstrcpy(cmdOptions[ ID_D_PASSWORD ].szValues,NULL_STRING);
  229. cmdOptions[ ID_D_PASSWORD ].pFunction = NULL;
  230. cmdOptions[ ID_D_PASSWORD ].pFunctionData = NULL;
  231. // -tid
  232. lstrcpy(cmdOptions[ ID_D_ID ].szOption,OPTION_TID);
  233. cmdOptions[ ID_D_ID ].dwFlags = CP_TYPE_TEXT|CP_MODE_ARRAY|
  234. CP_VALUE_NODUPLICATES|
  235. CP_VALUE_MANDATORY|CP_MANDATORY;
  236. cmdOptions[ ID_D_ID ].dwCount = 0;
  237. cmdOptions[ ID_D_ID ].dwActuals = 0;
  238. cmdOptions[ ID_D_ID ].pValue = &m_arrID;
  239. lstrcpy(cmdOptions[ ID_D_ID ].szValues,NULL_STRING);
  240. cmdOptions[ ID_D_ID ].pFunction = NULL;
  241. cmdOptions[ ID_D_ID ].pFunctionData = NULL;
  242. }
  243. // ***************************************************************************
  244. // Routine Description:
  245. // This function will process/parce the command line options.
  246. //
  247. // Arguments:
  248. // [ in ] argc : argument(s) count specified at the command prompt
  249. // [ in ] argv : argument(s) specified at the command prompt
  250. //
  251. // Return Value:
  252. // none
  253. // ***************************************************************************
  254. void
  255. CETDelete::ProcessOption(DWORD argc, LPCTSTR argv[])
  256. {
  257. // local variable
  258. BOOL bReturn = TRUE;
  259. CHString szTempString;
  260. PrepareCMDStruct();
  261. // init the password variable with '*'
  262. if ( m_pszPassword != NULL )
  263. lstrcpy( m_pszPassword, _T( "*" ) );
  264. // do the actual parsing of the command line arguments and check the result
  265. bReturn = DoParseParam( argc, argv, MAX_COMMANDLINE_D_OPTION, cmdOptions);
  266. if(bReturn==FALSE)
  267. throw CShowError(MK_E_SYNTAX);
  268. // empty Server is not valid
  269. szTempString = m_pszServerName;
  270. szTempString.TrimRight();
  271. lstrcpy(m_pszServerName,(LPCWSTR)szTempString);
  272. if ( (cmdOptions[ID_D_SERVER].dwActuals != 0) &&
  273. (lstrlen( m_pszServerName) == 0 ))
  274. {
  275. throw CShowError(IDS_ERROR_SERVERNAME_EMPTY);
  276. }
  277. // "-u" should not be specified without "-s"
  278. if ( cmdOptions[ ID_D_SERVER ].dwActuals == 0 &&
  279. cmdOptions[ ID_D_USERNAME ].dwActuals != 0 )
  280. {
  281. throw CShowError(IDS_ERROR_USERNAME_BUT_NOMACHINE);
  282. }
  283. // empty user is not valid
  284. szTempString = m_pszUserName;
  285. szTempString.TrimRight();
  286. lstrcpy(m_pszUserName,(LPCWSTR)szTempString);
  287. if ( (cmdOptions[ID_D_USERNAME].dwActuals != 0) &&
  288. (lstrlen( m_pszUserName) == 0 ))
  289. {
  290. throw CShowError(IDS_ERROR_USERNAME_EMPTY);
  291. }
  292. // "-p" should not be specified without -u
  293. if ( (cmdOptions[ID_D_USERNAME].dwActuals == 0) &&
  294. (cmdOptions[ID_D_PASSWORD].dwActuals != 0 ))
  295. {
  296. // invalid syntax
  297. throw CShowError(IDS_USERNAME_REQUIRED);
  298. }
  299. // check whether caller should accept the password or not
  300. // if user has specified -s (or) -u and no "-p", then utility should accept
  301. // password the user will be prompter for the password only if establish
  302. // connection is failed without the credentials information
  303. if ( cmdOptions[ ID_D_PASSWORD ].dwActuals != 0 &&
  304. m_pszPassword != NULL && lstrcmp( m_pszPassword, _T( "*" ) ) == 0 )
  305. {
  306. // user wants the utility to prompt for the password before trying to connect
  307. m_bNeedPassword = TRUE;
  308. }
  309. else if ( cmdOptions[ ID_D_PASSWORD ].dwActuals == 0 &&
  310. ( cmdOptions[ ID_D_SERVER ].dwActuals != 0 || cmdOptions[ ID_D_USERNAME ].dwActuals != 0 ) )
  311. {
  312. // -s, -u is specified without password ...
  313. // utility needs to try to connect first and if it fails then prompt for the password
  314. m_bNeedPassword = TRUE;
  315. if ( m_pszPassword != NULL )
  316. {
  317. lstrcpy( m_pszPassword, _T( "" ) );
  318. }
  319. }
  320. if(cmdOptions[ ID_D_ID ].dwActuals==0)
  321. {
  322. throw CShowError(IDS_ID_REQUIRED);
  323. }
  324. }
  325. // ***************************************************************************
  326. // Routine Description:
  327. // This routine will delete EventTriggers from WMI.
  328. //
  329. // Arguments:
  330. // None
  331. // Return Value:
  332. // None
  333. //
  334. //***************************************************************************
  335. BOOL
  336. CETDelete::ExecuteDelete()
  337. {
  338. BOOL bResult = FALSE; // Stores functins return status.
  339. LPTSTR lpTemp = NULL;
  340. LONG lTriggerID = 0;
  341. DWORD dNoOfIds=0; // Total Number of Event Trigger Ids...
  342. DWORD dwIndx = 0;
  343. BOOL bIsValidCommandLine = TRUE;
  344. BOOL bIsWildcard = FALSE;
  345. BOOL bRet = TRUE;
  346. HRESULT hr = S_OK; // used to reecive result form COM functions
  347. VARIANT vVariant;// variable used to get/set values from/to COM functions
  348. BSTR bstrTemp = NULL;
  349. TCHAR szEventTriggerName[MAX_RES_STRING];
  350. TCHAR szTaskSheduler[MAX_RES_STRING];
  351. TCHAR szMsgString[MAX_RES_STRING]; // Stores Message String
  352. TCHAR szMsgFormat[MAX_RES_STRING]; // Stores Message String
  353. DWORD lTemp = 0;
  354. BOOL bIsAtLeastOne = FALSE;
  355. try
  356. {
  357. // Analyze the default argument for ID
  358. dNoOfIds = DynArrayGetCount( m_arrID );
  359. for(dwIndx = 0;dwIndx<dNoOfIds;dwIndx++)
  360. {
  361. lstrcpy(m_pszTemp,
  362. DynArrayItemAsString(m_arrID,dwIndx));
  363. if(lstrcmp(m_pszTemp,ASTERIX)==0)
  364. {
  365. // Wildcard "*" cannot be clubed with other ids
  366. bIsWildcard = TRUE;
  367. if(dNoOfIds > 1)
  368. {
  369. bIsValidCommandLine = FALSE;
  370. break;
  371. }
  372. }
  373. else if(IsNumeric(m_pszTemp,10,FALSE)==FALSE)
  374. {
  375. // Other than "*" are not excepted
  376. throw CShowError(IDS_ID_NON_NUMERIC);
  377. }
  378. else if((AsLong(m_pszTemp,10)==0)||
  379. (AsLong(m_pszTemp,10)>ID_MAX_RANGE))
  380. {
  381. throw CShowError(IDS_INVALID_ID);
  382. }
  383. }
  384. m_hStdHandle = GetStdHandle(STD_ERROR_HANDLE);
  385. if(m_hStdHandle!=NULL)
  386. {
  387. GetConsoleScreenBufferInfo(m_hStdHandle,&m_ScreenBufferInfo);
  388. }
  389. InitializeCom(&m_pWbemLocator);
  390. m_bIsCOMInitialize = TRUE;
  391. ////////////////////////////////////////////////////
  392. // Connect Server.....
  393. {
  394. CHString szTempUser = m_pszUserName;
  395. CHString szTempPassword = m_pszPassword;
  396. BOOL bLocalSystem = TRUE;
  397. bResult = ConnectWmiEx( m_pWbemLocator,
  398. &m_pWbemServices,
  399. m_pszServerName,
  400. szTempUser,
  401. szTempPassword,
  402. &m_pAuthIdentity,
  403. m_bNeedPassword,
  404. WMI_NAMESPACE_CIMV2,
  405. &bLocalSystem);
  406. if(bResult == FALSE)
  407. {
  408. TCHAR szErrorMsg[MAX_RES_STRING+1];
  409. DISPLAY_MESSAGE2( stderr, szErrorMsg, L"%s %s", TAG_ERROR,
  410. GetReason());
  411. return FALSE;
  412. }
  413. // check the remote system version and its compatiblity
  414. if ( bLocalSystem == FALSE )
  415. {
  416. DWORD dwVersion = 0;
  417. dwVersion = GetTargetVersionEx( m_pWbemServices, m_pAuthIdentity );
  418. if ( dwVersion <= 5000 )// to block win2k versions
  419. {
  420. TCHAR szErrorMsg[MAX_RES_STRING+1];
  421. SetReason( ERROR_OS_INCOMPATIBLE );
  422. DISPLAY_MESSAGE2( stderr, szErrorMsg, L"%s %s", TAG_ERROR,
  423. GetReason());
  424. return FALSE;
  425. }
  426. }
  427. // check the local credentials and if need display warning
  428. if ( bLocalSystem && (lstrlen(m_pszUserName)!=0) )
  429. {
  430. CHString str;
  431. WMISaveError( WBEM_E_LOCAL_CREDENTIALS );
  432. str.Format( L"%s %s", TAG_WARNING, GetReason() );
  433. ShowMessage( stdout, str );
  434. }
  435. }
  436. ///////////////////////////////////////////////////
  437. // Show wait message
  438. m_hStdHandle = GetStdHandle(STD_ERROR_HANDLE);
  439. if(m_hStdHandle!=NULL)
  440. {
  441. GetConsoleScreenBufferInfo(m_hStdHandle,&m_ScreenBufferInfo);
  442. }
  443. PrintProgressMsg(m_hStdHandle,GetResString(IDS_MSG_EVTRIG_D),m_ScreenBufferInfo);
  444. // retrieves TriggerEventConsumer class
  445. bstrTemp = SysAllocString(CLS_TRIGGER_EVENT_CONSUMER);
  446. hr = m_pWbemServices->GetObject(bstrTemp,
  447. 0, NULL, &m_pClass, NULL);
  448. RELEASE_BSTR(bstrTemp);
  449. ON_ERROR_THROW_EXCEPTION(hr);
  450. // Gets information about the "DeleteETrigger" method of
  451. // "TriggerEventConsumer" class
  452. bstrTemp = SysAllocString(FN_DELETE_ETRIGGER);
  453. hr = m_pClass->GetMethod(bstrTemp,
  454. 0, &m_pInClass, NULL);
  455. RELEASE_BSTR(bstrTemp);
  456. ON_ERROR_THROW_EXCEPTION(hr);
  457. // create a new instance of a class "TriggerEventConsumer ".
  458. hr = m_pInClass->SpawnInstance(0, &m_pInInst);
  459. ON_ERROR_THROW_EXCEPTION(hr);
  460. //Following method will creates an enumerator that returns the instances of
  461. // a specified TriggerEventConsumer class
  462. bstrTemp = SysAllocString(CLS_TRIGGER_EVENT_CONSUMER);
  463. hr = m_pWbemServices->CreateInstanceEnum(bstrTemp,
  464. WBEM_FLAG_SHALLOW,
  465. NULL,
  466. &m_pEnumObjects);
  467. RELEASE_BSTR(bstrTemp);
  468. ON_ERROR_THROW_EXCEPTION(hr);
  469. // set the security at the interface level also
  470. hr = SetInterfaceSecurity( m_pEnumObjects, m_pAuthIdentity );
  471. ON_ERROR_THROW_EXCEPTION(hr);
  472. if(bIsWildcard == TRUE) // means * is choosen
  473. {
  474. // instance of NTEventLogConsumer is cretated and now check
  475. // for available TriggerID
  476. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  477. while(GiveTriggerID(&lTriggerID,szEventTriggerName)==TRUE)
  478. {
  479. LONG lTemp = -1;
  480. VariantInit(&vVariant);
  481. // Set the TriggerName property .
  482. hr = PropertyPut(m_pInInst,FPR_TRIGGER_NAME,_bstr_t(szEventTriggerName));
  483. ON_ERROR_THROW_EXCEPTION(hr);
  484. // All The required properties sets, so
  485. // executes DeleteETrigger method to delete eventtrigger
  486. hr = m_pWbemServices->ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  487. _bstr_t(FN_DELETE_ETRIGGER),
  488. 0, NULL, m_pInInst,&m_pOutInst,NULL);
  489. ON_ERROR_THROW_EXCEPTION(hr);
  490. // Get Return Value from DeleteETrigger function
  491. DWORD dwTemp;
  492. if(PropertyGet(m_pOutInst,FPR_RETURN_VALUE,dwTemp)==FALSE)
  493. {
  494. return FALSE;
  495. }
  496. lTemp = (LONG)dwTemp;
  497. if(lTemp==0) // Means deletion is successful......
  498. {
  499. lstrcpy(szMsgFormat,GetResString(IDS_DELETE_SUCCESS));
  500. FORMAT_STRING2(szMsgString,szMsgFormat,_X(szEventTriggerName),
  501. lTriggerID);
  502. ShowMessage(stdout,szMsgString);
  503. bIsAtLeastOne = TRUE;
  504. }
  505. else if ((lTemp == 1))// Provider sends this if if failed to
  506. // delete eventrigger of given ID
  507. // as id no longer exits....
  508. {
  509. lstrcpy(szMsgFormat,GetResString(IDS_DELETE_ERROR));
  510. FORMAT_STRING(szMsgString,szMsgFormat,lTriggerID);
  511. // Message shown on screen will be...
  512. // Info: "EventID" is not a Valid Event ID
  513. ShowMessage(stderr,szMsgString);
  514. }
  515. else if (lTemp==2) // Means unable to delete trigger due to
  516. // some problem like someone renamed
  517. // Schedule task name etc.
  518. {
  519. lstrcpy(szMsgFormat,GetResString(IDS_UNABLE_DELETE));
  520. FORMAT_STRING(szMsgString,szMsgFormat,lTriggerID);
  521. // Message shown on screen will be...
  522. // Info: Unable to delete event trigger id "EventID".
  523. ShowMessage(stderr,szMsgString);
  524. bIsAtLeastOne = TRUE;
  525. }
  526. else if (lTemp == 3)// This error will come only if multiple
  527. // instances are running. This is due to
  528. // sending a non existance Trigger Name
  529. {
  530. continue; // Just ignore this error.
  531. }
  532. else
  533. {
  534. ON_ERROR_THROW_EXCEPTION(lTemp);
  535. }
  536. } // End of while loop
  537. if(bIsAtLeastOne==FALSE)
  538. {
  539. ShowMessage(stdout,GetResString(IDS_NO_EVENTID));
  540. }
  541. } // end of if
  542. else
  543. {
  544. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  545. for(dwIndx=0;dwIndx<dNoOfIds;dwIndx++)
  546. {
  547. lTriggerID = AsLong(DynArrayItemAsString(m_arrID,dwIndx),10);
  548. if(GiveTriggerName(lTriggerID,szEventTriggerName)==TRUE)
  549. {
  550. hr = VariantClear(&vVariant);
  551. ON_ERROR_THROW_EXCEPTION(hr);
  552. // Set the TriggerName property .
  553. hr = PropertyPut(m_pInInst,FPR_TRIGGER_NAME,_bstr_t(szEventTriggerName));
  554. ON_ERROR_THROW_EXCEPTION(hr);
  555. // All The required properties sets, so
  556. // executes DeleteETrigger method to delete eventtrigger
  557. hr = m_pWbemServices->ExecMethod(_bstr_t(CLS_TRIGGER_EVENT_CONSUMER),
  558. _bstr_t(FN_DELETE_ETRIGGER),
  559. 0, NULL, m_pInInst, &m_pOutInst,
  560. NULL);
  561. ON_ERROR_THROW_EXCEPTION(hr);
  562. // Get Return Value from DeleteETrigger function
  563. DWORD dwTemp;
  564. if(PropertyGet(m_pOutInst,FPR_RETURN_VALUE,dwTemp)==FALSE)
  565. {
  566. return FALSE;
  567. }
  568. lTemp = (LONG)dwTemp;
  569. if(lTemp==0) // Means deletion is successful......
  570. {
  571. lstrcpy(szMsgFormat,GetResString(IDS_DELETE_SUCCESS));
  572. FORMAT_STRING2(szMsgString,szMsgFormat,_X(szEventTriggerName),
  573. lTriggerID);
  574. ShowMessage(stdout,szMsgString);
  575. }
  576. else if ((lTemp == 1))// Provider sends this if if failed to
  577. // delete eventrigger of given ID
  578. {
  579. lstrcpy(szMsgFormat,GetResString(IDS_DELETE_ERROR));
  580. FORMAT_STRING(szMsgString,szMsgFormat,lTriggerID);
  581. // Message shown on screen will be...
  582. // FAILURE: "EventID" is not a Valid Event ID
  583. ShowMessage(stderr,szMsgString);
  584. }
  585. else if (lTemp==2) // Means unable to delete trigger due to
  586. // some problem like someone renamed
  587. // Schedule task name etc.
  588. {
  589. lstrcpy(szMsgFormat,GetResString(IDS_UNABLE_DELETE));
  590. FORMAT_STRING(szMsgString,szMsgFormat,lTriggerID);
  591. // Message shown on screen will be...
  592. // Info: Unable to delete event trigger id "EventID".
  593. ShowMessage(stderr,szMsgString);
  594. }
  595. else if (lTemp == 3)// This error will come only if multiple
  596. // instances are running. This is due to
  597. // sending a non existance Trigger Name
  598. // So for user we are showing invalid id
  599. {
  600. lstrcpy(szMsgFormat,GetResString(IDS_DELETE_ERROR));
  601. FORMAT_STRING(szMsgString,szMsgFormat,lTriggerID);
  602. // Message shown on screen will be...
  603. // FAILURE: "EventID" is not a Valid Event ID
  604. ShowMessage(stderr,szMsgString);
  605. continue; // Just ignore this error.
  606. }
  607. else
  608. {
  609. ON_ERROR_THROW_EXCEPTION(lTemp);
  610. }
  611. } // End if
  612. else
  613. {
  614. lstrcpy(szMsgFormat,GetResString(IDS_DELETE_ERROR));
  615. FORMAT_STRING(szMsgString,szMsgFormat,lTriggerID);
  616. // Message shown on screen will be...
  617. // FAILURE: "EventID" is not a Valid Event ID
  618. ShowMessage(stderr,szMsgString);
  619. }
  620. }// End for
  621. } // End else
  622. }
  623. catch(_com_error)
  624. {
  625. TCHAR szErrorMsg[MAX_RES_STRING+1];
  626. PrintProgressMsg(m_hStdHandle,NULL_STRING,m_ScreenBufferInfo);
  627. if(hr == 0x80041002)// WMI returns string for this hr value is
  628. // "Not Found." which is not user friendly. So
  629. // changing the message text.
  630. {
  631. ShowMessage( stderr,GetResString(IDS_CLASS_NOT_REG));
  632. }
  633. else
  634. {
  635. DISPLAY_MESSAGE2( stderr, szErrorMsg, L"%s %s", TAG_ERROR,
  636. GetReason() );
  637. }
  638. return FALSE;
  639. }
  640. return TRUE;
  641. }
  642. /******************************************************************************
  643. Routine Descripton:
  644. This function Will return Event Trigger Name for lTriggerID
  645. Arguments:
  646. [in] lTriggerID : Will Have Event Trigger ID
  647. [out] pszTriggerName : Will return Event Trigger Name
  648. [out] pszTaskSheduler : Will return TaskScheduler
  649. [in] m_pEnumObjects: Will have valid pointer value for
  650. NTEventLogConsumer class
  651. Return Value:
  652. TRUE - if Successfully Gets EventTrigger ID and Event Trigger Name
  653. FALSE - if ERROR
  654. ******************************************************************************/
  655. BOOL
  656. CETDelete::GiveTriggerName(LONG lTriggerID, LPTSTR pszTriggerName)
  657. {
  658. BOOL bReturn = TRUE; // holds status of return value of this function
  659. LONG lTriggerID1; // Holds trigger id
  660. IWbemClassObject *pObj1 = NULL;
  661. ULONG uReturned1 = 0;
  662. HRESULT hRes = S_OK; // used to reecive result form COM functions
  663. // Resets it as It may be previouly pointing to other than first instance
  664. m_pEnumObjects->Reset();
  665. while(1)
  666. {
  667. hRes = m_pEnumObjects->Next(0,1,&pObj1,&uReturned1);
  668. if(FAILED(hRes))
  669. {
  670. ON_ERROR_THROW_EXCEPTION( hRes );
  671. break;
  672. }
  673. if(uReturned1 == 0)
  674. {
  675. SAFE_RELEASE_INTERFACE(pObj1);
  676. bReturn = FALSE;
  677. return bReturn;
  678. }
  679. // Get Trigger ID
  680. hRes = PropertyGet1(pObj1,FPR_TRIGGER_ID,0,&lTriggerID1,sizeof(LONG));
  681. if(FAILED(hRes))
  682. {
  683. SAFE_RELEASE_INTERFACE(pObj1);
  684. ON_ERROR_THROW_EXCEPTION( hRes );
  685. bReturn = FALSE;
  686. return bReturn;
  687. }
  688. if(lTriggerID==lTriggerID1)
  689. {
  690. // Get Trigger Name
  691. hRes = PropertyGet1(pObj1,FPR_TRIGGER_NAME,0,pszTriggerName,MAX_RES_STRING);
  692. if(FAILED(hRes))
  693. {
  694. SAFE_RELEASE_INTERFACE(pObj1);
  695. ON_ERROR_THROW_EXCEPTION( hRes );
  696. bReturn = FALSE;
  697. return bReturn;
  698. }
  699. bReturn = TRUE;
  700. break;
  701. }
  702. }
  703. SAFE_RELEASE_INTERFACE(pObj1);
  704. return bReturn;
  705. }
  706. /******************************************************************************
  707. Routine Description:
  708. This function Will return Trigger Id and Trigger Name of class pointed
  709. by IEnumWbemClassObject pointer
  710. Arguments:
  711. [out] pTriggerID : Will return Event Trigger ID
  712. [out] pszTriggerName : Will return Event Trigger Name
  713. [out] pszTaskSheduler : Will return TaskScheduler
  714. [in] m_pEnumObjects: Will have valid pointer value for
  715. NTEventLogConsumer class
  716. Return Value:
  717. TRUE - if Successfully Gets EventTrigger ID and Event Trigger Name
  718. FALSE - if ERROR
  719. ******************************************************************************/
  720. BOOL
  721. CETDelete::GiveTriggerID(LONG *pTriggerID,LPTSTR pszTriggerName)
  722. {
  723. BOOL bReturn = TRUE; // status of return value of this function
  724. IWbemClassObject *pObj1 = NULL;
  725. TCHAR strTemp[MAX_RES_STRING];
  726. ULONG uReturned1 = 0;
  727. DWORD dwTemp = 0;
  728. HRESULT hRes = m_pEnumObjects->Next(0,1,&pObj1,&uReturned1);
  729. if(FAILED(hRes))
  730. {
  731. ON_ERROR_THROW_EXCEPTION( hRes );
  732. bReturn = FALSE;
  733. return bReturn;
  734. }
  735. if(uReturned1 == 0)
  736. {
  737. SAFE_RELEASE_INTERFACE(pObj1);
  738. bReturn = FALSE;
  739. return bReturn;
  740. }
  741. // Get Trigger ID
  742. hRes = PropertyGet1(pObj1,FPR_TRIGGER_ID,0,pTriggerID,sizeof(LONG));
  743. if(FAILED(hRes))
  744. {
  745. SAFE_RELEASE_INTERFACE(pObj1);
  746. ON_ERROR_THROW_EXCEPTION( hRes );
  747. bReturn = FALSE;
  748. return bReturn;
  749. }
  750. // Get Trigger Name
  751. hRes = PropertyGet1(pObj1,FPR_TRIGGER_NAME,0,pszTriggerName,MAX_RES_STRING);
  752. if(FAILED(hRes))
  753. {
  754. SAFE_RELEASE_INTERFACE(pObj1);
  755. ON_ERROR_THROW_EXCEPTION( hRes );
  756. bReturn = FALSE;
  757. return bReturn;
  758. }
  759. SAFE_RELEASE_INTERFACE(pObj1);
  760. return bReturn;
  761. }