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.