Task Scheduler API Error 80041318
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的更多相关文章
- Task Scheduler Error Message: 80041318
Using the error lookup tool that comes with VC++ (errlook.exe, or "Error Lookup" on the To ...
- Windows Task Scheduler Fails With Error Code 2147943785
Problem: Windows Task Scheduler Fails With Error Code 2147943785 Solution: This is usually due to a ...
- 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 ...
- iOS之There was an internal API error错误
There was an internal API error. 错误原因:把Product Name作为程序名称,程序名称错乱 解决方法:检查Product Name, 不要包含中文以及特殊字符.在 ...
- There was an internal API error.的解决办法
1.当安装应用到4s时,偶尔有些程序会报这个错误,There was an internal API error 解决办法如下: 真机运行下,项目在iOS8.0下运行正常,但是一旦换到iOS7.0和7 ...
- 在Windows Server 2012的Task Scheduler里面配置自动发送邮件
最近在一台server上配置了每个周末备份数据库的定时任务,想顺手配置发送备份完成的邮件提醒我去Double Check一下备份结果. 悲剧地发现Send an email功能被新版的server给禁 ...
- Xcode真机调试中"There was an internal API error"错误解决方法
xcode7更新之后使用真机调试,在IOS8的一台iphone6也没问题.IOS9.2的一台iphone6s也没问题.但是在IOS7.0的一台iPhone4s上面在最后安装的时候居然安装失败,提示 T ...
- ERROR (ClientException): Unexpected API Error
- 使用C#创建计划任务(How to create a Task Scheduler use C# )
本文主要讲解了如何使用C#来创建windows计划任务. 需求:在不定时间段运行多个后台程序(winfrom,wpf,console,等等)用于更新数据. 问题:为什么要使用计划任务,而不直接在程序 ...
随机推荐
- Jedis连接外部Redis
Jedis连接外部Redis 1.在服务器开放端口redis默认6379,如果有宝塔面板则还需要在宝塔放行6379端口 2.修改redis.conf 注释掉 绑定IP 127.0.0.1 # bind ...
- python 开发工具简介
一.python 开发工具简介 1.IDLE IDLE是开发python程序的基本IDE(集成开发环境),具备基本的IDE的功能,是非商业Python开发的不错的选择.当安装好python以后,IDL ...
- 【nodejs 爬虫】使用 puppeteer 爬取链家房价信息
使用 puppeteer 爬取链家房价信息 目录 使用 puppeteer 爬取链家房价信息 页面结构 爬虫库 pupeteer 库 实现 打开待爬页面 遍历区级页面 方法一 方法二 遍历街道页面 遍 ...
- 用全站 CDN 部署 Discourse 论坛
Discourse 介绍 Discourse 是一款由 Stack Overflow 的联合创始人--Jeff Atwood,基于 Ruby on Rails 开发的开源论坛.相较于传统论坛,Disc ...
- 使用Putty + Xming 远程登录Linux显示图形化界面
一般我们远程登录linux 服务器,都是使用非加密的 telnet 或者加密的 ssh.这些登录方式有一个特点:只能登录字符界面,不能运行那些带有GUI界面的程序. 有时候为了学习或者工作的需要,我们 ...
- 1055 The World's Richest (25分)(水排序)
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- 【JAVA进阶架构师指南】之四:垃圾回收GC
前言 在[JAVA进阶架构师指南]系列二和三中,我们了解了JVM的内存模型以及类加载机制,其中在内存模型中,我们说到,从线程角度来说,JVM分为线程私有的区域(虚拟机栈/本地方法栈/程序计数器)和 ...
- 谈谈CSS3中display属性的Flex布局(弹性布局)
最近为了微信小程序,回忆起之前有接触过的弹性布局(display:Flex) 网上摘录的原文地址:https://www.cnblogs.com/xuyuntao/articles/6391728.h ...
- python爬虫两个影院的实例
主要两个的python代码如下: import requests from bs4 import BeautifulSoup url = 'https://www.17k.com/' headers ...
- sqlchemy查询的其他操作
sqlalchemy的数据查询排序 1 .正序排序:session.query(model).order_by(model.attr).all() session.query(model).order ...