Task scheduler in Vista

The IScheduledWorkItem::GetExitCode function behaves differently in Vista. It returns SCHED_S_TASK_HAS_NOT_RUN even if the task has been run.

Steps to reproduce:
Create a scheduled task, in the "Configure for" combo box select "Windows Server 2003,XP,2000".
Wait for the task to start and complete.
Write a small program that gets a pointer to the task's IScheduledWorkItem interface and calls GetExitCode. The return value will be SCHED_S_TASK_HAS_NOT_RUN. In Windows XP it would be S_OK.

Is it a bug


Answer this question

Task scheduler in Vista

  • koko

    Anton - yes, thank you for reporting, looks like this is a regression bug. How badly affected are you


  • andrew_zep

    Hi,

        When I get a task triger from one scheduled task and reset the time of the triger then save it, the task triger can't be updated. Do you have a solution Thanks!



  • Dale Beyer

    If the task is set to run 'Execute Independent of Logged in user', you will have to make a call to SetAccountInformation(...) and pass in the userId/Passwd before you save the Scheduled Task after trigger updates.
    Hope that helps.

  • Darrin Turner



    Task schedular runs elevated. The API may require elevation too, have you tried that

    I'm not seeing any bug that exactly matches your issue. I see an issue that the documentation needs be updated. I will see if can get that person to look into this more.




  • bhanoji

    I marked your answer as a work around until fixed. My understanding is that this is a high priority and hopefully in the first SP release.

    If you need help with your work around please post back.

  • Chris Honcoop

    Thanks for your great help! It works. I have another question about ITaskScheduler. How to use the flag TASK_FLAG_DELETE_WHEN_DONE to delete a task when it's done I set the only trigger's type as TASK_TIME_TRIGGER_ONCE. Thanks!

  • datahook

    Hi,

    what modification i needs to run the task in this security option (run whether user is logged on or not)

    TASK_EVENT_TRIGGER_AT_SYSTEMSTART i am using this task event but its not initiative on system startup

    Regards



  • ShadowRayz

    I compile with level="requireAdministrator" in .manifest.

    This test program should help you reproduce the issue. Error checking is omitted for clarity.
    Windows XP shows "GetExitCode returned 0x0 code=0x0" while Vista shows "GetExitCode returned 0x41303 code=0x0".

    #include<windows.h>
    #include<tchar.h>
    #include<lm.h>
    #include<initguid.h>
    #include<mstask.h>
    #include<comdef.h>

    _COM_SMARTPTR_TYPEDEF(ITaskScheduler,__uuidof(ITaskScheduler));
    _COM_SMARTPTR_TYPEDEF(ITask,__uuidof(ITask));
    _COM_SMARTPTR_TYPEDEF(IScheduledWorkItem,__uuidof(IScheduledWorkItem));
    _COM_SMARTPTR_TYPEDEF(ITaskTrigger,__uuidof(ITaskTrigger));

    int _tmain(int argc, _TCHAR* argv[])
    {
    CoInitialize(0);
    ITaskSchedulerPtr s;
    IUnknownPtr u;
    s.CreateInstance(CLSID_CTaskScheduler);
    s->Delete(L"test");
    {
    s->NewWorkItem(L"test",CLSID_CTask,IID_ITask,&u);
    ITaskPtr t;
    u->QueryInterface(&t);
    t->SetApplicationName(L"c:\\windows\\system32\\notepad.exe");
    t->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
    WCHAR name[UNLEN+1];
    DWORD foo=UNLEN+1;
    GetUserNameW(name,&foo);
    t->SetAccountInformation(name,0);

    WORD bar;
    ITaskTriggerPtr tt;
    t->CreateTrigger(&bar,&tt);
    TASK_TRIGGER tr={};
    tr.cbTriggerSize=sizeof tr;
    tr.TriggerType=TASK_TIME_TRIGGER_ONCE;
    SYSTEMTIME st;
    GetLocalTime(&st);
    tr.wBeginYear=st.wYear;
    tr.wBeginMonth=st.wMonth;
    tr.wBeginDay=st.wDay;
    tr.wEndYear=3000;
    tr.wEndMonth=1;
    tr.wEndDay=1;
    tr.wStartHour=st.wHour;
    tr.wStartMinute=st.wMinute+1;
    if(st.wSecond>55)st.wMinute+=1;
    tt->SetTrigger(&tr);

    IPersistFilePtr f;
    t->QueryInterface(&f);
    f->Save(0,TRUE);
    }
    MessageBoxW(0,L"Wait till notepad starts up, then close notepad and click OK",L"",MB_OK);
    {
    s->Activate(L"test",IID_ITask,&u);
    ITaskPtr t;
    u->QueryInterface(&t);
    DWORD code=0xbbbbcccc;
    HRESULT hr=t->GetExitCode(&code);
    WCHAR msg[1024];
    wsprintfW(msg,L"GetExitCode returned 0x%x code=0x%x\n",hr,code);
    MessageBoxW(0,msg,L"",MB_OK);
    }
    return 0;
    }


  • sph3ra

    Not much. It is workaroundable with the help of IScheduledWorkItem::GetMostRecentRunTime. This function allows to determine whether the task has actually been run or not.

  • Task scheduler in Vista