title: Task scheduler created in C++ could not be started
body: <p>I created an scheduler in C++.I have set all the parameters and the task is configured to run only when the user is logged on with the user name provided(Done by setting the TASK_FLAG_RUN_ONLY_IF_LOGGED_ON flag).</p>
  
  <p>When I try to run the task I get a status "Could not start". Now suppose I manually edit any property in task property and click on OK the task runs fine. </p>
  
  <p>Note:The manual edit specified may be anything, like just adding a space at the end of the excecutable name or the user name. What may be the problem?</p>
  
  <p>Below is the code i am using:</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;wchar.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  
  #pragma comment(lib, "Mstask.lib")
  #pragma comment(lib, "ole32.lib")
  
  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 **) &amp;pITS);
     if (FAILED(hr))
    {
     CoUninitialize();
     return 1;
     }
    }
      else
   {
    return 1;
   }
  
   LPCWSTR pwszTaskName;
   ITask *pITask;
    pwszTaskName = L"TestTask";
  
   hr = pITS-&gt;NewWorkItem(pwszTaskName,
    CLSID_CTask,
    IID_ITask,
   (IUnknown**)&amp;pITask);
  
    if (FAILED(hr))
   {
    wprintf(L"Failed calling ITaskScheduler::NewWorkItem: ");
    wprintf(L"error = 0x%x\n",hr);
    CoUninitialize();
     return 1;
    }
  
  
    LPCWSTR pwszApplicationName = L"C:\\windows\\notepad.exe";
  
    hr = pITask-&gt;SetApplicationName(pwszApplicationName);
  
    if (FAILED(hr))
   {
    wprintf(L"Failed calling ITask::SetApplicationName: ");
    wprintf(L"error = 0x%x\n",hr);
    pITS-&gt;Release();
    pITask-&gt;Release();
    CoUninitialize();
    return 1;
   }
  
    pITask-&gt;SetAccountInformation(L"USERNAME", NULL);
    pITask-&gt;SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
    pITask-&gt;SetWorkingDirectory(L"C:\\windows");
  
  
    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;
    }
  
    pITS-&gt;AddWorkItem(pwszTaskName, pITask);
    pITS-&gt;Release(); // Release sceduler
  
     hr = pITask-&gt;Run();
    if (FAILED(hr))
    {
      wprintf(L"Failed calling ITask::Run, error = 0x%x\n",hr);
      pITask-&gt;Release();
     CoUninitialize();
     return 1;
    }
  
     pITask-&gt;Release();      
    CoUninitialize();
      _getch();
    return 0;
  }
  </code></pre>