Recent

Author Topic: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??  (Read 37668 times)

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
[SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« on: December 15, 2011, 10:58:11 pm »
I have wrapper for API 1 in windows xp and in Windows vista and 7 is API 2.
How I can setup and managing tasks in task schreduler in Windows 7?
Any idea?

Regards
« Last Edit: December 18, 2011, 10:16:40 am by nicke85 »
ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #1 on: December 16, 2011, 11:19:11 am »
There are several ways of doing this.
-VB style: create com object and use late binding
-C++ style: define interfaces and use early binding
-mixed: create XML file and load it using one of the above methods.
-execute the Schtasks.exe command line tool
The easiest is probably the 4th option.

What method is your api 1 wrapper using now?

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #2 on: December 16, 2011, 04:17:37 pm »
Hi ludob,I use mstasks.pas from JEDI-library it is wrapper for API 1 and there are methods.
Well if i could make some process to get enumerated type with Schtasks.exe it would be great.
But with SchTasks i can create,modify and when want to delete task than there is a confirmation for that.How to make it to be an automated process without confirmation?

ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #3 on: December 16, 2011, 06:42:06 pm »
Then probably the VB style late binding is what you need. This sounds difficult but is rather simple once you get the basics. Nothing better than an example to show how it works  ;) I translated the VBscript sample you find here http://msdn.microsoft.com/en-us/library/windows/desktop/aa383665%28v=VS.85%29.aspx to pascal (tested on win7 64 bit):
Code: [Select]
uses comobj;

procedure TForm1.TimeTrigger;
var
  service,rootfolder,taskdefinition,reginfo,principal,settings,triggers,
  trigger,action_:Olevariant;
begin
  service:=CreateOleObject('Schedule.Service');
  service.connect;
  rootfolder:=service.GetFolder(Olevariant('\'));
  taskdefinition:=service.NewTask(0);
  reginfo:=taskdefinition.RegistrationInfo;
  reginfo.Description:='Start Notepad';
  reginfo.Author:='Ludob';
  principal:=taskdefinition.Principal;
  principal.LogonType:=3;
  settings:=taskdefinition.Settings;
  settings.Enabled:=true;
  settings.StartWhenAvailable:=true;
  settings.Hidden:=False;
  triggers:=taskdefinition.Triggers;
  trigger:=triggers.Create(1);
  trigger.StartBoundary:=olevariant('2011-12-16T18:30:00+01:00');
  trigger.EndBoundary:=olevariant('2011-12-16T18:40:00+01:00');
  trigger.ExecutionTimeLimit:=olevariant('PT5M');
  trigger.Id:=olevariant('TimeTriggerId');
  trigger.Enabled:=true;
  action_:=taskdefinition.Actions.Create(0);
  action_.Path:=olevariant('c:\Windows\system32\notepad.exe');
  rootfolder.RegisterTaskDefinition(olevariant('Test TimeTrigger'),
         taskdefinition,6,NULL,NULL , 3);
end;


This launched notepad at 18:30 utc+1 today. These are all reference counted objects. No need to clean up when leaving.

Note that you need a recent compiler since a lot of changes where made in the compilers com support. I recommend 2.6.0 rc1.
« Last Edit: December 16, 2011, 06:45:05 pm by ludob »

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #4 on: December 16, 2011, 07:49:34 pm »
You are right ludob this really sounds difficult but it is maybe solution for me since i don't programming in VB for a few years now but know the basics what are you talking about. :)
I really don't know how you figured this out?
Also did't know for comobj library in fpc this is really amazing news for me.
Is somewhere exist some more information about COM and ole classes in fpc?
Thank you a lot for an example.
I'll try to figure this way of COM programing. :)
ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #5 on: December 16, 2011, 08:30:08 pm »
Quote
Is somewhere exist some more information about COM and ole classes in fpc?
Com is a vast subject. There are a few articles around about delphi and ole automation that you can use as an introduction to this late binding COM method.
Fe. http://delphi.about.com/cs/adptips2003/a/bltip0303_5.htm, http://edn.embarcadero.com/article/10126 
Fpc misses some tools that are included with Delphi but has the necessary compiler support to work with COM. An important difference is that in fpc you need to use OleVariant for COM instead of variant. There are small differences between the 2 which affect marshaling. Variant has some OLE incompatible subtypes.

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #6 on: December 18, 2011, 12:28:08 am »
Here is a problem Object Pascal doesn't have for-each loop so i can't pass values from set from olevariant or variant type.
I tried to use late binding from this page http://msdn.microsoft.com/en-us/library/windows/desktop/aa446865%28v=VS.85%29.aspx
Code: [Select]
procedure TForm1.btn1Click(Sender: TObject);
var
  service,rootFolder,numberOfTasks,TaskCollection,MyTask :OleVariant;
  i:Integer;
begin
  service := CreateOleObject('Schedule.Service');
  service.Connect;
  rootFolder := service.GetFolder(OleVariant('\'));
  TaskCollection := rootFolder.GetTasks(0);
  numberOfTasks := TaskCollection.Count;
  if numberOfTasks = 0 then ShowMessage('there is nothing') else
  begin
    For i:=0 to numberOfTasks - 1 do
        ShowMessage('where are tasks im not for each in loop');
  end;
end;

Is it posible to enumerate values on some another way?

My logic is to make
Code: [Select]
For MyTask in TaskCollection do
        ShowMessage('test');

but it is not posible anyway on variant :)
« Last Edit: December 18, 2011, 12:39:02 am by nicke85 »
ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
Re: Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #7 on: December 18, 2011, 10:16:15 am »
Solved!!!
Here is referenced loop
Code: [Select]
for i:= 1 to numberOfTasks do
      ShowMessage(TaskCollection.item[i].name);

Thanks again ludob :)
« Last Edit: December 18, 2011, 10:22:24 am by nicke85 »
ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #8 on: December 18, 2011, 12:45:04 pm »
Code: [Select]
for i:= 1 to numberOfTasks do
      ShowMessage(TaskCollection.item[i].name);
That is indeed a workaround. I'm not sure that all collections are 1 based, though.
The better way is to use the IEnumVariant interface. I have ported TEnumVariant from comlib.pas (http://www.techvanguards.com/com/resources/downloads.asp) to fpc. Attached is the file.
To use it:
Code: [Select]
uses enumvariant;
...
var task olevariant;
...
with TEnumVariant.Create(taskcollection) do
  try
    while ForEach(task) do
       ShowMessage(task.name);
  finally
    free;
  end;

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #9 on: December 18, 2011, 02:01:13 pm »
Thanks,
Curently now I work with pure OleObject from your example. :)
I have edited taskschd.dll file and found all methods related for TaskSchreduler class.It is performed for C++ but as you say it can be used in late binding like in VB style.

If I make reference:
Code: [Select]
registeredTask := TaskFolderCollection.Item[3];
Then can use
Code: [Select]
ShowMessage(registeredTask.path); // to see path
registeredTask.Run(NULL); // to run task
rootFolder.DeleteTask(registeredTask.Name, 0); // to delete task

And this is a curently working code,I try to figure enumerated tasks
Code: [Select]
for i := 1 to numberOfTasks do
      begin
       Memo1.Lines.Add(TaskCollection.Item[i].Name);
       Memo1.Lines.Add(TaskCollection.Item[i].NextRunTime);
..etc..

The Idea is to set tasks into stringgrid and then to make reference between rootfolder enumerated tasks and stringgrid,to can use task index selection for current task and modify task.

I think that it is more simpler for me than interfaced object.
If you have some more sugestion go agead and tell I would like to figure out interface method
but it early binding style :)
ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #10 on: December 18, 2011, 03:04:18 pm »
The enumvariant unit works also with late binding. The code snippet
Code: [Select]
with TEnumVariant.Create(taskcollection) do
  try
    while ForEach(task) do
       ShowMessage(task.name);
  finally
    free;
  end;
is a replacement for
Code: [Select]
  numberOfTasks := TaskCollection.Count;
  for i:= 1 to numberOfTasks do
      ShowMessage(TaskCollection.item[i].name);

Early binding is too complex for the program you want to create. Late binding has quite some overhead but for this type of applications it really doesn't matter (unless you want to create and delete a few hundred tasks per minute  ;) )

nicke85

  • Jr. Member
  • **
  • Posts: 92
  • #13#10
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #11 on: December 18, 2011, 03:43:38 pm »
You are right,i didn't figured loop.
So the next question is,can I use late binding to make universal,platform independent code for XP and Win7 at the same time?
I can't find nothing about API 1 classes on the internet there is only early binding and C++ wrapper on MSDN site.
Seems that API 2 is more eaisier for manipulate than API 1.

Regards
ArchLinux X64 (XFCE) & Windows 7 SP1 Ultimate X64
FPC 2.7.1 / Lazarus 1.1 / ZeosDBO / fortes4lazarus -- all svn

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #12 on: December 18, 2011, 04:28:03 pm »
mstask is a COM object but not dual interfaced. It is missing the IDispatch interface needed to do late binding. Only early binding is possible. There are some tricks to access mstasks with late binding through WMI but that would be more difficult than using the wrapper for API 1.0. It wouldn't give you a "universal" solution neither.
Looks like you will have to duplicate the code for the 2 api versions.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #13 on: December 18, 2011, 07:04:49 pm »
mstask is a COM object but not dual interfaced. It is missing the IDispatch interface needed to do late binding. Only early binding is possible. There are some tricks to access mstasks with late binding through WMI but that would be more difficult than using the wrapper for API 1.0. It wouldn't give you a "universal" solution neither.
Looks like you will have to duplicate the code for the 2 api versions.

I found a description of TLB files btw. Would that help for early binding?

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED] Task Schreduling in Windows 7 (API ver. 2) ??
« Reply #14 on: December 18, 2011, 07:17:14 pm »
Quote
I found a description of TLB files btw. Would that help for early binding?
The mstasks.pas wrapper is already the interface definition for API 1.0. FPC early binding with 1.0 was already possible.
Using D6 I created the bindings for API 2.0 but that needs some tweaking to get it working with fpc. If anybody is interested I can do that. It won't help in creating common code for the 2 API's though.

 

TinyPortal © 2005-2018