Hi,

I have created a tsk scheduler using c++. i have also created a trigger for it. But when I am trying to save the trigger by calling pIPersistFile->Save(NULL, TRUE) method it is throwing an error.

The error is:

unable to establish existance of the account specified task scheduler

How do i resolve this?
Below is the code I am using.It is the same code provided in MSDN.

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();
   return 1;
  }
 }
 else
 {
  return 1;
 }

 ///////////////////////////////////////////////////////////////////
 // Call ITaskScheduler::NewWorkItem to get a new Task object.
 ///////////////////////////////////////////////////////////////////

 LPCWSTR pwszTaskName;
 ITask *pITask;
 pwszTaskName = L"TestTask";

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

 
 ///////////////////////////////////////////////////////////////////
 // Set the tasks application name.
 ///////////////////////////////////////////////////////////////////

 LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe";

 hr = pITask->SetApplicationName(pwszApplicationName);

 if (FAILED(hr))
 {
  wprintf(L"Failed calling ITask::SetApplicationName: ");
  wprintf(L"error = 0x%x\n",hr);
  pITS->Release();
  pITask->Release();
  CoUninitialize();
  return 1;
 }

 pITask->SetAccountInformation(L"USERNAME",NULL); 
 pITask->SetWorkingDirectory(L"C:\\windows");
 ///////////////////////////////////////////////////////////////////
 // Call ITask::CreateTrigger to create new trigger.
 ///////////////////////////////////////////////////////////////////
  
  ITaskTrigger *pITaskTrigger;
  WORD piNewTrigger;
  hr = pITask->CreateTrigger(&piNewTrigger,
                             &pITaskTrigger);
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::CreatTrigger: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    CoUninitialize();
    return 1;
  }
  
  
  //////////////////////////////////////////////////////
  // Define TASK_TRIGGER structure. Note that wBeginDay,
  // wBeginMonth, and wBeginYear must be set to a valid 
  // day, month, and year respectively.
  //////////////////////////////////////////////////////
  
  TASK_TRIGGER pTrigger;
  ZeroMemory(&pTrigger, sizeof (TASK_TRIGGER));
  
  // Add code to set trigger structure?
  pTrigger.wBeginDay =29;                  // Required
  pTrigger.wBeginMonth =11;                // Required
  pTrigger.wBeginYear =2010;              // Required
  pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER); 
  pTrigger.wStartHour = 23;
  pTrigger.wStartMinute = 07;
  pTrigger.TriggerType = TASK_TIME_TRIGGER_DAILY;
  pTrigger.Type.Daily.DaysInterval = 1;
  
  
  ///////////////////////////////////////////////////////////////////
  // Call ITaskTrigger::SetTrigger to set trigger criteria.
  ///////////////////////////////////////////////////////////////////
  
  hr = pITaskTrigger->SetTrigger (&pTrigger);
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITaskTrigger::SetTrigger: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    pITaskTrigger->Release();
    CoUninitialize();
    return 1;
  }
  
///////////////////////////////////////////////////////////////////
  // Call IPersistFile::Save to save trigger to disk.
  ///////////////////////////////////////////////////////////////////
  
  IPersistFile *pIPersistFile;
  hr = pITask->QueryInterface(IID_IPersistFile,
                              (void **)&pIPersistFile);
  hr = pIPersistFile->Save(NULL,
                           TRUE);

  if (FAILED(hr))
  {
    wprintf(L"Failed calling IPersistFile::Save: ");
    wprintf(L"error = 0x%x\n",hr);
    pITask->Release();
    pITaskTrigger->Release();
    pIPersistFile->Release();
    CoUninitialize();
    return 1;
  }
  
  wprintf(L"The trigger was created and IPersistFile::Save was \n");
  wprintf(L"called to save the new trigger to disk.\n"); 


 ///////////////////////////////////////////////////////////////////
 // Add the task to the sceduler
 ///////////////////////////////////////////////////////////////////

 pITS->AddWorkItem(pwszTaskName, pITask);
 pITS->Release(); // Release sceduler

 ///////////////////////////////////////////////////////////////////
 // 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);
  pITask->Release();
  CoUninitialize();
  return 1;
 }

 pITask->Release();
  pITaskTrigger->Release();
  pIPersistFile->Release();
 CoUninitialize();
	_getch();
 return 0;
}

Thanks,
Subrat

I suppose you ought to begin by changing your account information usage, i.e.

//// Instead of ...
//// pITask->SetAccountInformation(L"USERNAME",NULL); 

// .. use valid account information
pITask->SetAccountInformation(L"YOUR ACCOUNT HERE", L"ITS PASSWORD HERE");

See SetAccountInformation()

Tried that . Still not working.

One thing i forgot to mention is that the Task is getting created. But when i run it, it says could not be started. If i manually edit any property of the task it runs successfully.

Check the link below. I am facing the same problem.
http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/291d9722-ccbc-41ff-b33d-5f1af694ae72

I suppose you ought to begin by changing your account information usage, i.e.

//// Instead of ...
//// pITask->SetAccountInformation(L"USERNAME",NULL); 

// .. use valid account information
pITask->SetAccountInformation(L"YOUR ACCOUNT HERE", L"ITS PASSWORD HERE");

See SetAccountInformation()

>> Tried that . Still not working.

What is the exact error message the prog displays?

The same as before.
The error is:

unable to establish existance of the account specified task scheduler.


While calling pIPersistFile->Save(NULL, TRUE) method it is throwing an error.

>> The error is:
>> unable to establish existance of the account specified task scheduler.

>> While calling pIPersistFile->Save(NULL, TRUE) method it is throwing an error.

A quick test on XP shows that this error (upon pIPersistFile->Save() ) occurs when either;

  • The account name is valid, but the password is NULL
  • The account name is invalid i.e. it's a non-existing username

Are you absolutely sure you are using valid username & password?

Note that even if I supply a non-existing username, the task is created and when viewed via the Task Scheduler, the currently logged-on user's name has automagically appeared in the Run As -field (which is quite surprising).

yes I am supplying the correct username and password.were you able to run it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.