Current location: Hot Scripts Forums » Programming Languages » Other Languages » Doubts about Task Scheduler in windows


Doubts about Task Scheduler in windows

Reply
  #1 (permalink)  
Old 12-30-08, 01:07 AM
ashakumar1 ashakumar1 is offline
New Member
 
Join Date: Dec 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Doubts about Task Scheduler in windows

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
Code:
#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 trigger
ITaskTrigger* pITaskTrig = NULL;
TASK_TRIGGER rTrigger;
WORD wTrigNumber = 0;
hr = pITask->CreateTrigger ( &wTrigNumber, &pITaskTrig );
//filling the TASK_TRIGGER structure
ZeroMemory ( &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 task
rTrigger.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;
}

Last edited by Nico; 12-30-08 at 05:53 AM. Reason: Wrappers.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Count requesters from an apache log file ddolddolee82 Perl 1 03-17-06 03:08 AM
Web Hosting plan Reviews $ 55 Per Only Year megawebserver Website Reviews 0 09-05-05 01:10 PM
Programming Windows, Fifth Edition cyper General Advertisements 0 05-05-05 01:46 PM
Matrix Reseller - Windows 2003 and FreeBSD vortech General Advertisements 0 11-02-03 10:06 PM


All times are GMT -5. The time now is 03:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.