How to schedule a task programmatically

How to schedule a task using delphi 7, for example, Google updater?
I do not use the registry because it is detected by Kaspersky Anti-Virus as a false alarm.
Everything that I add to the registry as a startup item is detected as a Trojan, so I decided to use a task schedule

+7
source share
2 answers

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; 
+6
source

It turned out that the problem works well here

Tested on Windows 7 Pro, if someone can test me on XP PRO will be appreciated

 procedure ScheduleRunAtStartup(const ATaskName: string; const AFileName: string; const GetPCName: string ; Const GetPCUser: String); begin ShellExecute(0, nil, 'schtasks', PChar('/delete /f /tn "' + ATaskName + '"'), nil, SW_HIDE); ShellExecute(0, nil, 'schtasks', PChar('/create /tn "' + ATaskName + '" ' + '/tr "' + QuotedStr(AFileName) + '" /sc ONLOGON /ru "' + GetPCName+'\'+GetPCUser + '"'), nil, SW_HIDE) end; 
-one
source

All Articles