title: How to get ITimeTrigger interface for a Task Scheduler task?
body: <p>I'm trying to get the "<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa381886(v=vs.85).aspx" rel="nofollow noreferrer">random delay</a>" value for an arbitrary <a href="http://en.wikipedia.org/wiki/Windows_Task_Scheduler" rel="nofollow noreferrer">task scheduler</a> task. I came up with the following C++ code:</p>
  
  <pre><code>//IRegisteredTask* pRegisteredTask for task:
  //  Name: "Regular Maintenance"
  //  Folder: "\Microsoft\Windows\TaskScheduler"
  
  ITaskDefinition* pTaskDef = NULL;
  HRESULT hr = pRegisteredTask-&gt;get_Definition(&amp;pTaskDef);
  if(SUCCEEDED(hr))
  {
      //Get triggers
      ITriggerCollection* pTriggerCol = NULL;
      hr = pTaskDef-&gt;get_Triggers(&amp;pTriggerCol);
      if(SUCCEEDED(hr))
      {
          //Get number of triggers
          LONG nTriggerCnt = 0;
          hr = pTriggerCol-&gt;get_Count(&amp;nTriggerCnt);
          if(SUCCEEDED(hr))
          {
              //Look through all triggers
              for(LONG t = 0; t &lt; nTriggerCnt; t++)
              {
                  ITrigger* pTrigger = NULL;
                  hr = pTriggerCol-&gt;get_Item(t + 1, &amp;pTrigger);
                  if(SUCCEEDED(hr))
                  {
                      //Get time trigger interface
                      ITimeTrigger *pTimeTrigger = NULL;
                      hr = pTrigger-&gt;QueryInterface(IID_ITimeTrigger, (void**)&amp;pTimeTrigger);
                      if(SUCCEEDED(hr))
                      {
                          BSTR bstrRndDelay = NULL;
                          hr = pTimeTrigger-&gt;get_RandomDelay(&amp;bstrRndDelay);
                          if(SUCCEEDED(hr))
                          {
                              //Check random delay
                          }
  
                          SysFreeString(bstrRndDelay);
                      }
                      else
                      {
                          //0x80004002 (E_NOINTERFACE) error happens here
                      }
  
                      if(pTimeTrigger)
                          pTimeTrigger-&gt;Release();
                  }
  
                  if(pTrigger)
                      pTrigger-&gt;Release();
              }
          }
      }
  
      if(pTriggerCol)
          pTriggerCol-&gt;Release();
  }
  
  if(pTaskDef)
      pTaskDef-&gt;Release();
  </code></pre>
  
  <p>I'm testing the code snippet above on the task that I know has "random delay" set up, i.e., <code>\Microsoft\Windows\TaskScheduler</code>-><code>Regular Maintenance</code> on my Windows 8.1:</p>
  
  <p><img src="https://i.stack.imgur.com/QIWV8.png" alt="enter image description here"></p>
  
  <p>but for some reason when I try to get the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa381885%28v=vs.85%29.aspx" rel="nofollow noreferrer">ITimeTrigger</a> interface I'm getting error <code>0x80004002</code> or <code>E_NOINTERFACE</code> (as I marked in the code above.)</p>
  
  <p>Any idea what am I doing wrong?</p>