We help IT Professionals succeed at work.

Delphi create and access TaskScheduler subfolder

Stef Merlijn
Stef Merlijn asked
on
304 Views
Last Modified: 2017-11-13
Hi,

I'm trying to create subfolder in the TaskScheduler (using Delphi) and create schedules for my application inside the subfolder.
For TaskScheduler-access I use this EE-example.

To create a subfolder I found method TaskFolder.CreateFolder('\MyFolder', ?sddl?);.
Here sddl (=security descriptor associated with the folder) must be set, but I have no idea how to set it correctly.

So as a workaround I tried to create the folder "MyFolder" directly into folder "C:\Windows\System32\tasks\".
The folder is actually created by my application, but the folder is not visible inside Windows TaskScheduler.

procedure TForm1.Button1Click(Sender: TObject);
var ts : TTaskScheduler;
    tf: ITaskFolder;
begin
  CoInitialize(nil);                    // uses ActiveX
  ts := TTaskScheduler.Create(Self);
  try
    ts.AutoConnect := True;
    ts.Connect1(GetNetComputerName);
    if ts.Connected then
    begin

      // ts.GetFolder('\MyFolder', ?sddl? );   << THIS DOESN'T WORK
      // As a workaround I create the folder myself.
      // System32 is changed into SysNative, as otherwise the folder would be created in SysWOW64.
      ForceDirectories('C:\Windows\SysNative\tasks\MyFolder\');

      if DirectoryExists('C:\Windows\SysNative\tasks\MyFolder\') then
        tf := ts.GetFolder('\MyFolder')   // << THIS GIVES AN UNKNOWN ERROR
      else
        tf := ts.GetFolder('\');

Open in new window

How can I create a subfolder for taskscheduler in order to add my application's schedules inside that subfolder?
Comment
Watch Question

Sinisa VukSoftware architect
CERTIFIED EXPERT
Top Expert 2012

Commented:
Ah, you are using my old example... For your problem you need to add following differences:
var 
    ts : TTaskScheduler;
    tf: ITaskFolder;
    rootf: ITaskFolder;
   ....
    v: OleVariant;
begin
....

  CoInitialize(nil);
  ts := TTaskScheduler.Create(Self);
  try
    ts.AutoConnect := True;
    ts.Connect1(GetNetComputerName);
    if ts.Connected then
    begin
      rootf := ts.GetFolder('\');

      tf := nil;
      try
        tf := rootf.GetFolder('\MyFolder');
      except
       //if folder not exists - exception will raise...
      end;

      if not Assigned(tf) then
      begin
        TVariantArg(v).VT := VT_BSTR;
        //add rw access as string in SDDL_REVISION_1 format
        TVariantArg(v).bstrVal := StringToOleStr('D:(A;OICI;GRGW;;;AU)');  //discretionary ACL to allow read/write access for authenticated users
        tf := rootf.CreateFolder('\MyFolder', v);
      end;

      ....

Open in new window


Using MS ref on CreateFolder and Security Descriptor String Format def....
Stef MerlijnDeveloper

Author

Commented:
Hi Sinisa Vuk,
Your solution works partly.

It does create a folder, but when I open TaskScheduler itself I cannot delete the task. It sais I'm not authorised to do so (admin-user). And therefore I cannot delete the folder either.
Do you have a solution for that?
I've tried fiddling with the sddl settings, but that is complicated stuff hahaha, how the *** do you know that stuff? Is there some detailed info?

Also: how can I delete the created folder by code?
Software architect
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Sinisa VukSoftware architect
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Stef MerlijnDeveloper

Author

Commented:
@Sinisa Vuk: Thank you very much.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions