C ++ - 使用Task Scheduler 1.0接口添加任务会创建不可运行的任务

时间:2014-12-09 11:21:27

标签: winapi visual-c++ windows-xp scheduled-tasks

所以我正在编写一个c ++代码,其目标是向任务调度程序添加一个简单的任务。 该应用程序将在windows XP中运行,因此我正在使用Task Scheduler 1.0 Interfaces

我按照示例C/C++ Code Example: Creating a Task Using NewWorkItem进行了调整,并稍微调整一下以满足我的需求。

#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;
}

调整基本上是添加了对ITask::SetApplicationName method的调用,我认为这与我的问题无关。

代码编译并运行良好。我可以看到添加了一项任务。

问题

当我尝试运行任务时,我得到了:

Could not start

检查文件SchedLgU.txt我看到了:

""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.

我可以看到here原因可能是由于:

The account credentials are missing.

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。这条线丢失了:

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

声明

这仅适用于管理员用户帐户。我还没有想出办法让它在非管理员帐户中运行。