Announcement

Collapse
No announcement yet.

Doubts about task scheduler

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Doubts about task scheduler

    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;}

    #2
    Please use {CODE} {/CODE} tags on your code (using square brackets)
    Also looks like a C program. In XP, you need a password, I believe, to enable a scheduled task.
    Last edited by Fred Buffington; 30 Dec 2008, 10:00 PM.
    Client Writeup for the CPA

    buffs.proboards2.com

    Links Page

    Comment


      #3
      Thanks

      Dear Fred Buffington,
      Thanks a lot for your valuable suggestion; Now that program is working fine;

      I have two more doubts;

      (1)I tried to delete task which is already present in scheduled task list; It is giving the following error... "Cannot delete Access is denied"; If i want to delete scheduled task, Is admin right mandatory?

      (2)With out admin rights, can i reschedule the existing task?

      Comment


        #4
        Amazing. You get answers here to questions about another programming language with poorly-formatted code posted.

        Then again, it does demonstrate that "post all relevant code" does have at least as many advantages as does being an American Express cardmember....
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


          #5
          Try adding a password to the scheduled task you want to delete.
          Then exit (close) scheduled tasks, recall scheduled tasks and try to delete (you may need to reboot first).
          Client Writeup for the CPA

          buffs.proboards2.com

          Links Page

          Comment


            #6
            Hi

            I found out in a forum that ITaskscheduler's method 'Delete' can be used to delete task from task scheduler;

            So i used the following code; Program is executed successfully; But task was not deleted; Where i may be wrong? Please advice me;

            [#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"<<std::endl;
            return 1;
            }
            else
            {
            cout<<"Object created successfully"<<std::endl;
            }
            }
            else
            {
            cout<<"Failed to initialize"<<std::endl;
            return 1;
            }
            ///////////////////////////////////////////////////////////////////
            // Deleting already existing task
            ///////////////////////////////////////////////////////////////////

            pITS->Delete(L"Test Task16");
            if(FAILED(hr))
            {
            cout<<"Test task is not deleted";
            }
            else
            {
            cout<<"Test task deleted successfully"<<std::endl;
            }
            pITS->Release();

            return 0;
            }]

            Comment


              #7
              While you may 'get lucky' here, I think you would get a better quantity and quality of response in a 'C' language programming venue.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎