How can I schedule a task using delphi 7 like Google updater?
I'm not using the registry because it gets detected by Kaspersky antivirus as a false alarm.
Anything I add in the registry as a start-up item gets detected as a trojan so I decided to use task schedule
The following piece of code shows how to delete and create the task which will run the application at system startup with system privileges. It uses the following command line:
However the Task Scheduler since Windows Vista supports force creation of tasks, I wouldn't use it for backward compatibility with Windows XP, where this flag doesn't exist.
So the example below tries to delete the task (if already exists) and then create the new one.
It executes these commands:
schtasks /delete /f /tn "myjob"
schtasks /create /tn "myjob" /tr "C:\Application.exe" /sc ONSTART /ru "System"/delete - delete the task
/f - suppress the confirmation
/create - create task parameter
/tn - unique name of the task
/tr - file name of an executable file
/sc - schedule type, ONSTART - run at startup
/ru - run task under 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;
/RP
command switch). If your account hasn't got a password yet, you'll have to assign it. (A passwordless user account cannot be used with scheduling.) –
Aphotic Figured Out the problem here it works fine
Tested on windows 7 Pro if any one can test for me on XP PRO would b 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;
The new task has been created, but may not run because the account information could not be set. The specific error is: 0x80070005: Access is denied.
And I'm an administrator without password on that machine. So really, if you don't have a password set on your account, then it won't work. It would be very high security risk for your system. –
Demosthenes © 2022 - 2024 — McMap. All rights reserved.
command line
utility to create scheduled tasks. – DemosthenesShellExecute(0, NIL, 'cmd.exe', Pchar('/C SCHTASKS /CREATE /TN "myjob" /TR ' + '"' + QuotedStr(Pchar('C:\Program Files\myapp\myapp.exe')) + '" /SC ONSTART /RU "System"'), Nil, SW_SHOWNORMAL);
but application is not running as computer started i actually need both xp and windows 7 compatible – Jolin