Hi
I am trying to schedule a task with the help of below program;
After executing the program in scheduled Task lists , the defined task is present;
But, it is not running when trigger condition is reached. Please advice me in this
#include <windows.h>#include <initguid.h>#include <ole2.h>#include <mstask.h>#include <msterr.h>#include <wchar.h>#include<stdio.h>#include<iostream>#pragma comment(lib, "Mstask.lib")#pragma comment(lib, "ole32.lib")using namespace std;int main(int argc, char **argv){ HRESULT hr = S_OK; ITaskScheduler *pITS; /////////////////////////////////////////////////////////////////// // Call CoInitialize to initialize the COM library and then // CoCreateInstance to get the Task Scheduler object. /////////////////////////////////////////////////////////////////// hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, (void **) &pITS); if (FAILED(hr)) { CoUninitialize(); cout<<"Failed to create object"; return 1; } else { cout<<"Object created successfully"; } } else { return 1; } /////////////////////////////////////////////////////////////////// // Call ITaskScheduler::NewWorkItem to get a new Task object. /////////////////////////////////////////////////////////////////// LPCWSTR pwszTaskName; ITask *pITask; pwszTaskName = L"Test Task15"; hr = pITS->NewWorkItem(pwszTaskName, CLSID_CTask, IID_ITask, (IUnknown**)&pITask); if (FAILED(hr)) { wprintf(L"Failed calling ITaskScheduler::NewWorkItem: "); wprintf(L"error = 0x%x\n",hr); CoUninitialize(); return 1; } else { cout<<"Newworkitem created task successfully"; } /////////////////////////////////////////////////////////////////// // Set the tasks application name. /////////////////////////////////////////////////////////////////// LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe"; cout<<"Application name",pwszApplicationName; hr = pITask->SetApplicationName(pwszApplicationName); if (FAILED(hr)) { wprintf(L"Failed calling ITask::SetApplicationName: "); cout<<"Setting application name failed"; wprintf(L"error = 0x%x\n",hr); pITS->Release(); pITask->Release(); CoUninitialize(); return 1; } else { cout<<"Application name was set successfully"; } pITask->SetAccountInformation(L"USERNAME", NULL); pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON); // creating a triggerITaskTrigger* pITaskTrig = NULL;TASK_TRIGGER rTrigger;WORD wTrigNumber = 0;hr = pITask->CreateTrigger ( &wTrigNumber, &pITaskTrig );//filling the TASK_TRIGGER structureZeroMemory ( &rTrigger, sizeof (TASK_TRIGGER) );rTrigger.cbTriggerSize = sizeof (TASK_TRIGGER);rTrigger.wBeginYear = 2008;rTrigger.wBeginMonth = 12;rTrigger.wBeginDay = 30;rTrigger.wStartHour = 12;rTrigger.wStartMinute = 30;// associate the trigger with the taskrTrigger.TriggerType = TASK_TIME_TRIGGER_ONCE;hr = pITaskTrig->SetTrigger ( &rTrigger );if(FAILED(hr)){ cout<<"Trigger association failed";}else{ cout<<"Successfully trigger associated to task";}/////////////////////////////////////////////////////////////////// // Add the task to the sceduler /////////////////////////////////////////////////////////////////// pITS->AddWorkItem(pwszTaskName, pITask); /////////////////////////////////////////////////////////////////// // Call ITask::Run to start execution of "Test Task". /////////////////////////////////////////////////////////////////// hr = pITask->Run(); if (FAILED(hr)) { wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr); cout<<"Failed to execute the task"; pITask->Release(); CoUninitialize(); return 1; } else { cout<<"Successful execution of task"; } pITS->Release(); // Release sceduler pITask->Release(); //reselase task pITaskTrig->Release();//Release trigger return 0;}
I am trying to schedule a task with the help of below program;
After executing the program in scheduled Task lists , the defined task is present;
But, it is not running when trigger condition is reached. Please advice me in this
#include <windows.h>#include <initguid.h>#include <ole2.h>#include <mstask.h>#include <msterr.h>#include <wchar.h>#include<stdio.h>#include<iostream>#pragma comment(lib, "Mstask.lib")#pragma comment(lib, "ole32.lib")using namespace std;int main(int argc, char **argv){ HRESULT hr = S_OK; ITaskScheduler *pITS; /////////////////////////////////////////////////////////////////// // Call CoInitialize to initialize the COM library and then // CoCreateInstance to get the Task Scheduler object. /////////////////////////////////////////////////////////////////// hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, (void **) &pITS); if (FAILED(hr)) { CoUninitialize(); cout<<"Failed to create object"; return 1; } else { cout<<"Object created successfully"; } } else { return 1; } /////////////////////////////////////////////////////////////////// // Call ITaskScheduler::NewWorkItem to get a new Task object. /////////////////////////////////////////////////////////////////// LPCWSTR pwszTaskName; ITask *pITask; pwszTaskName = L"Test Task15"; hr = pITS->NewWorkItem(pwszTaskName, CLSID_CTask, IID_ITask, (IUnknown**)&pITask); if (FAILED(hr)) { wprintf(L"Failed calling ITaskScheduler::NewWorkItem: "); wprintf(L"error = 0x%x\n",hr); CoUninitialize(); return 1; } else { cout<<"Newworkitem created task successfully"; } /////////////////////////////////////////////////////////////////// // Set the tasks application name. /////////////////////////////////////////////////////////////////// LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe"; cout<<"Application name",pwszApplicationName; hr = pITask->SetApplicationName(pwszApplicationName); if (FAILED(hr)) { wprintf(L"Failed calling ITask::SetApplicationName: "); cout<<"Setting application name failed"; wprintf(L"error = 0x%x\n",hr); pITS->Release(); pITask->Release(); CoUninitialize(); return 1; } else { cout<<"Application name was set successfully"; } pITask->SetAccountInformation(L"USERNAME", NULL); pITask->SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON); // creating a triggerITaskTrigger* pITaskTrig = NULL;TASK_TRIGGER rTrigger;WORD wTrigNumber = 0;hr = pITask->CreateTrigger ( &wTrigNumber, &pITaskTrig );//filling the TASK_TRIGGER structureZeroMemory ( &rTrigger, sizeof (TASK_TRIGGER) );rTrigger.cbTriggerSize = sizeof (TASK_TRIGGER);rTrigger.wBeginYear = 2008;rTrigger.wBeginMonth = 12;rTrigger.wBeginDay = 30;rTrigger.wStartHour = 12;rTrigger.wStartMinute = 30;// associate the trigger with the taskrTrigger.TriggerType = TASK_TIME_TRIGGER_ONCE;hr = pITaskTrig->SetTrigger ( &rTrigger );if(FAILED(hr)){ cout<<"Trigger association failed";}else{ cout<<"Successfully trigger associated to task";}/////////////////////////////////////////////////////////////////// // Add the task to the sceduler /////////////////////////////////////////////////////////////////// pITS->AddWorkItem(pwszTaskName, pITask); /////////////////////////////////////////////////////////////////// // Call ITask::Run to start execution of "Test Task". /////////////////////////////////////////////////////////////////// hr = pITask->Run(); if (FAILED(hr)) { wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr); cout<<"Failed to execute the task"; pITask->Release(); CoUninitialize(); return 1; } else { cout<<"Successful execution of task"; } pITS->Release(); // Release sceduler pITask->Release(); //reselase task pITaskTrig->Release();//Release trigger return 0;}
Comment