使用ITaskScheduler,ITask接口计划任务的问题

时间:2013-02-20 19:36:52

标签: c++ winapi com scheduled-tasks

下面的代码执行没有任何错误,但任务没有执行,我没有在控制面板中看到它。请帮忙

HRESULT CreateOnceActionTask(const wchar_t* taskName, const wchar_t* programPath, const wchar_t* params)
{
    HRESULT hr = S_OK;

    CComPtr<ITaskScheduler> taskScheduler;
    if(FAILED(hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER,   IID_ITaskScheduler, (void**)&taskScheduler)))
        ReturnErrorMsg(hr, L"Failed to get IID_ITaskScheduler");

    // create task
    CComPtr<ITask> task;
    if(FAILED(hr = taskScheduler->NewWorkItem(taskName, CLSID_CTask, IID_ITask, (IUnknown**)&task)))
    {
        //if(FAILED(hr = taskScheduler->Activate(taskName, IID_ITask, (IUnknown**) &task)))
        ReturnErrorMsg(hr, L"Failed to create task");

        //taskScheduler->Delete(taskName);

        //return S_OK;
    }

    if(FAILED(hr = task->SetApplicationName(programPath)))
        ReturnErrorMsg(hr, L"Failed to set SetApplicationName");

    if(FAILED(hr = task->SetParameters(params)))
        ReturnErrorMsg(hr, L"Failed to set SetParameters");

    if(FAILED(hr = task->SetTaskFlags(TASK_FLAG_DELETE_WHEN_DONE)))
        ReturnErrorMsg(hr, L"Failed to set task flags");

    // save task
    CComPtr<IPersistFile> persistTask;  
    if(FAILED(hr = task->QueryInterface(IID_IPersistFile, (void **)&persistTask)))
        ReturnErrorMsg(hr, L"Failed to save task with IPersistFile");   

    if(FAILED(hr = persistTask->Save(NULL,TRUE)))
        ReturnErrorMsg(hr, L"Failed to save task with IPersistFile");

    // create trigger
    CComPtr<ITaskTrigger> taskTrigger;
    CComPtr<IPersistFile> persistTrigger = NULL;    
    WORD wTrigNumber = 0;

    if (FAILED(hr = task->CreateTrigger(&wTrigNumber, &taskTrigger)))
        ReturnErrorMsg(hr, L"Failed to createTrigger");

    // create trigger description
    TASK_TRIGGER triggerStruct = {0};
    ZeroMemory ( &triggerStruct, sizeof (TASK_TRIGGER) );
    triggerStruct.cbTriggerSize = sizeof (TASK_TRIGGER);
    triggerStruct.TriggerType = TASK_TIME_TRIGGER_ONCE;

    // 10 minutes from now
    COleDateTime oDate = COleDateTime::GetCurrentTime() + COleDateTimeSpan(0, 0, 1, 0);

    triggerStruct.wBeginYear    = oDate.GetYear();
    triggerStruct.wBeginMonth   = oDate.GetMonth();
    triggerStruct.wBeginDay     = oDate.GetDay();
    triggerStruct.wStartHour    = oDate.GetHour();
    triggerStruct.wStartMinute  = oDate.GetMinute();


    // associate the trigger with the task
    if (FAILED(hr = taskTrigger->SetTrigger(&triggerStruct)))
        ReturnErrorMsg(hr, L"Failed to set trigger for the task");

    if (FAILED(hr = task->QueryInterface(IID_IPersistFile, (void **) &persistTrigger)))
        ReturnErrorMsg(hr, L"Failed to get IPersistFile for trigger");

    if (FAILED(hr = persistTrigger->Save(NULL, FALSE)))
        ReturnErrorMsg(hr, L"Failed to save trigger");

    if (FAILED(hr = task->Run()))
        ReturnErrorMsg(hr, L"Failed to run task");

    return S_OK;
}

0 个答案:

没有答案