I'm trying to copy a system RegIdleBackup task and launch the copied task. I successfully copied a task with ITaskService API, and change some settings in it. After I tried to run this task, I got return code S_OK, but the task info didn't changed in Task Scheduler window and backup files didn't update.
I can manually run this task from task scheduler and my copied task works good. My problem is with the launch method, but I can't find it.
Here is my code
HRESULT GetTask(ITaskService* pService, const std::wstring& fullTaskName, IRegisteredTask** pTask)
{
ITaskFolder* pFolder = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
if (FAILED(hr))
return hr;
hr = pFolder->GetTask(SysAllocString(L"RegIdleBackup"), pTask);
pFolder->Release();
return hr;
}
HRESULT CreateTaskCopy(ITaskService* pService, IRegisteredTask* pTask, const std::wstring& fullTaskName)
{
ITaskFolder* pFolder = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
ITaskDefinition* pTaskDef = nullptr;
ITaskSettings* pTaskSettings = nullptr;
IRegisteredTask* iTask = nullptr;
hr = pTask->get_Definition(&pTaskDef);
if (FAILED(hr))
goto exit;
hr = pTaskDef->get_Settings(&pTaskSettings);
if (FAILED(hr))
goto exit;
hr = pTaskSettings->put_AllowDemandStart(_variant_t(TRUE));
if (FAILED(hr))
goto exit;
hr = pTaskSettings->put_MultipleInstances(TASK_INSTANCES_PARALLEL);
if (FAILED(hr))
goto exit;
hr = pTaskDef->put_Settings(pTaskSettings);
if (FAILED(hr))
goto exit;
pTaskSettings->Release();
pTaskSettings = nullptr;
hr = pFolder->DeleteTask(_bstr_t(taskName.c_str()), 0);
hr = pFolder->RegisterTaskDefinition(_bstr_t(taskName.c_str()),
pTaskDef,
TASK_CREATE_OR_UPDATE,
_variant_t(),
_variant_t(),
TASK_LOGON_SERVICE_ACCOUNT,
_variant_t(L""),
&iTask);
exit:
if (pTaskSettings)
pTaskSettings->Release();
if (iTask)
iTask->Release();
if (pTaskDef)
pTaskDef->Release();
return hr;
}
HRESULT CopyTask(const std::wstring& fullTaskName, const std::wstring& copyName)
{
ITaskService* pService = nullptr;
IRegisteredTask* pTask = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
hr = GetTask(pService, fullTaskName, &pTask);
if (FAILED(hr))
goto exit;
hr = CreateTaskCopy(pService, pTask, copyName);
exit:
if(pService)
pService->Release();
if (pTask)
pTask->Release();
return hr;
}
HRESULT StartTask(const std::wstring& fullTaskName)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pTask = nullptr;
pFolder->GetTask(_bstr_t(taskName.c_str()), &pTask);
pFolder->Release();
IRunningTask* pRunningTask = nullptr;
hr = pTask->RunEx(_variant_t(),0,0, _bstr_t(L"S-1-5-18"), &pRunningTask);
pTask->Release();
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
return hr;
hr = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, 0, nullptr);
if (FAILED(hr))
return hr;
hr = CopyTask(L"\\Microsoft\\Windows\\Registry\\RegIdleBackup", L"\\Microsoft\\Windows\\Registry\\MyTask");
hr = StartTask(L"\\Microsoft\\Windows\\Registry\\MyTask");
CoUninitialize();
return hr;
}
Today I tried to run task with replacing launch trigger, and at the same time fixed some task settings.
I started to test my program on Win 7 and all was great, but on Win 10 I got an interesting issue; the task created with another last launch time (on Win7 the field was empty, on Win 10 I got the message - "Task never launched before") and I tried to use the IRegisteredTask::Run
method, and it worked!
I tested on other OSes and the run method is working.
HRESULT GetTaskDefinition(ITaskService* pService, const std::wstring& fullTaskName, ITaskDefinition** pTask)
{
ITaskFolder* pFolder = nullptr;
IRegisteredTask* pRegTask = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
if (FAILED(hr))
return hr;
hr = pFolder->GetTask(SysAllocString(L"RegIdleBackup"), &pRegTask);
pFolder->Release();
if (FAILED(hr))
return hr;
hr = pRegTask->get_Definition(pTask);
return hr;
}
HRESULT ChangeTaskSettings(ITaskDefinition* pTaskDef)
{
ITaskSettings* pTaskSettings = nullptr;
HRESULT hr = pTaskDef->get_Settings(&pTaskSettings);
if (FAILED(hr))
return hr;
hr = pTaskSettings->put_AllowDemandStart(VARIANT_TRUE);
hr = pTaskSettings->put_MultipleInstances(TASK_INSTANCES_PARALLEL);
hr = pTaskSettings->put_RunOnlyIfIdle(VARIANT_FALSE);
hr = pTaskSettings->put_StartWhenAvailable(VARIANT_TRUE);
hr = pTaskSettings->put_StopIfGoingOnBatteries(VARIANT_FALSE);
hr = pTaskSettings->put_Enabled(VARIANT_FALSE);
hr = pTaskSettings->put_DisallowStartIfOnBatteries(VARIANT_FALSE);
hr = pTaskDef->put_Settings(pTaskSettings);
pTaskSettings->Release();
return hr;
}
FILETIME AddSecondsToFIleTime(FILETIME ft, unsigned seconds)
{
FILETIME res;
#define _SECONDS ((__int64) 10000000) //100 ns intervals in second
ULONGLONG tmp = (static_cast<ULONGLONG>(ft.dwHighDateTime) << 32) + ft.dwLowDateTime + seconds*_SECONDS;
res.dwLowDateTime = static_cast<DWORD>(tmp & 0xFFFFFFFF);
res.dwHighDateTime = static_cast<DWORD>(tmp >> 32);
return res;
}
std::wstring GetTaskLaunchTimeWstring(SYSTEMTIME st, unsigned wait_interval)
{
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
ft = AddSecondsToFIleTime(ft, wait_interval);
FileTimeToSystemTime(&ft, &st);
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << st.wYear << "-" <<
std::setfill('0') << std::setw(2) << st.wMonth << "-" <<
std::setfill('0') << std::setw(2) << st.wDay << "T" <<
std::setfill('0') << std::setw(2) << st.wHour << ":" <<
std::setfill('0') << std::setw(2) << st.wMinute << ":" <<
std::setfill('0') << std::setw(2) << st.wSecond;
std::string str = ss.str();
return std::wstring(str.begin(), str.end());
}
HRESULT ChangeTaskTriggers(ITaskDefinition* pTaskDef, const std::wstring& LaunchTimeStr)
{
ITriggerCollection* pTriggersCollection = nullptr;
HRESULT hr = pTaskDef->get_Triggers(&pTriggersCollection);
hr = pTriggersCollection->Clear();
ITrigger* pTrigger = nullptr;
hr = pTriggersCollection->Create(TASK_TRIGGER_TIME, &pTrigger);
pTriggersCollection->Release();
if (FAILED(hr))
return hr;
ITimeTrigger* pTimeTrigger = nullptr;
hr = pTrigger->QueryInterface(IID_ITimeTrigger, reinterpret_cast<void**>(&pTimeTrigger));
pTrigger->Release();
if (FAILED(hr))
return hr;
hr = pTimeTrigger->put_Id(_bstr_t(L"Trigger"));
hr = pTimeTrigger->put_Enabled(VARIANT_TRUE);
hr = pTimeTrigger->put_StartBoundary(_bstr_t(LaunchTimeStr.c_str()));
pTimeTrigger->Release();
return hr;
}
HRESULT RegisterTask(ITaskService* pService, ITaskDefinition* pTaskDef, const std::wstring& fullTaskName)
{
ITaskFolder* pFolder = nullptr;
HRESULT hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
return hr;
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pRegisteredTAsk = nullptr;
hr = pFolder->DeleteTask(_bstr_t(taskName.c_str()), 0);
hr = pFolder->RegisterTaskDefinition(_bstr_t(taskName.c_str()),
pTaskDef,
TASK_CREATE_OR_UPDATE,
_variant_t(),
_variant_t(),
TASK_LOGON_SERVICE_ACCOUNT,
_variant_t(L""),
&pRegisteredTAsk);
if (SUCCEEDED(hr))
{
pRegisteredTAsk->put_Enabled(VARIANT_TRUE);
pRegisteredTAsk->Release();
}
return hr;
}
HRESULT CreateModifiedTaskCopy(const std::wstring& fullTaskName, const std::wstring& copyName, unsigned seconds_to_start)
{
ITaskService* pService = nullptr;
ITaskDefinition* pTaskDef = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
hr = GetTaskDefinition(pService, fullTaskName, &pTaskDef);
if (FAILED(hr))
{
pService->Release();
return hr;
}
hr = ChangeTaskSettings(pTaskDef);
SYSTEMTIME st;
GetLocalTime(&st);
const std::wstring launchStr = GetTaskLaunchTimeWstring(st, seconds_to_start);
hr = ChangeTaskTriggers(pTaskDef, launchStr);
hr = RegisterTask(pService, pTaskDef, copyName);
pTaskDef->Release();
pService->Release();
return hr;
}
HRESULT WaitUntilTaskCompleted(const std::wstring& fullTaskName, unsigned taskTimeout, unsigned maxWaitTime)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pTask = nullptr;
hr = pFolder->GetTask(_bstr_t(taskName.c_str()), &pTask);
if (FAILED(hr))
{
pFolder->Release();
return hr;
}
DATE lastRun;
unsigned tries = 0;
do
{
Sleep(1000);
hr = pTask->get_LastRunTime(&lastRun);
tries++;
if (tries > maxWaitTime)
break;
} while (hr == SCHED_S_TASK_HAS_NOT_RUN);
return tries > maxWaitTime? SCHED_E_INVALID_TASK :S_OK;
}
HRESULT DeleteTask(const std::wstring& fullTaskName)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
hr = pFolder->DeleteTask(_bstr_t(taskName.c_str()), 0);
pFolder->Release();
return hr;
}
HRESULT RunTask(const std::wstring& fullTaskName)
{
ITaskService* pService = nullptr;
HRESULT hr = CoCreateInstance(CLSID_TaskScheduler, nullptr, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr))
return hr;
pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr))
{
pService->Release();
return hr;
}
const std::wstring taskFolder = fullTaskName.substr(0, fullTaskName.find_last_of(L'\\'));
const std::wstring taskName = fullTaskName.substr(fullTaskName.find_last_of(L'\\') + 1);
ITaskFolder* pFolder = nullptr;
hr = pService->GetFolder(_bstr_t(taskFolder.c_str()), &pFolder);
pService->Release();
if (FAILED(hr))
return hr;
IRegisteredTask* pTask = nullptr;
hr = pFolder->GetTask(_bstr_t(taskName.c_str()), &pTask);
pFolder->Release();
IRunningTask* pRunTask = nullptr;
pTask->Run(_variant_t(), &pRunTask);
pTask->Release();
pRunTask->Release();
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
return hr;
hr = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, 0, nullptr);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
const unsigned seconds_to_start = 30;
hr = CreateModifiedTaskCopy(L"\\Microsoft\\Windows\\Registry\\RegIdleBackup", L"\\Microsoft\\Windows\\Registry\\MyTask", seconds_to_start);
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
RunTask(L"\\Microsoft\\Windows\\Registry\\MyTask");
hr = WaitUntilTaskCompleted(L"\\Microsoft\\Windows\\Registry\\MyTask", seconds_to_start, seconds_to_start * 4);
hr = DeleteTask(L"\\Microsoft\\Windows\\Registry\\MyTask");
CoUninitialize();
printf("All ok!\r\n");
return hr;
}