研
研究僧
Unregistered / Unconfirmed
GUEST, unregistred user!
就是这段代码,特别是SetApplicationName这个过程有问题
procedure TMainForm.AddTaskBtnClick(Sender: TObject);
var
HR: HRESULT;
TaskName: String;
AppName, Path, UserName, Password, Parameter: WideString;
WorkItem: IUnknown;
piNewTrigger: Word;
ITTrigger: ITaskTrigger;
TaskTrig: TTaskTRIGGER;
begin
//New Task
TaskName := InputBox('Enter Task Name', 'Name:', '');
if TAskName = '' then
begin
ShowMessage('You have to enter a task name');
exit;
end;
SchedulingAgent.NewWorkItem(StrToWide(TaskName), CLSID_CTask, IID_IScheduledWorkItem, WorkItem); //创建一个新的工作区
Task := ITask(WorkItem); //required for save
ScheduledWorkItem := IScheduledWorkItem(WorkItem);
//SaveTask;
AppName := Format('"%s"', [Application.ExeName]);
Task.SetApplicationName(StrToWide(AppName)); //设置包含的应用程序(大家看看这句,虽然能写入,但不能执行,是不是PWideChar类型与String类型有什么问题)
Path := Format('"%s"', [ExtractFilePath(Application.ExeName)]);
Task.SetWorkingDirectory(PWideChar(@Path[1])); //设置工作目录
Parameter := '';
Task.SetParameters(PWideChar(Parameter)); //设置参数
Task.SetComment('');
UserName := 'luo';
Password := '';
Task.SetAccountInformation(StrToWide(UserName), StrToWide(Password)); //设置用户名和密码
Task.SetTaskFlags(0);
Task.SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
SaveTask;
Task.CreateTrigger(piNewTrigger, ITTrigger); //创建触发
ZeroMemory(@TaskTrig, SizeOf(TASK_TRIGGER)); //分配内存
//触发结构值
TaskTrig.cbTriggerSize := SizeOf(TASK_TRIGGER); //长度
TaskTrig.Reserved1 := 0; //第一保留字
TaskTrig.wBeginYear := YearOf(Date); //开始年份
TaskTrig.wBeginMonth := MonthOf(Date); //开始月份
TaskTrig.wBeginDay := DayOf(Date); //开始日期
TaskTrig.wEndYear := 0; //结束年份
TaskTrig.wEndMonth := 0; //结束月份
TaskTrig.wEndDay := 0; //结束日期
TaskTrig.wStartHour := HourOf(Time); //开始小时
TaskTrig.wStartMinute := MinuteOf(Time)+1; //开始分钟
TaskTrig.MinutesDuration := 0; //持续分钟
TaskTrig.MinutesInterval := 0; //间隔分钟
TaskTrig.rgFlags := 0; //状态值
TaskTrig.TriggerType := TASK_TIME_TRIGGER_DAILY; //每天一次
TaskTrig.Type_.Daily.DaysInterval := 1; //间隔一天
TaskTrig.wRandomMinutesInterval := 0; //
TaskTrig.Reserved2 := 0; //第二保留字
ITTrigger.SetTrigger(@TaskTrig); //设置触发器
HR := SaveTask; //保存任务设置
if HR <> S_OK then
ShowMessage('Could not create the job ' + taskname + ' Error Code :' + IntTohex(hr, 8));
EnumTasksBtnClick(Self); //for updating
end;
procedure TMainForm.AddTaskBtnClick(Sender: TObject);
var
HR: HRESULT;
TaskName: String;
AppName, Path, UserName, Password, Parameter: WideString;
WorkItem: IUnknown;
piNewTrigger: Word;
ITTrigger: ITaskTrigger;
TaskTrig: TTaskTRIGGER;
begin
//New Task
TaskName := InputBox('Enter Task Name', 'Name:', '');
if TAskName = '' then
begin
ShowMessage('You have to enter a task name');
exit;
end;
SchedulingAgent.NewWorkItem(StrToWide(TaskName), CLSID_CTask, IID_IScheduledWorkItem, WorkItem); //创建一个新的工作区
Task := ITask(WorkItem); //required for save
ScheduledWorkItem := IScheduledWorkItem(WorkItem);
//SaveTask;
AppName := Format('"%s"', [Application.ExeName]);
Task.SetApplicationName(StrToWide(AppName)); //设置包含的应用程序(大家看看这句,虽然能写入,但不能执行,是不是PWideChar类型与String类型有什么问题)
Path := Format('"%s"', [ExtractFilePath(Application.ExeName)]);
Task.SetWorkingDirectory(PWideChar(@Path[1])); //设置工作目录
Parameter := '';
Task.SetParameters(PWideChar(Parameter)); //设置参数
Task.SetComment('');
UserName := 'luo';
Password := '';
Task.SetAccountInformation(StrToWide(UserName), StrToWide(Password)); //设置用户名和密码
Task.SetTaskFlags(0);
Task.SetFlags(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
SaveTask;
Task.CreateTrigger(piNewTrigger, ITTrigger); //创建触发
ZeroMemory(@TaskTrig, SizeOf(TASK_TRIGGER)); //分配内存
//触发结构值
TaskTrig.cbTriggerSize := SizeOf(TASK_TRIGGER); //长度
TaskTrig.Reserved1 := 0; //第一保留字
TaskTrig.wBeginYear := YearOf(Date); //开始年份
TaskTrig.wBeginMonth := MonthOf(Date); //开始月份
TaskTrig.wBeginDay := DayOf(Date); //开始日期
TaskTrig.wEndYear := 0; //结束年份
TaskTrig.wEndMonth := 0; //结束月份
TaskTrig.wEndDay := 0; //结束日期
TaskTrig.wStartHour := HourOf(Time); //开始小时
TaskTrig.wStartMinute := MinuteOf(Time)+1; //开始分钟
TaskTrig.MinutesDuration := 0; //持续分钟
TaskTrig.MinutesInterval := 0; //间隔分钟
TaskTrig.rgFlags := 0; //状态值
TaskTrig.TriggerType := TASK_TIME_TRIGGER_DAILY; //每天一次
TaskTrig.Type_.Daily.DaysInterval := 1; //间隔一天
TaskTrig.wRandomMinutesInterval := 0; //
TaskTrig.Reserved2 := 0; //第二保留字
ITTrigger.SetTrigger(@TaskTrig); //设置触发器
HR := SaveTask; //保存任务设置
if HR <> S_OK then
ShowMessage('Could not create the job ' + taskname + ' Error Code :' + IntTohex(hr, 8));
EnumTasksBtnClick(Self); //for updating
end;