https://stackoverflow.com/questions/42307917/task-scheduler-api-error-80041318/42462235#42462235

Hi I am having an issue with the Task Scheduler API in my QT C++ program. I used the example code for a logon trigger here.

The first error I got was CoInitializeSecurity failed: RPC_E_TOO_LATE which means
CoInitializeSecurity has already been called. according to here. So I commented out the coinitializesecurity call and it fixed that.

However now I am getting the 80041318 error when trying to do the very last RegisterTaskDefinition step. I read here this means a value incorrectly formatted or out of range and also possibly an incorrect argument to pLogonTrigger.
I tried commenting out the start boundary and end boundary code for
the pLogonTrigger which didn't help. I also changed the changing the
pLogonTrigger UserId and the userId parameter to the RegisterTaskDefinition function to my account as L"Josh". The only arguments to pLogonTrigger that are left are specified via put_Id and put_UserId.

Should I include any code if that would help and if so which code?
The code is pretty much identical to the example code except for the
pLogonTrigger modifications, the userId mods, and the commenting out of
the cointializesecurity.

 //  Set setting values for the task.
hr = pSettings->put_StartWhenAvailable(VARIANT_TRUE);
pSettings->Release();
if( FAILED(hr) )
{
updateStatus("Cannot put setting info: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}*/ // ------------------------------------------------------
// Get the trigger collection to insert the logon trigger.
ITriggerCollection *pTriggerCollection = NULL;
hr = pTask->get_Triggers( &pTriggerCollection );
if( FAILED(hr) )
{
updateStatus("Cannot get trigger collection: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} // Add the logon trigger to the task.
ITrigger *pTrigger = NULL;
hr = pTriggerCollection->Create( TASK_TRIGGER_LOGON, &pTrigger );
pTriggerCollection->Release();
if( FAILED(hr) )
{
updateStatus("Cannot create the trigger: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} ILogonTrigger *pLogonTrigger = NULL;
hr = pTrigger->QueryInterface(
IID_ILogonTrigger, (void**) &pLogonTrigger );
pTrigger->Release();
if( FAILED(hr) )
{
updateStatus("QueryInterface call failed for ILogonTrigger: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} hr = pLogonTrigger->put_Id( _bstr_t( L"Trigger1" ) );
if( FAILED(hr) )
updateStatus("Cannot put the trigger ID: " + QString("%1").arg(hr,,,QLatin1Char(''))); // Set the task to start at a certain time. The time
// format should be YYYY-MM-DDTHH:MM:SS(+-)(timezone).
// For example, the start boundary below
// is January 1st 2005 at 12:05
/*hr = pLogonTrigger->put_StartBoundary( _bstr_t(L"2005-01-01T12:05:00") );
if( FAILED(hr) )
updateStatus("Cannot put the start boundary: " + QString("%1").arg(hr,8,16,QLatin1Char('0'))); hr = pLogonTrigger->put_EndBoundary( _bstr_t(L"2025-05-02T08:00:00") );
if( FAILED(hr) )
updateStatus("Cannot put the end boundary: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));*/ // Define the user. The task will execute when the user logs on.
// The specified user must be a user on this computer.
//hr = pLogonTrigger->put_UserId( _bstr_t( L"DOMAIN\\UserName" ) );
//hr = pLogonTrigger->put_UserId( _bstr_t( L"JOSHDESKTOP10\\Josh" ) );
hr = pLogonTrigger->put_UserId( _bstr_t( L"Josh" ) );
pLogonTrigger->Release();
if( FAILED(hr) )
{
updateStatus("Cannot add user ID to logon trigger: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} // ------------------------------------------------------
// Add an Action to the task. This task will execute notepad.exe.
IActionCollection *pActionCollection = NULL; // Get the task action collection pointer.
hr = pTask->get_Actions( &pActionCollection );
if( FAILED(hr) )
{
updateStatus("Cannot get Task collection pointer: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} // Create the action, specifying that it is an executable action.
IAction *pAction = NULL;
hr = pActionCollection->Create( TASK_ACTION_EXEC, &pAction );
pActionCollection->Release();
if( FAILED(hr) )
{
updateStatus("Cannot create the action: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} IExecAction *pExecAction = NULL;
// QI for the executable task pointer.
hr = pAction->QueryInterface(
IID_IExecAction, (void**) &pExecAction );
pAction->Release();
if( FAILED(hr) )
{
updateStatus("QueryInterface call failed for IExecAction: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} // Set the path of the executable to notepad.exe.
hr = pExecAction->put_Path( _bstr_t( wstrExecutablePath.c_str() ) );
pExecAction->Release();
if( FAILED(hr) )
{
updateStatus("Cannot set path of executable: " + QString("%1").arg(hr,,,QLatin1Char('')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} // ------------------------------------------------------
// Save the task in the root folder.
IRegisteredTask *pRegisteredTask = NULL; /*hr = pRootFolder->RegisterTaskDefinition(
_bstr_t( wszTaskName ),
pTask,
TASK_CREATE_OR_UPDATE,
//_variant_t(L"Builtin\\Administrators"),
_variant_t(L"Josh"),
_variant_t(),
TASK_LOGON_PASSWORD,
_variant_t(L""),
&pRegisteredTask);*/ hr = pRootFolder->RegisterTaskDefinition(
_bstr_t( wszTaskName ),
pTask,
TASK_CREATE_OR_UPDATE,
//_variant_t(L"Builtin\\Administrators"),
_variant_t(),
_variant_t(),
TASK_LOGON_GROUP,
_variant_t(L""),
&pRegisteredTask); if( FAILED(hr) )
{
switch (hr)
{
case E_ACCESSDENIED:
code = "E_ACCESSDENIED";
break;
case E_OUTOFMEMORY:
code = "E_OUTOFMEMORY";
break;
case SCHED_S_BATCH_LOGON_PROBLEM:
code = "SCHED_S_BATCH_LOGON_PROBLEM";
break;
case SCHED_S_SOME_TRIGGERS_FAILED:
code = "SCHED_S_SOME_TRIGGERS_FAILED";
break;
} updateStatus("Error saving the Task : " + QString("%1").arg(hr,,,QLatin1Char('')) + code);
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
} updateStatus(" Success! Task successfully registered. " ); // Clean up
pRootFolder->Release();
pTask->Release();
pRegisteredTask->Release();
CoUninitialize();
}

Finally solved this problem after lots of troubleshooting... the answer is simple: set UserId to the appropriate user, in my case "Josh", you can use the Win32 API getusername function if you need to

hr = pLogonTrigger->put_UserId( _bstr_t( L"Josh" ) );

and then for RegisterTaskDefinition do this:

VARIANT varPassword;
varPassword.vt = VT_EMPTY;
hr = pRootFolder->RegisterTaskDefinition(
_bstr_t( wszTaskName ),
pTask,
TASK_CREATE_OR_UPDATE,
_variant_t(L"Builtin\\Administrators"),
varPassword,
TASK_LOGON_GROUP,
_variant_t(L""),
&pRegisteredTask);

So the userID parameter for RegisterTaskDefinition needs to be _variant_t(L"Builtin\Administrators") and the logonType needs to be TASK_LOGON_GROUP

So the main point is the two UserId values aren't necessarily the same.

 

Task Scheduler API Error 80041318的更多相关文章

  1. Task Scheduler Error Message: 80041318

    Using the error lookup tool that comes with VC++ (errlook.exe, or "Error Lookup" on the To ...

  2. Windows Task Scheduler Fails With Error Code 2147943785

    Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a ...

  3. Task Scheduler Error and Success Constants (Windows)

    If an error occurs, the Task Scheduler APIs can return one of the following error codes as an HRESUL ...

  4. iOS之There was an internal API error错误

    There was an internal API error. 错误原因:把Product Name作为程序名称,程序名称错乱 解决方法:检查Product Name, 不要包含中文以及特殊字符.在 ...

  5. There was an internal API error.的解决办法

    1.当安装应用到4s时,偶尔有些程序会报这个错误,There was an internal API error 解决办法如下: 真机运行下,项目在iOS8.0下运行正常,但是一旦换到iOS7.0和7 ...

  6. 在Windows Server 2012的Task Scheduler里面配置自动发送邮件

    最近在一台server上配置了每个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果. 悲剧地发现Send an email功能被新版的server给禁 ...

  7. Xcode真机调试中"There was an internal API error"错误解决方法

    xcode7更新之后使用真机调试,在IOS8的一台iphone6也没问题.IOS9.2的一台iphone6s也没问题.但是在IOS7.0的一台iPhone4s上面在最后安装的时候居然安装失败,提示 T ...

  8. ERROR (ClientException): Unexpected API Error

  9. 使用C#创建计划任务(How to create a Task Scheduler use C# )

    本文主要讲解了如何使用C#来创建windows计划任务. 需求:在不定时间段运行多个后台程序(winfrom,wpf,console,等等)用于更新数据.  问题:为什么要使用计划任务,而不直接在程序 ...

随机推荐

  1. MATLAB 一维随机变量及其概率分布

    1.两点分布 clc clear a=rand(1,10); for ii=1:10 if a(ii)<0.2 a(ii)=0; else a(ii)=1; end end a x=0的概率为0 ...

  2. 1025 PAT Ranking (25 分)

    Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhe ...

  3. Scrapy-01-追踪爬取

    目的:利用scrapy完成盗墓笔记小说的抓取 创建项目: scrapy   startproject    books cd  books scrapy   genspider    dmbj 编写p ...

  4. MTK Android 设置下添加一级菜单[ZedielPcbTest]

    功能描述:Android7.1.2 设置下添加一级菜单[ZedielPcbTest],点击ZedielPcbTest,启动ZedielPcbTest.apk应用. 编译:需要在out目录删除Settt ...

  5. MySQL入门,第二部分,必备基础知识点

    一.数据类型 日期和时间数据类型 date 字节 日期,格式:2014-09-18 日期和时间数据类型 time 字节 时间,格式:08:42:30 日期和时间数据类型 datetime 字节 日期时 ...

  6. 使用nginx转换HTTPS流量

    背景 公司卖了一个产品给甲方,甲方要求部署后,要以https来访问.甲方提供了证书信息和私钥,记录一下部署过程. 实现 1.思路 在我们产品服务器上部署一个nginx.证书信息也放在这个服务器上.外界 ...

  7. Quil-delta-enhanced 简介

    Quill 是一款时下非常热门的富文本编辑器,它拥有非常强大的扩展能力,可以让开发者根据自己的需要编写插件,使编辑器支持的内容类型更加丰富.它之所以能够拥有这么强大的扩展能力,一方面是因为它的架构和 ...

  8. 讲讲HashMap的理解,以及HashMap在1.7和1.8版本的变化(2020/4/16)

    HashMap的适用场景,作用,优缺点

  9. Floyd-例题-实现-我的第一篇博客

    https://www.cnblogs.com/lbssxz/p/11014911.html 这是网上看到的题目,以上是原博主的解答和题目来源(没找到别的题目来源) 题目大意: 城市交通费 [问题描述 ...

  10. matlab将数据读取和写入txt文档

    原文链接 matlab中打开文件 fid = fopen(文件名,‘打开方式’): 说明:fid用于存储文件句柄值,如果fid>0,这说明文件打开成功. 另外,在这些字符串后添加一个“t”,如‘ ...