Click here to Skip to main content
15,896,453 members
Articles / Programming Languages / C++

Harnessing the task scheduler

Rate me:
Please Sign up or sign in to vote.
4.70/5 (20 votes)
28 Aug 2013CPOL9 min read 118.9K   3.4K   83  
Using the Task Scheduler interface in applications can be tricky, as it requires a detailed knowledge of the COM technology. This article presents a practical solution to this problem, based on simplifying communications with the interface.
#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <objidl.h>
#include <wchar.h>
#include <stdio.h>

int main(int /*argc*/, char** /*argv*/)
{
  HRESULT hr = ERROR_SUCCESS;
  ITaskScheduler *pITS;
  
  
  /////////////////////////////////////////////////////////////////
  // Inicjalizacja biblioteki COM
  /////////////////////////////////////////////////////////////////
  hr = CoInitialize(NULL);

  if (SUCCEEDED(hr))
  {
     hr = CoCreateInstance(CLSID_CTaskScheduler,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_ITaskScheduler,
                           (void **) &pITS);
     if (FAILED(hr))
     {
        CoUninitialize();
        return 1;
     }
  }
  else
  {
     return 1;
  }
  
  
  /////////////////////////////////////////////////////////////////
  // Wywolujemy ITaskScheduler::NewWorkItem() aby utworzyc nowe zadanie
  /////////////////////////////////////////////////////////////////
  LPCWSTR pwszTaskName;
  ITask *pITask;
  IPersistFile *pIPersistFile;
  pwszTaskName = L"Test Task1";
  
  hr = pITS->NewWorkItem(pwszTaskName,           // nazwa zadania
                         CLSID_CTask,            // identyfikator klasy 
                         IID_ITask,              // identyfikator interfejsu
                         (IUnknown**)&pITask); // adres wskaznika do interfejsu ITask
  
  
  pITS->Release();                               // Release object
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling NewWorkItem, error = 0x%x\n",hr);
     return 1;
  }
  
  
  /////////////////////////////////////////////////////////////////
  // Wywolaj IUnknown::QueryInterface aby dostac wskaznik do 
  // IPersistFile i IPersistFile::Save aby zapisac zadanie na dysku
  /////////////////////////////////////////////////////////////////
  
  hr = pITask->QueryInterface(IID_IPersistFile,
                              (void **)&pIPersistFile);
  
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
     return 1;
  }
  
  
  hr = pIPersistFile->Save(NULL,
                           TRUE);
  pIPersistFile->Release();
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);
     return 1;
  }
  


///////////////////////////////////////////////////
///////////tworzymy trigger////////////////////////
///////////////////////////////////////////////////

ITaskTrigger*   pITaskTrig = NULL;
IPersistFile*   pIFile     = NULL;
TASK_TRIGGER    rTrigger;
WORD            wTrigNumber;


 hr = pITask->CreateTrigger ( &wTrigNumber, &pITaskTrig );

    if ( FAILED (hr) )
        {
        fprintf(stderr, "Failed calling CreateTrigger, error = 0x%x\n",hr);
        return hr;
        }

	//wypelniamy strukture TASK_TRIGGER
	ZeroMemory ( &rTrigger, sizeof (TASK_TRIGGER) );

    rTrigger.cbTriggerSize = sizeof (TASK_TRIGGER);
    rTrigger.wBeginYear    = 2004;
    rTrigger.wBeginMonth   = 4;
    rTrigger.wBeginDay     = 10;
    rTrigger.wStartHour    = 10;
    rTrigger.wStartMinute  = 0;
	rTrigger.TriggerType = TASK_TIME_TRIGGER_ONCE;
	

	// skojarz Trigger z zadaniem
	hr = pITaskTrig->SetTrigger ( &rTrigger );

    if ( FAILED(hr) )
        {
        fprintf(stderr, "Failed calling SetTrigger, error = 0x%x\n",hr);
        return 1;
        }


    // zapisujemy zmiany w PersistFile
    hr = pITask->QueryInterface ( IID_IPersistFile, (void **) &pIFile );

    if ( FAILED (hr) )
        {
        fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
        return 1;
        }

    hr = pIFile->Save ( NULL, FALSE );

    if ( FAILED(hr) )
        {
        fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);

        return hr;
        }
  
  /////////////////////////////////////////////////////////////////
  // Zwolnij zasoby
  /////////////////////////////////////////////////////////////////
  
  hr = pITask->Release();
  if (FAILED(hr))
  {
     CoUninitialize();
     fprintf(stderr, "Failed calling Release, error = 0x%x\n",hr);
     return 1;
  }
  
  
  printf("Succesfully created task and trigger.\n");


  return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Publisher
Poland Poland
Software Developer's Journal (formerly Software 2.0) is a magazine for professional programmers and developers publishing news from the software world and practical articles presenting very interesting ready programming solutions. To read more

Comments and Discussions