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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); // 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0'))); 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,8,16,QLatin1Char('0')) + 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.
原文地址:https://www.cnblogs.com/liujx2019/p/11812056.html
- 系统进程管理工具Process Explorer
- jquery 操作DOM元素(1)
- 开源的读取Excel文件组件-ExcelDataReader
- BlackPearl 的 ServiceObject 开发部署
- [程序设计语言]-[核心概念]-04:数据类型
- jquery 筛选元素(1)
- [程序设计语言]-00:目录
- 使用 SQL Server 2008 数据类型-xml 字段类型参数进行数据的批量选取或删除数据
- jquery 筛选元素 (2)
- 专家:中国还不是网络强国 今后须打破国外垄断
- jquery 筛选元素 (3)
- ASP.NET安全隐患的临时解决方法
- 实用代码-C#之IP地址和整数的互转
- jquery操作元素的位置
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法