zoukankan      html  css  js  c++  java
  • Task Scheduler 参看——有关闭电源设置和添加目录设置参考

        //for windows Vista or later OS and admin
     if ( IsVistaorLater() && IsAdmin() )
     {
      //  Initialize COM.
      ::CoInitialize(NULL);

      // Provides access to the Task Scheduler service for managing registered tasks.
      CComPtr<ITaskService> pService;
      HRESULT hr = CoCreateInstance(CLSID_TaskScheduler,
               NULL,
               CLSCTX_INPROC_SERVER,
               IID_ITaskService,
               (void**)&pService);
      if ( SUCCEEDED(hr) )
      {
       //  The Connect method should be called before calling any of the other ITaskService methods.
       // Connects to a remote computer and associates all subsequent calls on this interface
       // with a remote session. If the serverName parameter is empty, then this method will
       // execute on the local computer. If the userId is not specified, then the current token is used.
       hr = pService->Connect(CComVariant(), CComVariant(), CComVariant(), CComVariant());
       if ( SUCCEEDED(hr) )
       {
        // Provides the methods that are used to register (create) tasks in the folder,
        // remove tasks from the folder, and create or remove subfolders from the folder.
        CComPtr<ITaskFolder> pRootFolder;

        //Gets a folder that contains tasks at a specified location
        hr = pService->GetFolder(CComBSTR(_T("\\")), &pRootFolder);
        if ( SUCCEEDED(hr) )
        {
         //Get SONY folder
         CComPtr<ITaskFolder> pSonyFolder;
         hr = pRootFolder->GetFolder(CComBSTR(TEST), &pSonyFolder);
         if ( FAILED(hr) )
         {
          //Create SONY folder
          pRootFolder->CreateFolder(CComBSTR(TEST), CComVariant(), &pSonyFolder);
         }

         CComPtr<ITaskFolder> pPYVFolder;
         hr = pSonyFolder->GetFolder(CComBSTR(TASK_NAME), &pPYVFolder);
         if ( FAILED(hr) )
         {
          // Creates a folder for related tasks
          pSonyFolder->CreateFolder(CComBSTR(TASK_NAME), CComVariant(), &pPYVFolder);
         }

         //  If the same task exists, remove it.
         pPYVFolder->DeleteTask(CComBSTR(TASK_NAME), 0);

         // Defines all the components of a task, such as the task settings,
         // triggers, actions, and registration information.
         CComPtr<ITaskDefinition> pTask;

         // Returns an empty task definition object to be filled in with settings and properties
         // and then registered using the ITaskFolder::RegisterTaskDefinition method.
         hr = pService->NewTask(0, &pTask);
         if ( SUCCEEDED(hr) )
         {
          // Provides the security credentials for a principal. These security credentials
          // define the security context for the tasks that are associated with the principal.
          CComPtr<IPrincipal> pPrincipal;
          hr = pTask->get_Principal(&pPrincipal);
          if ( SUCCEEDED(hr) )
          {
           // Defines LUA elevation flags that specify with what privilege level the task will be run.
           // TASK_RUNLEVEL_HIGHEST:Tasks will be run with the highest privileges.
           hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);
           if ( SUCCEEDED(hr) )
           {
            hr = pTask->put_Principal(pPrincipal);
            if ( SUCCEEDED(hr) )
            {
             //  Get the registration info for setting the identification.
             CComPtr<IRegistrationInfo> pRegInfo;
             pTask->get_RegistrationInfo(&pRegInfo);
             pRegInfo->put_Author((CComBSTR)SONY);

             //  Create the settings for the task
             CComPtr<ITaskSettings> pSettings;
             pTask->get_Settings(&pSettings);
             pSettings->put_DisallowStartIfOnBatteries(VARIANT_BOOL(FALSE));//auto run even OnBatteries
             pSettings->put_StopIfGoingOnBatteries(VARIANT_BOOL(FALSE));
             pSettings->put_ExecutionTimeLimit( CComBSTR(_T("PT0S")));//never auto end
             pSettings->put_MultipleInstances(TASK_INSTANCES_PARALLEL);
             pSettings->put_AllowDemandStart(TRUE);
             pSettings->put_AllowHardTerminate(TRUE);

             //  Set setting values for the task.
             hr = pSettings->put_StartWhenAvailable(VARIANT_BOOL(TRUE));
             if ( SUCCEEDED(hr) )
             {
              //  Get the trigger collection to insert the logon trigger.
              CComPtr<ITriggerCollection> pTriggerCollection;
              hr = pTask->get_Triggers(&pTriggerCollection);
              if ( SUCCEEDED(hr) )
              {
               //  Add the logon trigger to the task.
               CComPtr<ITrigger> pTrigger;
               hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger);
               if ( SUCCEEDED(hr) )
               {
                CComPtr<ILogonTrigger> pLogonTrigger;  
                pTrigger->QueryInterface(IID_ILogonTrigger, (void**)&pLogonTrigger);
                pLogonTrigger->put_Delay(CComBSTR(_T("PT5S")));
                pLogonTrigger->put_Id( CComBSTR( TASK_NAME ) );

                //  Add an Action to the task. This task will execute 
                CComPtr<IActionCollection> pActionCollection;
                hr = pTask->get_Actions(&pActionCollection);
                if ( SUCCEEDED(hr) )
                {
                 //  Create the action, specifying that it is an executable action.
                 CComPtr<IAction> pAction;
                 hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction);
                 if ( SUCCEEDED(hr) )
                 {
                  // Represents an action that executes a command-line operation.
                  CComPtr<IExecAction> pExecAction;
                  //  QI for the executable task pointer.
                  hr = pAction->QueryInterface(IID_IExecAction,
                   (void**)&pExecAction);
                  if ( SUCCEEDED(hr) )
                  {
                   // set app path and arguments
                   pExecAction->put_Path(CComBSTR(strAppPath.AllocSysString()));
                   pExecAction->put_Arguments(CComBSTR(_T("")));
                   // Provides the methods that are used to run the task immediately,
                   // get any running instances of the task, get or set the credentials
                   // that are used to register the task, and the properties that describe the task.
                   CComPtr<IRegisteredTask> pRegisteredTask;
                   hr = pPYVFolder->RegisterTaskDefinition(
                    CComBSTR(TASK_NAME),
                    pTask,
                    TASK_CREATE_OR_UPDATE,
                    CComVariant(_T("S-1-5-32-545")),
                    CComVariant(),
                    TASK_LOGON_GROUP,
                    CComVariant(_T("")),
                    &pRegisteredTask);
                   if ( SUCCEEDED(hr) )
                   {
                    bRet = TRUE;
                   }
                  }              
                 }             
                }            
               }           
              }          
             }
            }       
           }       
          }      
         }    
        }    
       }
      }
     }
     ::CoUninitialize();

  • 相关阅读:
    IP 地址无效化
    上升下降字符串
    STL-----map
    只出现一次的数字
    4的幂
    GDI+_入门教程【一】
    大白话系列之C#委托与事件讲解(二)
    大白话系列之C#委托与事件讲解(二)
    大白话系列之C#委托与事件讲解(一)
    大白话系列之C#委托与事件讲解(一)
  • 原文地址:https://www.cnblogs.com/lzjsky/p/1931782.html
Copyright © 2011-2022 走看看