2

I'm trying to create a task (via C++) that should be executed with highest privileges, although I cannot see in the docs for Task Scheduler how to set that flag (which is available via the Task Scheduler UI).

Ideas ?

Robert
  • 2,330
  • 29
  • 47

4 Answers4

3

It's the ITaskDefinition::Principal property. Set RunLevel in IPrincipal to TASK_RUNLEVEL_HIGHEST.

wj32
  • 8,053
  • 3
  • 28
  • 37
2

Here is the code snippet how to setup run level.

IPrincipal *pPrincipal = NULL;
hr = pTask->get_Principal(&pPrincipal);
if (FAILED(hr))
    {
        printf("\nCannot get principal pointer: %x", hr);
        pRootFolder->Release();
        pTask->Release();
        CoUninitialize();
        return false;
    }
//  Set up principal run level to the highest one
hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);
pPrincipal->Release();
if (FAILED(hr))
{
    printf("\nCannot put principal info: %x", hr);
    pRootFolder->Release();
    pTask->Release();
    CoUninitialize();
    return false;
}
  • amazing, there is also the default option, `Stop the task if it is running longer than` it is automatically set to 3 days, how do i turn that off ? Thank you – Ahmed Can Unbay Feb 15 '21 at 23:36
  • also there is `Stop if the computer ceases to be idle` i want to get that to `0` as well, iwant my task to keep running always – Ahmed Can Unbay Feb 15 '21 at 23:40
2

Here is an example on MSDN.

Naszta
  • 7,560
  • 2
  • 33
  • 49
0

You may wish to explore MSDN Task Scheduler Reference. Also, check Schtasks.exe.

Donotalo
  • 12,748
  • 25
  • 83
  • 121