Reputation: 567
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.
*Edited Code Added
void Replicator::taskCreate()
{
/*QProcess *taskProcess = new QProcess(this);
QString program = "schtasks.exe";
QStringList arguments;
arguments << "/create" << "/XML" << "rep.xml" << "/tn" << "task";
taskProcess->start(program, arguments);*/
// ------------------------------------------------------
// Initialize COM.
QString code;
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if( FAILED(hr) )
{
//updateStatus("\nCoInitializeEx failed: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
switch (hr)
{
case S_OK:
code = "S_OK";
break;
case S_FALSE:
code = "S_FALSE";
break;
case RPC_E_CHANGED_MODE:
code = "RPC_E_CHANGED_MODE";
break;
}
updateStatus("CoInitializeEx failed: " + QString("%1").arg(hr,8,16,QLatin1Char('0')) + code);
return;
}
// Set general COM security levels.
/* hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
0,
NULL);
if( FAILED(hr) )
{
switch (hr)
{
case S_OK:
code = "S_OK";
break;
case RPC_E_TOO_LATE:
code = "RPC_E_TOO_LATE";
break;
case RPC_E_NO_GOOD_SECURITY_PACKAGES:
code = "RPC_E_NO_GOOD_SECURITY_PACKAGES";
break;
//case E_OUTOFMEMORY:
// code = "E_OUTOFMEMORY";
// break;
}
updateStatus("CoInitializeSecurity failed: " + code);
CoUninitialize();
return;
}*/
// ------------------------------------------------------
// Create a name for the task.
LPCWSTR wszTaskName = L"Jerp";
// Get the windows directory and set the path to notepad.exe.
wstring wstrExecutablePath = _wgetenv( L"WINDIR");
wstrExecutablePath += L"\\SYSTEM32\\NOTEPAD.EXE";
// ------------------------------------------------------
// Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService );
if (FAILED(hr))
{
updateStatus("Failed to create an instance of ITaskService: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
CoUninitialize();
return;
}
// Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if( FAILED(hr) )
{
updateStatus("ITaskService::Connect failed: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
pService->Release();
CoUninitialize();
return;
}
// ------------------------------------------------------
// Get the pointer to the root task folder. This folder will hold the
// new task that is registered.
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder( _bstr_t( L"\\") , &pRootFolder );
if( FAILED(hr) )
{
updateStatus("Cannot get Root Folder pointer: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
pService->Release();
CoUninitialize();
return;
}
// If the same task exists, remove it.
pRootFolder->DeleteTask( _bstr_t( wszTaskName), 0 );
// Create the task builder object to create the task.
ITaskDefinition *pTask = NULL;
hr = pService->NewTask( 0, &pTask );
pService->Release(); // COM clean up. Pointer is no longer used.
if (FAILED(hr))
{
updateStatus("Failed to create a task definition: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
pRootFolder->Release();
CoUninitialize();
return;
}
// ------------------------------------------------------
// Get the registration info for setting the identification.
IRegistrationInfo *pRegInfo= NULL;
hr = pTask->get_RegistrationInfo( &pRegInfo );
if( FAILED(hr) )
{
updateStatus("Cannot get identification pointer: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
//hr = pRegInfo->put_Author(L"Author Name");
BSTR authorName = SysAllocString(L"Josh");
hr = pRegInfo->put_Author(authorName);
pRegInfo->Release();
if( FAILED(hr) )
{
updateStatus("Cannot put identification info: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// ------------------------------------------------------
// Create the settings for the task
/*ITaskSettings *pSettings = NULL;
hr = pTask->get_Settings( &pSettings );
if( FAILED(hr) )
{
updateStatus("Cannot get settings pointer: " + QString("%1").arg(hr,8,16,QLatin1Char('0')));
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// 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();
}
Upvotes: 0
Views: 781
Reputation: 567
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.
Upvotes: 1