How to Schedule a task programmatically
Asked Answered
J

2

8

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

Jolin answered 2/1, 2012 at 12:17 Comment(12)
Use the Task Scheduler API. The MSDN documentation to which I link contains many examples.Peristyle
One has to wonder why you are getting flagged as a Trojan. Perhaps there is a real issue.Peristyle
well prog is not harmful at all its very simple application but Kasper-sky detect application as Trojan if use RegSetValueEx Software\Microsoft\Windows\CurrentVersion\Run to set registry any where in code. task scheduler API do u have any running example for Delphi ?Jolin
Because a lot of malware try to add entries to the \Run key, most AV will flag it as a suspicious behaviour. Moreover programs there will slow down system startup. Better to add them as a scheduled task and run them when system is idle, preferably. This way you deliver a "better user experience". Never imply your app is the only one running on a user's system :)Louanne
exactly ! can i get any scheduled task API working example for DelphiJolin
Did you try looking at one of the examples from MSDN?Peristyle
@David, did you mean this one ? Working on it..Demosthenes
@Demosthenes For example that one would do. I do note that user has not actually said precisely what type of task is desired.Peristyle
My personal conclusion, I would prefer command line utility to create scheduled tasks.Demosthenes
@Tlama i tried this ShellExecute(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 compatibleJolin
You've had an extra quotes in the file name parameter. See my answer below ;)Demosthenes
Maybe you should get a digital signature on your app if Kaspersky is catching it.Fitted
D
10

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;
Demosthenes answered 3/1, 2012 at 10:19 Comment(3)
Well it successfully ADD in scheduled task but wouldn't RUN on windows start-up i hope my (kaspersky anti-virus) not blocking scheduled task . what to do ? i tried to replace "system" user as my current user logged in but still failed.Jolin
@user1023395: If you want to use your own user account, you'll also need to provide the corresponding password (using the /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
I confess I haven't tested if the application is executed (I don't have much experience with scheduled tasks), I've tested only if they are created. And @AndriyM is right, you will either have to have and know the password to the account or modify the registry settings. One is clear, scheduled tasks without this password protection would be one big hole to your system security.Demosthenes
J
-1

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;
Jolin answered 3/1, 2012 at 15:1 Comment(5)
I've tried that on Windows XP Professional SP3, but no, it doesn't work without password. It is the same as if you create a scheduled task through the Scheduled Task Wizard. When I've finished the wizard I got the warning message 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
This isn't an answer. You should indicate why it is an answer instead of just slapping some code down (and asking others to test it on other platforms makes it even more not an answer.Cointreau
without quotedstr(AFileName) it set path as parameter so it would not run application test on windows 7 @TLAMA replace ONSTART with ONLOGONJolin
That's exactly the same; how you schedule the trigger is one thing, another thing is who is the task owner. Consider that if you would have your account without password I would be able to schedule some malware task which would trigger every minute. That's why you have to have the password on your account and if you are scheduling a task you have to know this password. So Task Scheduler is not the way to go for running your application at startup (for your application distribution), because your user would have his account password protected and you would have to know this password.Demosthenes
/IT A value that enables the task to run interactively only if the /RU user is currently logged on at the time the task runs. The task runs only if the user is logged on. Windows XP and Windows Server 2003: This option is not available.Jolin

© 2022 - 2024 — McMap. All rights reserved.