如何使用 C++ 检查任务调度程序中的任务触发器?

How to check trigger of a task in Task scheduler using c++?

我想使用 C++ 检查任务计划程序中任务的触发器。 我想使用函数 HRESULT get_Type(TASK_TRIGGER_TYPE2 *pType); 检查任务是登录还是开机触发。

TASK_STATE taskState;

for (LONG i = 0; i < numTasks; i++)
{
  IRegisteredTask* pRegisteredTask = NULL;
  hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);

 if (SUCCEEDED(hr))
    {
        BSTR taskName = NULL;
                    TASK_TRIGGER_TYPE2 *pType = NULL;
        hr = pRegisteredTask->get_Name(&taskName);               
        if (SUCCEEDED(hr))
        {
          printf("\nTask Name: %S", taskName);
          SysFreeString(taskName);
          hr = pRegisteredTask->get_Type(*pType);   //Implemented here
          if (SUCCEEDED(hr))
             printf("\n%s",&pType);
          else
             printf("\n\tCannot get the registered task state: %x", hr);
         } 

          hr = pRegisteredTask->get_State(&taskState);
          if (SUCCEEDED(hr))
       printf("\n\tState: %d", taskState);
          else
    printf("\n\tCannot get the registered task state: %x", hr);
        }
        else
        {
    printf("\nCannot get the registered task name: %x", hr);
        }
    pRegisteredTask->Release();
    }
    else
    {
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
    }
}

在编译时,它给我一个错误 "IRegisteredTask has no member get_Type()"

然后我修改了代码并添加了 ITrigger *trig = NULL; trig->get_Type(&pType);

但这也没有给我任何价值

查看 documentation,看起来在你有一个 IRegisteredTask 之后你需要调用 get_Definition() 来获取它的 ITaskDefinition。

使用 ITaskDefinition,您可以通过 get_Triggers()

获取 ITriggerCollection

然后,枚举 ITriggerCollection,您可以 QueryInterface() 每个 ITrigger 以查看它是否支持 ILogonTrigger 或 IBootTrigger 接口。

以下是适合我的代码片段你可以试一试:

for (LONG i = 0; i < numTasks; i++)
{
    IRegisteredTask* pRegisteredTask = NULL;
    hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);

    if (SUCCEEDED(hr))
    {
        BSTR taskName = NULL;
        hr = pRegisteredTask->get_Name(&taskName);
        if (SUCCEEDED(hr))
        {
            printf("\nTask Name: %S", taskName);
            SysFreeString(taskName);

            hr = pRegisteredTask->get_State(&taskState);
            if (SUCCEEDED(hr))
                printf("\n\tState: %d", taskState);
            else
                printf("\n\tCannot get the registered task state: %x", hr);
        }
        else
        {
            printf("\nCannot get the registered task name: %x", hr);
        }

        ITaskDefinition* taskDef = NULL;
        hr = pRegisteredTask->get_Definition(&taskDef);
        if (SUCCEEDED(hr))
        {
            ITriggerCollection* triggers = NULL;
            taskDef->get_Triggers(&triggers);
            LONG trigCnt = 0;
            triggers->get_Count(&trigCnt);

            for (LONG i = 0; i < trigCnt; i++)
            {
                ITrigger* trig = NULL;
                TASK_TRIGGER_TYPE2 pType = TASK_TRIGGER_EVENT;

                triggers->get_Item(_variant_t(i + 1), &trig);
                trig->get_Type(&pType);
                DWORD errCode = GetLastError();
                if(pType != NULL)
                    printf("\nTrigger Type : %d", pType);
            }

        }
        else
        {
            printf("\nCannot get the registered task definition: %x", hr);
        }

        pRegisteredTask->Release();
    }
    else
    {
        printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
    }
}

参考“Displaying Task Names and States (C++)