C++ - Adding task using Task Scheduler 1.0 Interfaces creates non runnable task

2

So I'm writing a c++ code that its goal is to add a simple task to the task scheduler. The application is to be run in windows XP so I'm using Task Scheduler 1.0 Interfaces.

I followed the example, C/C++ Code Example: Creating a Task Using NewWorkItem, and tweaked it a little to fit my needs.

#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <objidl.h>
#include <wchar.h>
#include <stdio.h>


int main(int argc, char **argv)
{
  HRESULT hr = S_OK;
  ITaskScheduler *pITS;
  
  
  /////////////////////////////////////////////////////////////////
  // Call CoInitialize to initialize the COM library and then 
  // call CoCreateInstance to get the Task Scheduler object. 
  /////////////////////////////////////////////////////////////////
  hr = CoInitialize(NULL);
  if (SUCCEEDED(hr))
  {
     hr = CoCreateInstance(CLSID_CTaskScheduler,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_ITaskScheduler,
                           (void **) &pITS);
     if (FAILED(hr))
     {
        CoUninitialize();
        return 1;
     }
  }
  else
  {
     return 1;
  }
  
  
  /////////////////////////////////////////////////////////////////
  // Call ITaskScheduler::NewWorkItem to create new task.
  /////////////////////////////////////////////////////////////////
  LPCWSTR pwszTaskName;
  ITask *pITask;
  IPersistFile *pIPersistFile;
  pwszTaskName = L"Test Task";
  
  hr = pITS->NewWorkItem(pwszTaskName,         // Name of task
                         CLSID_CTask,          // Class identifier 
                         IID_ITask,            // Interface identifier
                         (IUnknown**)&pITask); // Address of task 
                                                                                                                                                                                            //  interface
  
  
  pITS->Release();                               // Release object
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling NewWorkItem, error = 0x%x\n",hr);
     return 1;
  }

    // set exe path
    hr = pITask->SetApplicationName("some_exe.exe");
    if (FAILED(hr))
    {
        CoUninitialize();
        return 1;
    }

  /////////////////////////////////////////////////////////////////
  // Call IUnknown::QueryInterface to get a pointer to 
  // IPersistFile and IPersistFile::Save to save 
  // the new task to disk.
  /////////////////////////////////////////////////////////////////
  
  hr = pITask->QueryInterface(IID_IPersistFile,
                              (void **)&pIPersistFile);
  
  pITask->Release();
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
     return 1;
  }
  
  
  hr = pIPersistFile->Save(NULL,
                           TRUE);
  pIPersistFile->Release();
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);
     return 1;
  }
  
  
  CoUninitialize();
  printf("Created task.\n");
  return 0;
}

The tweak is basically adding a call to ITask::SetApplicationName method which I don't think is related to my issue.

The code compiles and runs well. I can see that a task was added.

Problem

When I try to run the task I get:

Could not start

Inspecting file SchedLgU.txt I see:

""Test Task.job" (some_exe.exe) 12/9/2014 2:23:42 AM ** ERROR **
The attempt to retrieve account information for the specified task failed; therefore, the task did not run. Either an error occurred, or no account information existed for the task.
The specific error is:
0x8004130f: No account information could be found in the Task Scheduler security database for the task indicated..job" (some_exe.exe) 12/9/2014 2:23:42 AM ** ERROR **
    The attempt to retrieve account information for the specified task failed; therefore, the task did not run. Either an error occurred, or no account information existed for the task.
    The specific error is:
    0x8004130f: No account information could be found in the Task Scheduler security database for the task indicated.

I can see here that the cause might be due to:

The account credentials are missing.

Any ideas?

winapi
visual-c++
windows-xp
scheduled-tasks
asked on Stack Overflow Dec 9, 2014 by idanshmu • edited Jun 20, 2020 by Community

1 Answer

0

I found the issue. This line was missing:

hr = pITask->SetAccountInformation(L"", NULL);

Disclaimer

This only works in administrator user accounts. I haven't figured out a way to make it work in non administrator accounts.

answered on Stack Overflow Dec 9, 2014 by idanshmu

User contributions licensed under CC BY-SA 3.0