I was trying to run the sample program, taken from the Microsoft docs. Seems the is being created (.job
file found in the C:WindowsTasks
) but it is not listed on Task schedular GUI.
Is this related to interface 1.0?
EDIT:
In addition to the sample program added TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
flag and set the account using SetAccountInformation
.
void stringError(HRESULT hr) {
_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();
fprintf(stderr, "Error (%ls) : ", errMsg);
}
int main()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to initialize COM library, error = 0x%xn", hr);
return 1;
}
ITaskScheduler* pITS = NULL;
hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, (void**)&pITS);
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to create Task Scheduler instance, error = 0x%xn", hr);
CoUninitialize();
return 1;
}
LPCWSTR pwszTaskName = L"SampleTask";
ITask* pITask = NULL;
IPersistFile* pIPersistFile = NULL;
hr = pITS->NewWorkItem(pwszTaskName, CLSID_CTask, IID_ITask, (IUnknown**)&pITask);
if (FAILED(hr))
{
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS))
{
fprintf(stderr, "Task already exists. Attempting to delete...n");
hr = pITS->Delete(pwszTaskName);
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to delete existing task, error = 0x%xn", hr);
pITS->Release();
CoUninitialize();
return 1;
}
hr = pITS->NewWorkItem(pwszTaskName, CLSID_CTask, IID_ITask, (IUnknown**)&pITask);
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to create new task after deletion, error = 0x%xn", hr);
pITS->Release();
CoUninitialize();
return 1;
}
}
else
{
stringError(hr);
fprintf(stderr, "Failed to create new task, error = 0x%xn", hr);
pITS->Release();
CoUninitialize();
return 1;
}
}
pITS->Release();
hr = pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to set task flags, error = 0x%xn", hr);
pITask->Release();
CoUninitialize();
return 1;
}
hr = pITask->SetAccountInformation(L"", NULL);
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to set account information, error = 0x%xn", hr);
pITask->Release();
CoUninitialize();
return 1;
}
hr = pITask->QueryInterface(IID_IPersistFile, (void**)&pIPersistFile);
pITask->Release();
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to query IPersistFile interface, error = 0x%xn", hr);
CoUninitialize();
return 1;
}
hr = pIPersistFile->Save(NULL, TRUE);
pIPersistFile->Release();
if (FAILED(hr))
{
stringError(hr);
fprintf(stderr, "Failed to save the task, error = 0x%xn", hr);
CoUninitialize();
return 1;
}
CoUninitialize();
printf("Task created successfully.n");
return 0;
}
Issue: Unable to save the task. Prints the following error – (The task XML contains an unexpected node.) : Failed to save the task, error = 0x80041316.
Task Scheduler 1.0 API uses C:WindowsTasks
folder to create and enumerate tasks.
Task Scheduler 2.0 API uses C:WindowsSystem32Tasks
folder to create and enumerate tasks.
It is the reason that the task is not listed on Task schedular GUI.
If you want to use Task Scheduler 1.0 API to create tasks and show the task on Task schedular GUI, you could try to set flag TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
and set AccountInformation
.
For more details, you could refer to the thread: /a/9782931
4