Robert
Robert

Reputation: 2389

Programmatically creating/deleting tasks for task scheduler

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 ?

Upvotes: 2

Views: 4497

Answers (4)

Aleksandar Panov
Aleksandar Panov

Reputation: 21

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

Upvotes: 2

wj32
wj32

Reputation: 8463

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

Upvotes: 3

Donotalo
Donotalo

Reputation: 13035

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

Upvotes: 0

Naszta
Naszta

Reputation: 7744

Here is an example on MSDN.

Upvotes: 2

Related Questions