再請教有關 ITaskScheduler 讀取排程設定的撰寫 |
缺席
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
小弟曾經詢問過有關排程 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=54928
感謝 阿子 提供範例 但是拷貝部份程式碼過來使用, 卻發生奇怪錯誤, 只好再上來請教 (另開主題是為了可以給答題者分數) 下面紅色那行會出現 [Error] Unit1.pas(67): Constant object cannot be passed as var parameter 錯誤 <textarea class="delphi" rows="10" cols="60" name="code"> unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, ActiveX, ImgList, MsTask, MsTaskUtils, Menus, Shellapi; type TForm1 = class(TForm) Memo1: TMemo; eComputerName: TEdit; lvTasks: TListView; SmallImageList: TImageList; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; SchedulingAgent: ITaskScheduler; ScheduledWorkItem: IScheduledWorkItem; pIPersistFile: IPersistFile; Task: ITask; implementation uses AuxProcs; {$R *.dfm} function Init(): HRESULT; begin Result := S_OK; if not assigned(SchedulingAgent) then begin Result := ActiveX.CoInitialize(nil); if Result <> S_OK then begin ActiveX.CoUninitialize; ShowMessage('Error in ActiveX.CoInitialize.Error Code :' IntToHex(Result, 8)); Application.Terminate; end; Result := ActiveX.CoCreateInstance(CLSID_CSchedulingAgent, nil, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, SchedulingAgent); if Result <> S_OK then begin ShowMessage('Error in ActiveX.CoCreateInstance.Error Code :' IntToHex(Result, 8)); Application.Terminate; end; end; end; function GetITask(TaskName: string): ITask; var PITask: IUnknown; hr: HRESULT; begin hr := SchedulingAgent.Activate(StrToWide(TaskName), IID_ITask, PITask); if hr = S_OK then Result := ITask(PITask) else begin ShowMessage('Error in SchedulingAgent.Activate.Error Code :' IntToHex(hr, 8)); Result := nil; end; end; procedure TForm1.FormCreate(Sender: TObject); var hr: HRESULT; pEnum: IEnumWorkItems; TaskNames: PLPWSTR; dwFetched: DWORD; szString: string; ListItem: TListItem; TriggCount: Word; Appname: PWideChar; IconIndex: Word; Icon: TIcon; TriggStr: PwideChar; NextRunTime: _SYSTEMTIME; ComputerName: PWideChar; begin Icon := TIcon.Create; IconIndex := 0; {used to fill edtGetTargetComputer for checking if targetcomputer is local or not so that the icons of the programs are not added to imagelist and listview if tasks are of remote computer } // BtnGetTargetComputerClick(self); // BtnStartClick(self); hr := StartScheduler; if hr = ERROR_SUCCESS then Memo1.Lines.Add('Task Scheduler started successfully.') else Memo1.Lines.Add('Task Scheduler did not start. Error Code:' IntTohex(hr, 8)); Init; hr := SchedulingAgent.GetTargetComputer(ComputerName); if hr = S_OK then begin Memo1.Lines.Add('Target Computername is :' WideCharToString(ComputerNAme) #13#10); eComputerName.Text := WideCharToString(ComputerNAme); end else Memo1.Lines.Add('GetTargetComputer failed with error code :$' IntTohex(hr, 8) #13#10); hr := SchedulingAgent.Enum(pEnum); if hr <> S_OK then Memo1.Lines.Add('SchedulingAgent.Enum Result:' IntTohex(hr, 8)); lvTAsks.Items.Clear; SmallImageList.Clear; //or icons of previous get tasks will appear when another computer is selected while (pEnum.Next(1, TaskNames, dwFetched) = 0) and (dwFetched > 0) do begin //Taskname from enum szString := WideCharToString(TaskNames^); Memo1.Lines.Add('Enum.Next fetched:' IntToStr(dwFetched) 'item(s).' 'Taskname :' szString); ListItem := lvTasks.Items.Add; ListItem.Caption := szString; //for getting the icon from filename of task Task := GetITask(szString); Task.GetApplicationName(Appname); //if we try to extract an icon for a file on a remote machine our program will crash so extract only if fileexists //Itask.GetTargetComputer retrieves computername with '\\' so while checking with local computername we add that too if eComputerName.Text = '\\' GetLocalComputerName then begin Icon.Handle := ExtractAssociatedIcon(Hinstance, PChar(WideCharToString(Appname)), IconIndex); ListItem.ImageIndex := SmallImageLIst.AddIcon(Icon); end; //GetWorkingDir Task.GetWorkingDirectory(Appname); ListItem.SubItems.Add(WideCharToString(Appname)); //GetStatus Task.GetStatus(hr); ListItem.SubItems.Add(MessageFromValue(hr)); //GetNextRecentTime hr := Task.GetNextRunTime(NextRunTime); if hr = S_OK then ListItem.SubItems.Add(DateTimeToStr(SystemTimeToDateTime(NextRunTime))) else ListItem.SubItems.Add(MessageFromValue(hr)); ; //GetMostRecentTime hr := Task.GetMostRecentRunTime(NextRunTime); if hr = S_OK then ListItem.SubItems.Add(DateTimeToStr(SystemTimeToDateTime(NextRunTime))) else ListItem.SubItems.Add(MessageFromValue(hr)); ; //GetTriggerString and GetTriggerCount hr := Task.GetTriggerCount(TriggCount); if hr = S_OK then if TriggCount > 1 then begin ListItem.SubItems.Add('Multiple schedule times'); end else begin Task.GetTriggerString(0, TriggStr); if TriggStr <> nil then ListItem.SubItems.Add(WideCharToString(TriggStr)); end; /////////////////////////////////////// ActiveX.CoTaskMemFree(TaskNames); end; Icon.free; end; end. </textarea>發表人 - pcboy2 於 2004/11/19 14:25:07 發表人 - pcboy2 於 2004/11/19 14:27:24
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
小弟發現 MsTask.pas 有下面設定
<textarea class="delphi" rows="10" cols="60" name="code"> const IID_ITask: TGUID = '{148BD524-A2AB-11CE-B11F-00AA00530503}'; </textarea>於是將自己程式略作修改 <textarea class="delphi" rows="10" cols="60" name="code"> function GetITask(TaskName: string): ITask; var PITask: IUnknown; hr: HRESULT; IID_ITask2 : TGUID; begin IID_ITask2 := IID_ITask; hr := SchedulingAgent.Activate(StrToWide(TaskName), IID_ITask2, PITask); </textarea>執行成功了 有誰可以告訴小弟, 為何原作者可以正常使用, 小弟必須這樣修改 ? 發表人 - pcboy2 於 2004/11/19 16:24:58
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |