The following code snippet shows how to remove and create a task that launches the application when the system starts with system privileges. It uses the following command line:
However, the task scheduler with Windows Vista supports forced task creation; I would not use it for backward compatibility with Windows XP, where this flag does not exist. So, the example below tries to delete a task (if it already exists), and then create a new one.
It executes the following commands:
schtasks / delete / f / tn "myjob"
schtasks / create / tn "myjob" / tr "C: \ Application.exe" / sc ONSTART / ru "System"
/ delete - delete a task
/ f - suppress confirmation
/ create - create a task parameter
/ tn - unique task name
/ tr - executable file name
/ sc - schedule type, ONSTART - start at startup
/ ru - run the task under the permissions of the specified user
And here is the code:
uses ShellAPI; procedure ScheduleRunAtStartup(const ATaskName: string; const AFileName: string; const AUserAccount: string); begin ShellExecute(0, nil, 'schtasks', PChar('/delete /f /tn "' + ATaskName + '"'), nil, SW_HIDE); ShellExecute(0, nil, 'schtasks', PChar('/create /tn "' + ATaskName + '" ' + '/tr "' + AFileName + '" /sc ONSTART /ru "' + AUserAccount + '"'), nil, SW_HIDE); end; procedure TForm1.Button2Click(Sender: TObject); begin ScheduleRunAtStartup('myjob', 'C:\Application.exe', 'System'); end;
TLama
source share