Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi,
- As said in my previous post, here is a complete fully functional demo showing how to create a scheduled task(in the example launch the calc.exe) with the MSTask.pas unit. See how the procedure of eah button are implemented to perform each task.
- __________________ paste code below and save as Unit1.pas __________________________
- Unit 1
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics,
- Controls, Forms, Dialogs,
- ActiveX, ImgList, MsTask, MsTaskUtils, Menus,
- Shellapi, StdCtrls, ExtCtrls, ComCtrls;
- type
- TForm1 = class(TForm)
- Label1: TLabel;
- ListandCountTasks: TButton;
- Memo1: TMemo;
- SchedAgentEnum: TButton;
- GetTargetPC: TButton;
- ListBox1: TListBox;
- LoadTasks: TButton;
- ImageList1: TImageList;
- Image1: TImage;
- FileNameOfSelTask: TButton;
- RunSelectTask: TButton;
- TaskStatut: TButton;
- SeeParam: TButton;
- SeePrior: TButton;
- WorkDTaskFlags: TButton;
- Button12: TButton;
- ApplicTask: TButton;
- Planification: TButton;
- SetTaskAccount: TButton;
- Label2: TLabel;
- RichEdit1: TRichEdit;
- procedure FormCreate(Sender: TObject);
- procedure ListandCountTasksClick(Sender: TObject);
- procedure SchedAgentEnumClick(Sender: TObject);
- procedure GetTargetPCClick(Sender: TObject);
- procedure LoadTasksClick(Sender: TObject);
- procedure ListBox1Click(Sender: TObject);
- procedure RunSelectTaskClick(Sender: TObject);
- procedure TaskStatutClick(Sender: TObject);
- procedure SeeParamClick(Sender: TObject);
- procedure SeePriorClick(Sender: TObject);
- procedure WorkDTaskFlagsClick(Sender: TObject);
- procedure Button12Click(Sender: TObject);
- procedure ApplicTaskClick(Sender: TObject);
- procedure PlanificationClick(Sender: TObject);
- procedure SetTaskAccountClick(Sender: TObject);
- procedure FileNameOfSelTaskClick(Sender: TObject);
- private
- { Déclarations privées }
- Function Init(Lbl:TLabel): HRESULT;
- public
- { Déclarations publiques }
- end;
- var
- Form1: TForm1;
- SchedulingAgent: ITaskScheduler;
- ScheduledWorkItem: IScheduledWorkItem;
- pIPersistFile: IPersistFile;
- Task: ITask;
- MyComputerName,
- MyAppname,
- MyParameters,
- MyWorkingDir,
- MyTaskFlags : String;
- implementation
- uses AuxProcs;
- {$R *.DFM}
- ///////////////////////////////////////////////////////////
- Function TForm1.Init(Lbl:TLabel): HRESULT;
- begin
- Result := S_OK;
- Lbl.Caption:= '';
- if not assigned(SchedulingAgent) then
- begin
- Result := ActiveX.CoInitialize(nil);
- if Result <> S_OK then
- begin
- ActiveX.CoUninitialize;
- Lbl.caption:= 'Error in ActiveX.CoInitialize. Error Code: ' + IntToHex(Result, 8);
- Application.Terminate;
- end else
- Lbl.Caption:= 'OK with ActiveX.CoInitialize';
- Result := ActiveX.CoCreateInstance(
- CLSID_CSchedulingAgent,
- nil,
- CLSCTX_INPROC_SERVER,
- IID_ITaskScheduler,
- SchedulingAgent);
- if Result <> S_OK then
- begin
- Lbl.caption:= 'Error in ActiveX.CoCreateInstance. Error Code: ' + IntToHex(Result, 8);
- Application.Terminate;
- end;
- end;
- end;
- ///////////////////////////////////////////////////////////
- Function GetITask(TaskName: string;Lbl:TLabel): ITask;
- var
- PITask: IUnknown;
- hr: HRESULT;
- begin
- Lbl.caption:= '';
- hr := SchedulingAgent.Activate(AuxProcs.StrToWide(TaskName), IID_ITask, PITask);
- if hr = S_OK then
- Result := ITask(PITask)
- else
- begin
- Lbl.Caption:= 'Error in SchedulingAgent.Activate. Error Code: ' + IntToHex(hr, 8);
- Result := nil;
- end;
- end;
- ///////////////////////////////////////////////////////////
- Function SaveTask(Lbl:TLabel): HRESULT;
- begin
- Result := -1;
- Task.QueryInterface(IID_IPersistFile, pIPersistFile);
- if pIPersistFile <> nil then
- begin
- Result := pIPersistFile.Save(nil, true);
- end
- else
- Lbl.caption:= 'Error in Task.QueryInterface(IID_IPersistFile, pIPersistFile): ';
- end;
- ///////////////////////////////////////////////////////////
- Function DeleteTask(JobName: string): HRESULT;
- begin
- Result := SchedulingAgent.Delete(StrToWide(JobName));
- end;
- ///////////////////////////////////////////////////////////
- Procedure IsOkScheduler(Lbl: TLabel);
- var
- hr: HRESULT;
- begin
- Lbl.Caption:= '';
- hr := StartScheduler;
- if hr = ERROR_SUCCESS then
- Lbl.Caption:= 'Task Scheduler has started successfully.'
- else
- Lbl.Caption := 'Task Scheduler didn''t start. Error Code:' + IntTohex(hr, 8);
- end;
- ///////////////////////////////////////////////////////////
- procedure SetTargetComputer(Txt:TMemo);
- var
- hr: HResult;
- ComputerName: PWideChar;
- begin
- //$80070035 networkpath not found
- //$80070005 General access denied
- //passing nil as computername should return local machine target computer
- ComputerNAme := StrToWide(MyComputerName);
- Txt.Lines.Add('Trying to set Target Computername to:' + MyComputerName);
- hr := SchedulingAgent.SetTargetComputer(ComputerName);
- if hr = S_OK then
- Txt.Lines.Add('Target Computername is set to:' + WideCharToString(ComputerNAme))
- else
- begin
- if hr = SCHED_E_SERVICE_NOT_INSTALLED then
- Txt.Lines.Add('TargetComputer does not have service installed');
- Txt.Lines.Add('SetTargetComputer failed with error code:$' + IntTohex(hr, 8));
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- IsOkScheduler(Label1);
- LoadTasksClick(self);
- end;
- //-----------------------------------------------------------
- procedure TForm1.SchedAgentEnumClick(Sender: TObject);
- var
- hr: HRESULT;
- pEnum: IEnumWorkItems;
- begin
- Memo1.clear;
- Init(Label2);
- hr := SchedulingAgent.Enum(pEnum);
- if hr <> S_OK then
- Memo1.Lines.Add('SchedulingAgent.Enum Result:' + IntTohex(hr, 8))
- else
- Memo1.Lines.Add('SchedulingAgent.Enum Result: OK');
- end;
- //-----------------------------------------------------------
- procedure TForm1.GetTargetPCClick(Sender: TObject);
- var
- ComputerName: PWideChar;
- hr: HResult;
- begin
- Memo1.clear;
- Init(Label2);
- hr := SchedulingAgent.GetTargetComputer(ComputerName);
- if hr = S_OK then
- begin
- Memo1.Lines.Add('Target Computername is :' + WideCharToString(ComputerNAme) + #13#10);
- MyComputerName := WideCharToString(ComputerNAme);
- end
- else
- Memo1.Lines.Add('GetTargetComputer failed with error code :$' + IntTohex(hr, 8) + #13#10);
- end;
- //-----------------------------------------------------------
- procedure TForm1.ListandCountTasksClick(Sender: TObject);
- var
- hr: HRESULT;
- pEnum: IEnumWorkItems;
- TaskNames: PLPWSTR;
- dwFetched: DWORD;
- szString: string;
- Appname: PWideChar;
- begin
- Memo1.clear;
- Listbox1.clear;
- GetTargetPCClick(self);
- hr := SchedulingAgent.Enum(pEnum);
- while (pEnum.Next(1, TaskNames, dwFetched) = 0) and (dwFetched > 0) do
- begin
- szString := WideCharToString(TaskNames^);
- Memo1.Lines.Add('Enum.Next fetched: ' + IntToStr(dwFetched) + ' item(s).');
- Memo1.Lines.Add('Taskname: ' + szString);
- Memo1.Lines.Add('---------------------');
- ListBox1.Items.Add(szString);
- ActiveX.CoTaskMemFree(TaskNames);
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.FileNameOfSelTaskClick(Sender: TObject);
- var
- i: integer;
- Appname: PWideChar;
- begin
- Memo1.clear;
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Memo1.clear;
- Task := GetITask(ListBox1.Items[ListBox1.Itemindex],label2);
- Task.GetApplicationName(Appname);
- Memo1.lines.Add('The task : ' + ListBox1.Items[ListBox1.Itemindex]);
- Memo1.lines.Add('Call the following application file: ');
- Memo1.lines.Add(AppName);
- end;
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.LoadTasksClick(Sender: TObject);
- var
- hr: HRESULT;
- pEnum: IEnumWorkItems;
- TaskNames: PLPWSTR;
- dwFetched: DWORD;
- szString: string;
- TriggCount: Word;
- Appname: PWideChar;
- IconIndex: Word;
- Icon: TIcon;
- TriggStr: PwideChar;
- NextRunTime: _SYSTEMTIME;
- begin
- Memo1.clear;
- Listbox1.Clear;
- Icon := TIcon.Create;
- IconIndex := 0;
- GetTargetPCClick(self);
- Init(label2);
- hr := SchedulingAgent.Enum(pEnum);
- ImageList1.Clear;
- while (pEnum.Next(1, TaskNames, dwFetched) = 0) and (dwFetched > 0) do
- begin
- //Taskname from enum
- szString := WideCharToString(TaskNames^);
- Memo1.lines.Add(szString);
- ListBox1.Items.Add(szString); // add the name of each existing task
- //for getting the icon from filename of task
- Task := GetITask(szString,label2);
- Task.GetApplicationName(Appname);
- if MyComputerName = '\\' + GetLocalComputerName then
- begin
- Memo1.lines.Add('Name of application: '+(PChar(WideCharToString(Appname))));
- Icon.Handle := ExtractAssociatedIcon(Hinstance, PChar(WideCharToString(Appname)), IconIndex);
- Image1.Picture.Icon:= Icon;
- end;
- Task.GetWorkingDirectory(Appname);
- Memo1.lines.Add('GetWorkingDir: '+WideCharToString(Appname));
- //GetStatus
- Task.GetStatus(hr);
- Memo1.lines.Add(MessageFromValue(hr));
- //GetNextRecentTime
- hr := Task.GetNextRunTime(NextRunTime);
- if hr = S_OK then
- Memo1.lines.Add('NextRunTime: '+ DateTimeToStr(SystemTimeToDateTime(NextRunTime)))
- else
- Memo1.lines.Add('Next RunTime'+ MessageFromValue(hr)); ;
- //GetMostRecentTime
- hr := Task.GetMostRecentRunTime(NextRunTime);
- if hr = S_OK then
- Memo1.lines.Add('Recent runtime: ' + DateTimeToStr(SystemTimeToDateTime(NextRunTime)))
- else
- Memo1.lines.Add('Recent runtime: ' + MessageFromValue(hr)); ;
- //GetTriggerString and GetTriggerCount
- hr := Task.GetTriggerCount(TriggCount);
- if hr = S_OK then
- if TriggCount > 1 then
- begin
- Memo1.lines.Add('Multiple schedule times');
- end
- else
- begin
- Task.GetTriggerString(0, TriggStr);
- if TriggStr <> nil then
- Memo1.lines.Add('Planification: ' + WideCharToString(TriggStr));
- end;
- Memo1.lines.Add('');
- ActiveX.CoTaskMemFree(TaskNames);
- end;
- Icon.free;
- end;
- //-----------------------------------------------------------
- procedure TForm1.TaskStatutClick(Sender: TObject);
- var
- hr: HRESULT;
- i: integer;
- begin
- Memo1.clear;
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Task := GetITask(Listbox1.Items[i],label2);
- Task.GetStatus(hr);
- Memo1.Lines.Add('GetStatus returned value :' + IntToHex(hr, 8));
- Memo1.Lines.Add('Message from value : ' + MessageFromValue(hr));
- end;
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.SeeParamClick(Sender: TObject);
- var
- i: integer;
- pParameters: PWideChar;
- begin
- Memo1.clear;
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Task := GetITask(Listbox1.Items[i],label2);
- Task.GetParameters(pParameters);
- if WideCharToString(pParameters) = '' then
- Memo1.Lines.Add('GetParameters did not return any value as there are no parameters' + #13#10)
- else
- Memo1.Lines.Add('GetParameters returned :' + WideCharToString(pParameters) + #13#10);
- MyParameters := WideCharToString(pParameters);
- end;
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.SeePriorClick(Sender: TObject);
- var
- i: integer;
- Priority: Cardinal;
- PriorityStr: string;
- begin
- Memo1.clear;
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Task := GetITask(Listbox1.Items[i],label2);
- Task.GetPriority(Priority);
- case Priority of
- REALTIME_PRIORITY_CLASS: PriorityStr := 'Realtime';
- HIGH_PRIORITY_CLASS: PriorityStr := 'High';
- NORMAL_PRIORITY_CLASS: PriorityStr := 'Normal';
- IDLE_PRIORITY_CLASS: PriorityStr := 'Idle';
- end;
- Memo1.Lines.Add('The priority of the task is ' + PriorityStr + #13#10);
- end;
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.WorkDTaskFlagsClick(Sender: TObject);
- var
- //Appname: PWideChar;
- i: integer;
- pWorkingDir: PWideChar;
- PdwFlags: Cardinal;
- begin
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Memo1.clear;
- Task.GetWorkingDirectory(pWorkingDir);
- if WideCharToString(pWorkingDir) = '' then
- Memo1.Lines.Add('GetWorkingDirectory did not return any value as there are no parameters')
- else
- Memo1.Lines.Add('GetWorkingDirectory returned: ' + WideCharToString(pWorkingDir));
- MyWorkingDir := WideCharToString(pWorkingDir);
- Task.GetTaskFlags(PdwFlags);
- Memo1.Lines.Add('GetTask Flags returned value of PdwFlags ' + IntToStr(PdwFlags));
- MyTaskFlags := IntToStr(PdwFlags);
- end;
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.ListBox1Click(Sender: TObject);
- //var
- // Appname: PWideChar;
- // szString: string;
- // IconIndex: Word;
- // Icon: TIcon;
- // i: integer;
- begin
- //Memo1.clear;
- // for i:= 0 to ListBox1.items.count -1 do
- // begin
- //
- // if listbox1.Selected[i] then
- // begin
- // Memo1.clear;
- // Task := GetITask(ListBox1.Items[ListBox1.Itemindex],label2);
- // Task.GetApplicationName(Appname);
- // Memo1.Lines.Add('Application Name is: ' + WideCharToString(Appname));
- // MyAppname:= WideCharToString(Appname);
- //
- // Icon := TIcon.Create;
- // IconIndex := 0;
- //
- // if MyComputerName = '\\' + GetLocalComputerName then
- // begin
- // Memo1.lines.Add('Name of application: '+(PChar(WideCharToString(Appname))));
- // Icon.Handle := ExtractAssociatedIcon(Hinstance, PChar(WideCharToString(Appname)), IconIndex);
- // Image1.Picture.Icon:= Icon;
- // end;
- // Icon.free;
- // end;
- //end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.ApplicTaskClick(Sender: TObject);
- var
- Priority: Cardinal;
- PriorityStr: string;
- hr: HRESULT;
- Idx : integer;
- begin
- Memo1.clear;
- Idx:= ListBox1.Itemindex;
- Task := GetITask(Listbox1.Items[idx],label2);
- MyAppName:= 'Calc.exe';
- HR := Task.SetApplicationName(StrToWide(MyAppname));
- if HR = S_OK then
- Memo1.Lines.Add('Application Name is set to :' + MyAppname)
- else
- begin
- Memo1.Lines.Add('Error in setting Application Name to :' + MyAppname + 'Error Code:' + IntTohex(HR, 8));
- Exit;
- end;
- SaveTAsk(Label2);
- end;
- //-----------------------------------------------------------
- procedure TForm1.SetTaskAccountClick(Sender: TObject);
- var
- i: integer;
- Username, Password: string;
- hr: HRESULT;
- uname, pass: PWideChar;
- PdwFlags: Cardinal;
- begin
- Memo1.clear;
- if ListBox1.items.count -1 > 0 then
- begin
- for i:= 0 to Listbox1.Items.count - 1 do
- if listbox1.Selected[i] then
- begin
- Username := 'Bernard';
- Password := '';
- GetMem(uname, 255);
- GetMem(pass, 255);
- StringToWideChar(Username, uname, Length(UserName) + 1);
- StringToWideChar(Password, pass, Length(Password) + 1);
- Task := GetITask(Listbox1.Items[i],label2);
- Task.SetFlags(8192);
- hr := Task.SetAccountInformation(uname, pass);
- SaveTask(Label2);
- LoadTasksClick(Self);
- case hr of
- S_OK:
- begin
- Memo1.Lines.Add('SetAccountInformation success');
- end;
- E_ACCESSDENIED: Memo1.Lines.Add('The caller does not have permission to perform the operation. For more information, see Remarks.');
- E_INVALIDARG: Memo1.Lines.Add('The arguments are not valid.');
- E_OUTOFMEMORY: Memo1.Lines.Add('Not enough memory is available.');
- else
- begin
- Memo1.Lines.Add(MessageFromValue(hr));
- Memo1.Lines.Add('SetAccountInformation failed with errorcode :' + IntTohex(hr, 8) + #13#10);
- end;
- end;
- FreeMem(uname);
- FreeMem(pass);
- HR := Task.SetTaskFlags(StrToInt('$2000'));
- if HR = S_OK then Memo1.Lines.Add('SetTaskFlags set to :' + myTaskFlags)
- else
- begin
- Memo1.Lines.Add('Error in setting flags to :' + MyTaskFlags + 'Error Code:' + IntTohex(HR, 8));
- exit;
- end;
- if hr <> S_OK then
- Memo1.lines.add('Save task failed with error code' + IntTohex(hr, 8) + #13#10);
- end;
- Task.Run;
- // Uncomment line under to see the windows dialog sched.
- //Task.EditWorkItem(self.handle,0);
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.RunSelectTaskClick(Sender: TObject);
- var
- hr: HRESULT;
- i: integer;
- begin
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Task := GetITask(Listbox1.Items[i],label2);
- HR := Task.SetTaskFlags(StrToInt('$2000'));//MyTaskFlags));
- hr := Task.Run;
- Memo1.Lines.Add('Run Application Error Code :' + IntTohex(hr, 8) + #13#10);
- Memo1.Lines.Add(MyAppName);//Listbox1.Items[i]);
- // BtnGetTasksClick(self); //for updating
- end;
- end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.Button12Click(Sender: TObject);
- var
- Appname: PWideChar;
- szString: string;
- IconIndex: Word;
- Icon: TIcon;
- i: integer;
- pWorkingDir: PWideChar;
- PdwFlags: Cardinal;
- //
- HR : HRESULT;
- TaskName: string;
- WorkItem: IUnknown;
- Creator: PWideChar;
- MaxRunTime: DWORD;
- begin
- Memo1.clear;
- TaskName := '001000'; // name of the task - here hardcoded
- Init(Label2);
- HR := SchedulingAgent.NewWorkItem(
- StrToWide(TaskName),
- CLSID_CTask,
- IID_IScheduledWorkItem,
- WorkItem);
- Task := ITask(WorkItem); //required for save
- ScheduledWorkItem := IScheduledWorkItem(WorkItem);
- if HR = S_OK then
- begin
- Listbox1.items.add(Taskname);
- Memo1.Lines.Add(Taskname + ' created successfully');
- Listbox1.Update;
- end
- else
- begin
- Memo1.Lines.Add('Could not create the job ' + taskname + '. Error Code: ' + IntTohex(hr, 8));
- exit;
- end;
- hr := SaveTAsk(Label2);
- if hr <> S_OK then Memo1.lines.add('Save task failed with error code: ' + IntTohex(hr, 8));
- Memo1.clear;
- Task := GetITask(Taskname,label2);
- Task.GetApplicationName(Appname);
- Memo1.Lines.Add('Application Name is: ' + WideCharToString(Appname));
- MyAppname:= WideCharToString(Appname);
- Icon := TIcon.Create;
- IconIndex := 0;
- if MyComputerName = '\\' + GetLocalComputerName then
- begin
- Memo1.lines.Add('Name of application: '+(PChar(WideCharToString(Appname))));
- Icon.Handle := ExtractAssociatedIcon(Hinstance, PChar(WideCharToString(Appname)), IconIndex);
- Image1.Picture.Icon:= Icon;
- Task := GetITask(TaskName,label2);
- Task.GetCreator(Creator);
- Memo1.Lines.Add('The Name of the Creator of the selected job is : ' + WideCharToString(Creator));
- Task := GetITask(Taskname,label2);
- Task.GetMaxRunTime(MaxRunTime);
- Memo1.Lines.Add('GetMaxRunTime returned: ' + IntToStr(MaxRunTime) + ' milliseconds');
- end;
- Icon.free;
- // end;
- end;
- //-----------------------------------------------------------
- procedure TForm1.PlanificationClick(Sender: TObject);
- var
- i: integer;
- hr: HRESULT;
- piNewTrigger: Word;
- ITTrigger: ITaskTrigger;
- TaskTrig: TTaskTRIGGER;
- Count: Word;
- begin
- memo1.clear;
- for i:= 0 to ListBox1.items.count -1 do
- begin
- if listbox1.Selected[i] then
- begin
- Task := GetITask(Listbox1.Items[i],label2);
- HR := Task.CreateTrigger(piNewTrigger, ITTrigger);
- if HR = S_OK then
- Memo1.Lines.Add('Create Trigger success ')
- else
- begin
- Memo1.Lines.Add('Error in Create Trigger Error Code:' + IntToHex(HR, 8));
- exit;
- end;
- ZeroMemory(@TaskTrig, sizeof(TASK_TRIGGER));
- TaskTrig.cbTriggerSize := sizeof(TASK_TRIGGER);
- TaskTrig.Reserved1 := 0;
- TaskTrig.wBeginYear := 2006;
- TaskTrig.wBeginMonth := 11;
- TaskTrig.wBeginDay := 27;
- TaskTrig.wEndYear := 2006;
- TaskTrig.wEndMonth := 11;
- TaskTrig.wEndDay := 30;
- TaskTrig.wStartHour := 16;
- TaskTrig.wStartMinute := 25;
- TaskTrig.MinutesDuration := 0;
- TaskTrig.MinutesInterval := 0;
- TaskTrig.rgFlags := 0;
- {rgFlags
- Value that describes the task trigger's behavior.
- This value is a combination of the following flags:
- TASK_TRIGGER_FLAG_HAS_END_DATE
- Trigger structure's end date is valid.
- If this flag is not set, the end date data is ignored and
- the trigger will be valid indefinitely.
- TASK_TRIGGER_FLAG_KILL_AT_DURATION_END
- Task will be terminated at the end of the active trigger's
- lifetime. At the duration end, the scheduling agent
- first sends a WM_CLOSE message to
- the associated application. If the application hasn't
- exited in three minutes,TerminateProcess runs.
- If more than one instance of a task is running,
- all instances are terminated.
- TASK_TRIGGER_FLAG_DISABLED
- Task trigger is inactive.
- }
- TaskTrig.TriggerType := TASK_TIME_TRIGGER_ONCE;
- TaskTrig.wRandomMinutesInterval := 0;
- TaskTrig.Reserved2 := 0;
- hr := ITTrigger.SetTrigger(@TaskTrig);
- if (hr <> S_OK) then
- begin
- Memo1.Lines.Add(MessageFromValue(hr));
- exit;
- end;
- SaveTAsk(Label2);
- end;
- end; {for}
- //display properties of trigger
- try Task.GetTriggerCount(Count)except {} end;
- if Count <= 0 then
- begin
- ShowMessage('No triggers');
- Exit;
- end;
- Task.GetTrigger(0, ITTrigger);
- TaskTrig.cbTriggerSize := sizeof(TASK_TRIGGER);
- hr := ITTrigger.GetTrigger(@TaskTrig);
- if (hr <> S_OK) then
- begin
- Memo1.clear;
- Memo1.Lines.Add(MessageFromValue(hr));
- exit;
- end;
- Memo1.Lines.Add('Get Trigger Returned :');
- Memo1.Lines.Add(' Begin Year:' + IntToStr(TaskTrig.wBeginYear) + #13#10 +
- ' Begin Month:' + IntToStr(TaskTrig.wBeginMonth) + #13#10 +
- ' Begin Day:' + IntToStr(TaskTrig.wBeginDay) + #13#10 +
- ' End Year:' + IntToStr(TaskTrig.wEndYear) + #13#10 +
- ' End Month:' + IntToStr(TaskTrig.wEndMonth) + #13#10 +
- ' End Day:' + IntToStr(TaskTrig.wEndDay));
- end; {proc}
- end.
- __________________ paste code below and save as Projec1.dpr __________________________
- program Project1;
- uses
- Forms,
- Unit1 in 'Unit1.pas' {Form1},
- AuxProcs in 'AuxProcs.pas',
- MsTaskUtils in 'MsTaskUtils.pas';
- {$R *.RES}
- begin
- Application.Initialize;
- Application.CreateForm(TForm1, Form1);
- Application.Run;
- end.
- __________________ paste code below and save as Unit1.dfm __________________________
- object Form1: TForm1
- Left = 243
- Top = 105
- Width = 696
- Height = 601
- Caption = 'Creating a scheduled task under Delphi ...'
- Color = clWhite
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWindowText
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = []
- OldCreateOrder = False
- ShowHint = True
- OnCreate = FormCreate
- PixelsPerInch = 96
- TextHeight = 13
- object Label1: TLabel
- Left = 19
- Top = 16
- Width = 331
- Height = 13
- AutoSize = False
- Color = clBlue
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWhite
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = [fsBold]
- ParentColor = False
- ParentFont = False
- end
- object Image1: TImage
- Left = 72
- Top = 483
- Width = 57
- Height = 46
- Center = True
- end
- object Label2: TLabel
- Left = 359
- Top = 16
- Width = 304
- Height = 13
- AutoSize = False
- Color = clYellow
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clRed
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = [fsBold]
- ParentColor = False
- ParentFont = False
- end
- object ListandCountTasks: TButton
- Left = 14
- Top = 105
- Width = 169
- Height = 25
- Caption = 'ListandCountTasks'
- TabOrder = 0
- OnClick = ListandCountTasksClick
- end
- object Memo1: TMemo
- Left = 360
- Top = 48
- Width = 303
- Height = 281
- ScrollBars = ssVertical
- TabOrder = 1
- end
- object SchedAgentEnum: TButton
- Left = 14
- Top = 45
- Width = 169
- Height = 25
- Caption = 'SchedulingAgent.Enum'
- TabOrder = 2
- OnClick = SchedAgentEnumClick
- end
- object GetTargetPC: TButton
- Left = 14
- Top = 75
- Width = 169
- Height = 25
- Caption = 'GetTargetComputer'
- TabOrder = 3
- OnClick = GetTargetPCClick
- end
- object ListBox1: TListBox
- Left = 192
- Top = 48
- Width = 161
- Height = 497
- Color = 16731983
- Columns = 1
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clWhite
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = [fsBold]
- ItemHeight = 13
- ParentFont = False
- TabOrder = 4
- OnClick = ListBox1Click
- end
- object LoadTasks: TButton
- Left = 14
- Top = 134
- Width = 169
- Height = 25
- Caption = 'Load task and infos'
- Font.Charset = DEFAULT_CHARSET
- Font.Color = clRed
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = [fsBold]
- ParentFont = False
- TabOrder = 5
- OnClick = LoadTasksClick
- end
- object FileNameOfSelTask: TButton
- Left = 14
- Top = 164
- Width = 169
- Height = 25
- Hint = 'Select job and click '
- Caption = 'Applic. name of selected task'
- TabOrder = 6
- OnClick = FileNameOfSelTaskClick
- end
- object RunSelectTask: TButton
- Left = 14
- Top = 432
- Width = 169
- Height = 25
- Caption = 'Run application'
- TabOrder = 7
- OnClick = RunSelectTaskClick
- end
- object TaskStatut: TButton
- Left = 14
- Top = 194
- Width = 169
- Height = 25
- Caption = 'Statut of theTask'
- TabOrder = 8
- OnClick = TaskStatutClick
- end
- object SeeParam: TButton
- Left = 14
- Top = 224
- Width = 169
- Height = 25
- Caption = 'See Parameters'
- TabOrder = 9
- OnClick = SeeParamClick
- end
- object SeePrior: TButton
- Left = 14
- Top = 254
- Width = 169
- Height = 25
- Caption = 'See Priority of task'
- TabOrder = 10
- OnClick = SeePriorClick
- end
- object WorkDTaskFlags: TButton
- Left = 14
- Top = 284
- Width = 169
- Height = 25
- Caption = 'Get Work Dir & Get Task Flags'
- TabOrder = 11
- OnClick = WorkDTaskFlagsClick
- end
- object Button12: TButton
- Left = 14
- Top = 313
- Width = 169
- Height = 25
- Caption = 'New scheduled task'
- Font.Charset = DEFAULT_CHARSET
- Font.Color = 16744448
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = [fsBold]
- ParentFont = False
- TabOrder = 12
- OnClick = Button12Click
- end
- object ApplicTask: TButton
- Left = 14
- Top = 373
- Width = 169
- Height = 25
- Caption = 'Set Application Name'
- TabOrder = 13
- OnClick = ApplicTaskClick
- end
- object Planification: TButton
- Left = 14
- Top = 343
- Width = 169
- Height = 25
- Caption = 'Set Trigger = Creer planification'
- TabOrder = 14
- OnClick = PlanificationClick
- end
- object SetTaskAccount: TButton
- Left = 14
- Top = 401
- Width = 169
- Height = 25
- Caption = 'Account'
- TabOrder = 15
- OnClick = SetTaskAccountClick
- end
- object RichEdit1: TRichEdit
- Left = 360
- Top = 344
- Width = 305
- Height = 201
- Font.Charset = DEFAULT_CHARSET
- Font.Color = 12615680
- Font.Height = -11
- Font.Name = 'MS Shell Dlg 2'
- Font.Style = []
- Lines.Strings = (
- 'Short Explanations'
- ''
- 'Load task and infos'
- 'Will read all the existing tasks and display infos about them'
- ''
- 'New Scheduled task'
- 'Create a new task called 001000 if it doesn'#39't exist. '
- 'After thisstep, you need to select the task called Set '
- 'trigger (to add date, time...) to this task. '
- 'And finally, you need to click on the Account button to '
- 'supply the password and username.'
- 'That'#39's all.'
- ''
- 'Note all is hardcoded in this demo (name of the task, date '
- 'and '
- 'time of execution ...). In a real application, it will be '
- 'necessary to dynamiccaly supply those infos.'
- ''
- 'Hope this help.'
- '')
- ParentFont = False
- ScrollBars = ssVertical
- TabOrder = 16
- end
- object ImageList1: TImageList
- Left = 584
- Top = 264
- end
- end
Advertisement
RAW Paste Data
Copied
Advertisement