Task Scheduler: Problem attempting to retrieve a task object

1

I am using/learning the Windows Task Scheduler in win32 C++. I am attempting to retrieve a task object (for a task that exists) but it continually fails & returns the error COR_E_FILENOTFOUND 0x80070002 = The task does not exist

What do you think is going wrong? I know this task exists because I create it (& it does what its supposed to do, open an app at specified time).

Maybe the task name I use to retrieve the ITask object is not correct? The status (priority?) of the task I create is == 3 (if that info helps).

The code I use to create a task is exactly the same as the example code from msdn, the task name is "MyTaskSascha".

Is there anything wrong with my code to retrieve an existing task (I believe the task I am trying to retrieve is not considered "Running" but "Scheduled" so that maybe the problem?):

bool RemoveTask( std::string taskName )
{
// Post: 

ITaskScheduler  *taskSched = NULL;
ITask           *task      = NULL;
HRESULT         hr         = S_OK;
HRESULT         taskStatus = NULL;

LPCWSTR wTaskName;
wTaskName = L"MyTestSascha";

/// Initialise COM library & obtain Task Scheduler object

hr = CoInitialize( NULL );

if ( FAILED(hr) )
{
    printf( "Failed to coinitialise hresult \n" );
    return false;
}

hr = CoCreateInstance( CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, 
                       IID_ITaskScheduler, (void**) &taskSched );

if ( FAILED(hr) )
{
    printf( "Failed to create instance \n" );
    CoUninitialize();
    return false;
}

/// Obtain task object

hr = taskSched -> Activate( wTaskName, IID_ITask, (IUnknown**) &task );
taskSched -> Release();

std::cout <<  wTaskName << std::endl;
printf( "%s \n", wTaskName );

if ( FAILED(hr) )
{
    // COR_E_FILENOTFOUND E_INVALIDARG E_OUTOFMEMORY SCHED_E_UNKNOWN_OBJECT_VERSION
    if      ( hr == 0x80070002 ) { std::cout << "The task does not exist \n"; }
    else if ( hr == 0x80000003 ) { std::cout << "The pwszName parameter is not valid \n"; }
    else if ( hr == 0x80070057 ) { std::cout << "A memory allocation failed \n"; }
    else if ( hr == 0x80041313 ) { std::cout << "The task object version is either unsupported or invalid \n"; }
    printf( "Failed retrieving task object %x \n", hr );
    CoUninitialize();
    return false;
}
winapi
scheduled-tasks
asked on Stack Overflow Jan 15, 2011 by Sascha

1 Answer

1

You are creating the task using the Task Scheduler 2.0 API, but are trying to access it using the older 1.0 API.

The task scheduler and its COM API have been redesigned for Windows Vista, and the Task Scheduler 1.0 API cannot be used to access tasks that use the new interface (presumably because it uses a very different design that allows for a lot of new features). You can create a backward compatible task that can be accessed using this example code by selecting "Configure for: Windows Server 2003, Windows XP, or Windows 2000" when creating the task using the MMC snap-in, or presumably by creating it using the old API (I did not succeed in doing so in a simple test application, though). Tasks in the root folder work for me this way with or without the leading backslash; tasks in sub-folders do not seem to be accessible at all.
If you are not interested in compatibility with older Windows versions, you could just access the task using the new API.

MSDN examples exist for both API versions (note the top/bottom separation) and the new features relevant for developers are also listed there.

answered on Stack Overflow Feb 2, 2011 by ThFabba • edited Feb 3, 2011 by ThFabba

User contributions licensed under CC BY-SA 3.0