title: Trying to create a scheduled task to run once using C++ , Task Scheduler 1.0 on win7
body: <p>I'm trying to create a very simple program which will run Notepad after two minutes (these are all simplified to make a clearer question). I tried to merge some of <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa383579%28v=vs.85%29.aspx" rel="noreferrer">MSDN's examples </a> and to my surprise everything compiled successfully, when running the program a <code>.job</code> task is created in <code>C:\Windows\Tasks</code>. I don't see the new task in the "Task Scheduler" GUI program and I assume this is because "Task Scheduler" shows only Task Scheduler 2.0 tasks, but I'm not sure. The problem is that the task is not being executed. I'm attaching the code, it is a bit long but well documented. </p>
  
  <p>Can someone help figuring this out? 
  If a task got an error when running where is it logged? </p>
  
  <p>Note 1: I know the way I'm calculating the start time is not ideal (and will give faulty results on times where minutes is <code>&gt;57</code>). As I said before the code below is a simplified version.</p>
  
  <p>Note 2: I'm running it on win7 and I assume it is compatible with Task Scheduler 1.0 API. This code should run on both XP and win7 machines (and hopefully on win 8 in the future)</p>
  
  <p>Note 3: To anyone who's brave enough to try it, if you get error <code>0x80070050</code> it means that a task with that name already exists, delete the <code>.job</code> file or change the name.</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;initguid.h&gt;
  #include &lt;ole2.h&gt;
  #include &lt;mstask.h&gt;
  #include &lt;msterr.h&gt;
  #include &lt;objidl.h&gt;
  #include &lt;wchar.h&gt;
  #include &lt;stdio.h&gt;
  
  
  int main(int argc, char **argv)
  {
    HRESULT hr = S_OK;
    ITaskScheduler *pITS;
  
  
    /////////////////////////////////////////////////////////////////
    // Call CoInitialize to initialize the COM library and then 
    // call CoCreateInstance to get the Task Scheduler object. 
    /////////////////////////////////////////////////////////////////
    hr = CoInitialize(NULL);
    if (SUCCEEDED(hr))
    {
       hr = CoCreateInstance(CLSID_CTaskScheduler,
                             NULL,
                             CLSCTX_INPROC_SERVER,
                             IID_ITaskScheduler,
                             (void **) &amp;pITS);
       if (FAILED(hr))
       {
          CoUninitialize();
          return 1;
       }
    }
    else
    {
       return 1;
    }
  
  
    /////////////////////////////////////////////////////////////////
    // Call ITaskScheduler::NewWorkItem to create new task.
    /////////////////////////////////////////////////////////////////
    LPCWSTR pwszTaskName;
    ITask *pITask;
    IPersistFile *pIPersistFile;
    pwszTaskName = L"Test Task";
  
    hr = pITS-&gt;NewWorkItem(pwszTaskName,         // Name of task
                           CLSID_CTask,          // Class identifier 
                           IID_ITask,            // Interface identifier
                           (IUnknown**)&amp;pITask); // Address of task 
                                                                                                                                                                                              //  interface
  
  
    pITS-&gt;Release();                               // Release object
    if (FAILED(hr))
    {
       CoUninitialize();
       fprintf(stderr, "Failed calling NewWorkItem, error = 0x%x\n",hr);
       return 1;
    }
  
    /////////////////////////////////////////////////////////////////
    //Set Comment, Name, Working dir, Params
    /////////////////////////////////////////////////////////////////
    pITask-&gt;SetComment(L"This is a comment");
    pITask-&gt;SetApplicationName(L"C:\\Windows\\System32\\notepad.exe");
    pITask-&gt;SetWorkingDirectory(L"C:\\Windows\\System32");
    pITask-&gt;SetParameters(L"");
  
    ///////////////////////////////////////////////////////////////////
    // Call ITask::CreateTrigger to create new trigger.
    ///////////////////////////////////////////////////////////////////
  
    ITaskTrigger *pITaskTrigger;
    WORD piNewTrigger;
    hr = pITask-&gt;CreateTrigger(&amp;piNewTrigger,
                               &amp;pITaskTrigger);
    if (FAILED(hr))
    {
      wprintf(L"Failed calling ITask::CreatTrigger: ");
      wprintf(L"error = 0x%x\n",hr);
      pITask-&gt;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(&amp;pTrigger, sizeof (TASK_TRIGGER));
  
    LPSYSTEMTIME lpSystemTime;
    GetLocalTime(lpSystemTime);
  
  
    // Add code to set trigger structure?
    pTrigger.wBeginDay = lpSystemTime-&gt;wDay;                  // Required
    pTrigger.wBeginMonth = lpSystemTime-&gt;wMonth;                // Required
    pTrigger.wBeginYear =lpSystemTime-&gt;wYear;              // Required
    pTrigger.cbTriggerSize = sizeof (TASK_TRIGGER); 
    pTrigger.wStartHour = lpSystemTime-&gt;wHour;
    pTrigger.wStartMinute = lpSystemTime-&gt;wMinute + 2;
    pTrigger.TriggerType = TASK_TIME_TRIGGER_DAILY;
    pTrigger.Type.Daily.DaysInterval = 1;
  
  
    ///////////////////////////////////////////////////////////////////
    // Call ITaskTrigger::SetTrigger to set trigger criteria.
    ///////////////////////////////////////////////////////////////////
  
    hr = pITaskTrigger-&gt;SetTrigger (&amp;pTrigger);
    if (FAILED(hr))
    {
      wprintf(L"Failed calling ITaskTrigger::SetTrigger: ");
      wprintf(L"error = 0x%x\n",hr);
      pITask-&gt;Release();
      pITaskTrigger-&gt;Release();
      CoUninitialize();
      return 1;
    }
  
  
  
  
    /////////////////////////////////////////////////////////////////
    // Call IUnknown::QueryInterface to get a pointer to 
    // IPersistFile and IPersistFile::Save to save 
    // the new task to disk.
    /////////////////////////////////////////////////////////////////
  
    hr = pITask-&gt;QueryInterface(IID_IPersistFile,
                                (void **)&amp;pIPersistFile);
  
    pITask-&gt;Release();
    if (FAILED(hr))
    {
       CoUninitialize();
       fprintf(stderr, "Failed calling QueryInterface, error = 0x%x\n",hr);
       return 1;
    }
  
  
    hr = pIPersistFile-&gt;Save(NULL,
                             TRUE);
    pIPersistFile-&gt;Release();
    if (FAILED(hr))
    {
       CoUninitialize();
       fprintf(stderr, "Failed calling Save, error = 0x%x\n",hr);
       return 1;
    }
  
  
    CoUninitialize();
    printf("Created task.\n");
    return 0;
  }
  </code></pre>
  
  <h1>EDIT:</h1>
  
  <p>I added the following:</p>
  
  <pre><code>  /////////////////////////////////////////////////////////////////
    //Set Flags
    /////////////////////////////////////////////////////////////////
  
    pITask-&gt;SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>///////////////////////////////////////////////////////////////////
    // Call ITask::SetAccountInformation to specify the account name
    // and the account password for Test Task.
    ///////////////////////////////////////////////////////////////////
    hr = pITask-&gt;SetAccountInformation(L"", 
              NULL);
  
  
    if (FAILED(hr))
    {
      wprintf(L"Failed calling ITask::SetAccountInformation: ");
      wprintf(L"error = 0x%x\n",hr);
      pITask-&gt;Release();
      CoUninitialize();
      return 1;
    }
  </code></pre>
  
  <p>Now the task is getting displayed in the task scheduler and is being executed under SYSTEM account (Notepad.exe is not displayed since SYSTEM account is not interactive with desktop or something). If I change it to <code>hr = pITask-&gt;SetAccountInformation(L"MyUserName", NULL);</code> then Notepad is being displayed. Problem solved :).</p>