procedure TForm10.Button1Click(Sender: TObject);
const
RPC_C_AUTHN_LEVEL_PKT_PRIVACY = 6;
const
RPC_C_IMP_LEVEL_IMPERSONATE = 3;
var
Res: HRESULT;
TaskService: ITaskService;
RootFolder: ITaskFolder;
TaskDefinition: ITaskDefinition;
TaskTrigger: ITrigger;
LogonTrigger: ILogonTrigger;
TaskAction: IAction;
ExecAction: IExecAction;
RegisteredTask: IRegisteredTask;
begin
// Res := CoInitializeEx(nil, COINIT_MULTITHREADED);
Res := CoInitialize(
nil);
if FAILED(Res)
then
begin
raise Exception.CreateFmt('
CoInitializeEx failed: 0x%x', [Res]);
end;
Res := CoInitializeSecurity(
nil, -1,
nil,
nil, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
nil, 0,
nil);
if FAILED(Res)
then
begin
raise Exception.CreateFmt('
CoInitializeSecurity failed: 0x%x', [Res]);
end;
Res := CoCreateInstance(CLASS_TaskScheduler_,
nil, CLSCTX_INPROC_SERVER,
IID_ITaskService, TaskService);
if FAILED(Res)
then
begin
raise Exception.CreateFmt('
Failed to create an instance of ITaskService: 0x%x', [Res]);
end;
try
TaskService.Connect(NULL, NULL, NULL, NULL);
except
on E: EOleException
do
raise Exception.CreateFmt('
Failed to connect to task service: 0x%x', [E.ErrorCode]);
end;
try
RootFolder := TaskService.GetFolder('
\');
except
on E: EOleException
do
raise Exception.CreateFmt('
Could not get the root folder: 0x%x', [E.ErrorCode]);
end;
try
TaskDefinition := TaskService.NewTask(0);
except
on E: EOleException
do
raise Exception.CreateFmt('
Failed to create a task definition: 0x%x', [E.ErrorCode]);
end;
TaskDefinition.RegistrationInfo.Author := '
Zacherl';
TaskDefinition.Settings.AllowDemandStart := false;
TaskDefinition.Settings.StartWhenAvailable := true;
TaskDefinition.Settings.StopIfGoingOnBatteries := false;
TaskDefinition.Settings.DisallowStartIfOnBatteries := false;
TaskDefinition.Settings.AllowHardTerminate := false;
TaskDefinition.Settings.RunOnlyIfNetworkAvailable := false;
TaskDefinition.Settings.RunOnlyIfIdle := false;
// No ExecutionTimeLimit
TaskDefinition.Settings.ExecutionTimeLimit := '
PT0S';
// Allow multiple instances
TaskDefinition.Settings.MultipleInstances := TASK_INSTANCES_PARALLEL;
TaskDefinition.Principal.LogonType := TASK_LOGON_GROUP;
// Trigger task for SID S-1-1-0 = All Users
TaskDefinition.Principal.GroupId := '
S-1-1-0';
// UAC Elevation if possible (admin privileges)
TaskDefinition.Principal.RunLevel := TASK_RUNLEVEL_HIGHEST;
TaskTrigger := TaskDefinition.Triggers.Create(TASK_TRIGGER_LOGON);
Res := TaskTrigger.QueryInterface(IID_ILogonTrigger, LogonTrigger);
if FAILED(Res)
then
begin
raise Exception.CreateFmt('
QueryInterface call failed for ILogonTrigger: 0x%x', [Res]);
end;
LogonTrigger.Id := '
LoggonTriggerTestId';
TaskAction := TaskDefinition.Actions.Create(TASK_ACTION_EXEC);
Res := TaskAction.QueryInterface(IID_IExecAction, ExecAction);
if FAILED(Res)
then
begin
raise Exception.CreateFmt('
QueryInterface call failed for IExecAction: 0x%x', [Res]);
end;
ExecAction.Path := '
C:\Windows\notepad.exe';
RegisteredTask := RootFolder.RegisterTaskDefinition('
LogonTaskTest', TaskDefinition,
TASK_CREATE_OR_UPDATE, NULL, NULL, TASK_LOGON_GROUP, NULL)
end;