用 C++ 创建的任务调度程序无法启动

发布于 2024-10-04 20:22:36 字数 2620 浏览 4 评论 0原文

我用 C++ 创建了一个调度程序。我已经设置了所有参数,并且该任务配置为仅在用户使用提供的用户名登录时运行(通过设置 TASK_FLAG_RUN_ONLY_IF_LOGGED_ON 标志来完成)。

当我尝试运行任务时,我收到状态“无法启动”。现在假设我手动编辑任务属性中的任何属性,然后单击“确定”,任务运行正常。

注意:指定的手动编辑可以是任何内容,例如仅在可执行名称或用户名末尾添加空格。可能是什么问题?

下面是我正在使用的代码:

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

#pragma comment(lib, "Mstask.lib")
#pragma comment(lib, "ole32.lib")

int main(int argc, char **argv)
{
 HRESULT hr = S_OK;
 ITaskScheduler *pITS;

 ///////////////////////////////////////////////////////////////////
 // Call CoInitialize to initialize the COM library and then
 // 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;
 }

 LPCWSTR pwszTaskName;
 ITask *pITask;
  pwszTaskName = L"TestTask";

 hr = pITS->NewWorkItem(pwszTaskName,
  CLSID_CTask,
  IID_ITask,
 (IUnknown**)&pITask);

  if (FAILED(hr))
 {
  wprintf(L"Failed calling ITaskScheduler::NewWorkItem: ");
  wprintf(L"error = 0x%x\n",hr);
  CoUninitialize();
   return 1;
  }


  LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe";

  hr = pITask->SetApplicationName(pwszApplicationName);

  if (FAILED(hr))
 {
  wprintf(L"Failed calling ITask::SetApplicationName: ");
  wprintf(L"error = 0x%x\n",hr);
  pITS->Release();
  pITask->Release();
  CoUninitialize();
  return 1;
 }

  pITask->SetAccountInformation(L"USERNAME", NULL);
  pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
  pITask->SetWorkingDirectory(L"C:\\windows");


  ITaskTrigger *pITaskTrigger;
  WORD piNewTrigger;
  hr = pITask->CreateTrigger(&piNewTrigger,
                         &pITaskTrigger);
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::CreatTrigger: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    CoUninitialize();
    return 1;
  }

  pITS->AddWorkItem(pwszTaskName, pITask);
  pITS->Release(); // Release sceduler

   hr = pITask->Run();
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr);
    pITask->Release();
   CoUninitialize();
   return 1;
  }

   pITask->Release();      
  CoUninitialize();
    _getch();
  return 0;
}

I created an scheduler in C++.I have set all the parameters and the task is configured to run only when the user is logged on with the user name provided(Done by setting the TASK_FLAG_RUN_ONLY_IF_LOGGED_ON flag).

When I try to run the task I get a status "Could not start". Now suppose I manually edit any property in task property and click on OK the task runs fine.

Note:The manual edit specified may be anything, like just adding a space at the end of the excecutable name or the user name. What may be the problem?

Below is the code i am using:

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

#pragma comment(lib, "Mstask.lib")
#pragma comment(lib, "ole32.lib")

int main(int argc, char **argv)
{
 HRESULT hr = S_OK;
 ITaskScheduler *pITS;

 ///////////////////////////////////////////////////////////////////
 // Call CoInitialize to initialize the COM library and then
 // 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;
 }

 LPCWSTR pwszTaskName;
 ITask *pITask;
  pwszTaskName = L"TestTask";

 hr = pITS->NewWorkItem(pwszTaskName,
  CLSID_CTask,
  IID_ITask,
 (IUnknown**)&pITask);

  if (FAILED(hr))
 {
  wprintf(L"Failed calling ITaskScheduler::NewWorkItem: ");
  wprintf(L"error = 0x%x\n",hr);
  CoUninitialize();
   return 1;
  }


  LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe";

  hr = pITask->SetApplicationName(pwszApplicationName);

  if (FAILED(hr))
 {
  wprintf(L"Failed calling ITask::SetApplicationName: ");
  wprintf(L"error = 0x%x\n",hr);
  pITS->Release();
  pITask->Release();
  CoUninitialize();
  return 1;
 }

  pITask->SetAccountInformation(L"USERNAME", NULL);
  pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
  pITask->SetWorkingDirectory(L"C:\\windows");


  ITaskTrigger *pITaskTrigger;
  WORD piNewTrigger;
  hr = pITask->CreateTrigger(&piNewTrigger,
                         &pITaskTrigger);
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::CreatTrigger: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    CoUninitialize();
    return 1;
  }

  pITS->AddWorkItem(pwszTaskName, pITask);
  pITS->Release(); // Release sceduler

   hr = pITask->Run();
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr);
    pITask->Release();
   CoUninitialize();
   return 1;
  }

   pITask->Release();      
  CoUninitialize();
    _getch();
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-10-11 20:22:36

我认为您需要测试所有返回值,这可能会有所启发。我最怀疑的是:

 pITask->SetAccountInformation(L"USERNAME", NULL);
 pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);

阅读 http://msdn。 microsoft.com/en-us/library/aa381276(VS.85).aspx 我的印象是您需要先调用 SetFlags,然后调用 SetAccountInformation。

I think you need to test all return values, that could be revealing. I'm mostly suspicious about:

 pITask->SetAccountInformation(L"USERNAME", NULL);
 pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);

Readinig http://msdn.microsoft.com/en-us/library/aa381276(VS.85).aspx I got the impression that you need to call SetFlags first and then SetAccountInformation.

扭转时空 2024-10-11 20:22:36

我在 XP 上遇到了同样的问题:状态“无法启动”,手动编辑后一切正常。
解决办法:

  • 进入高级->查看日志。并查看失败的原因。
  • 就我而言,“尝试检索指定任务的帐户信息失败”。因此,我只需要通过 GetUserName 获取用户名并通过 SetAccountInformation 设置它。请注意,SetAccountInformationSetFlags 的顺序并不重要。
  • 出于其他原因,请查看此处

也许有一天它会对某人有所帮助。

I've had the same problem on XP: status "Could not start" and all ok after manual editing.
Solution:

  • Go to Advanced -> View Log. And see the reason for the fail.
  • In my case there was "The attempt to retrieve account information for the specified task failed". So I've just needed to get user name via GetUserName and set it via SetAccountInformation. Note order of SetAccountInformation and SetFlags doesn't matter.
  • Take a look here for other reasons.

May be it will help someone someday.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文