Windows Scheduled Tasks

Just starting out? Need help? Post your questions and find answers here.
Phant0m``

Windows Scheduled Tasks

Post by Phant0m`` »

Hey

I’m trying to get Target Info from Windows Scheduled Tasks .job files.
%SystemRoot%\Tasks

Is this easily possible with PureBasic? :(
Phant0m``

Post by Phant0m`` »

Well here is C/C++ Code

Code: Select all

#include <windows.h>
#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#include <wchar.h>

#define TASKS_TO_RETRIEVE          5


int main(int argc, char **argv)
{
  HRESULT hr = S_OK;
  ITaskScheduler *pITS;
  
  
  /////////////////////////////////////////////////////////////////
  // Call CoInitialize to initialize the COM library and 
  // then CoCreateInstance to get the Task Scheduler object. 
  /////////////////////////////////////////////////////////////////
  hr = CoInitialize(NULL);
  if (SUCCEEDED(hr))
  {
    hr = CoCreateInstance(CLSID_CTaskScheduler,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_ITaskScheduler,
                          (void **) &pITS);
    if (FAILED(hr))
    {
      CoUninitialize();
      return hr;
    }
  }
  else
  {
    return hr;
  }
  
  /////////////////////////////////////////////////////////////////
  // Call ITaskScheduler::Enum to get an enumeration object.
  /////////////////////////////////////////////////////////////////
  IEnumWorkItems *pIEnum;
  hr = pITS->Enum(&pIEnum);
  pITS->Release();
  if (FAILED(hr))
  {
    CoUninitialize();
    return hr;
  }
  
  /////////////////////////////////////////////////////////////////
  // Call IEnumWorkItems::Next to retrieve tasks. Note that 
  // this example tries to retrieve five tasks for each call.
  /////////////////////////////////////////////////////////////////
  LPWSTR *lpwszNames;
  DWORD dwFetchedTasks = 0;
  while (SUCCEEDED(pIEnum->Next(TASKS_TO_RETRIEVE,
                                &lpwszNames,
                                &dwFetchedTasks))
                  && (dwFetchedTasks != 0))
  {
    ///////////////////////////////////////////////////////////////
    // Process each task. Note that this example prints the 
    // name of each task to the screen.
    //////////////////////////////////////////////////////////////
    while (dwFetchedTasks)
    {
       wprintf(L"%s\n", lpwszNames[--dwFetchedTasks]);
       CoTaskMemFree(lpwszNames[dwFetchedTasks]);
    }
    CoTaskMemFree(lpwszNames);
  }
  
  pIEnum->Release();
  CoUninitialize();
  return S_OK;
}
http://msdn.microsoft.com/library/defau ... _tasks.asp

Main Index http://msdn.microsoft.com/library/defau ... frame=true
Kris_a
User
User
Posts: 92
Joined: Sun Feb 15, 2004 8:04 pm
Location: Manchester, UK

Post by Kris_a »

in theory you should be able to convert most of those functions to PB format by adding a _ on the end, and converting the constants by putting a # before them :D
Phant0m``

Post by Phant0m`` »

Theory sounds interesting, but to much for newb like myself...
Kris_a
User
User
Posts: 92
Joined: Sun Feb 15, 2004 8:04 pm
Location: Manchester, UK

Post by Kris_a »

haha :D

I'll see what I can do for you

edit: hmm, a lot of these structures are missing from PB o_O

help anyone? :?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Yes, the needed interface definitions are not included with PB.
But the Interfac Importer provides a quick way to get such :)

here's the code:

Code: Select all

; IEnumWorkItems interface definition
;
Interface IEnumWorkItems Extends IUnknown
  Next(a.l, b.l, c.l)
  Skip(a.l)
  Reset()
  Clone(a.l)
EndInterface

; ITaskScheduler interface definition
;
Interface ITaskScheduler Extends IUnknown
  SetTargetComputer(a.l)
  GetTargetComputer(a.l)
  Enum(a.l)
  Activate(a.l, b.l, c.l)
  Delete(a.l)
  NewWorkItem(a.l, b.l, c.l, d.l)
  AddWorkItem(a.l, b.l)
  IsOfType(a.l, b.l)
EndInterface


; ITask interface definition
;
Interface ITask Extends IUnknown
  CreateTrigger(a.l, b.l)
  DeleteTrigger(a.l)
  GetTriggerCount(a.l)
  GetTrigger(a.l, b.l)
  GetTriggerString(a.l, b.l)
  GetRunTimes(a.l, b.l, c.l, d.l)
  GetNextRunTime(a.l)
  SetIdleWait(a.l, b.l)
  GetIdleWait(a.l, b.l)
  Run()
  Terminate()
  EditWorkItem(a.l, b.l)
  GetMostRecentRunTime(a.l)
  GetStatus(a.l)
  GetExitCode(a.l)
  SetComment(a.l)
  GetComment(a.l)
  SetCreator(a.l)
  GetCreator(a.l)
  SetWorkItemData(a.l, b.l)
  GetWorkItemData(a.l, b.l)
  SetErrorRetryCount(a.l)
  GetErrorRetryCount(a.l)
  SetErrorRetryInterval(a.l)
  GetErrorRetryInterval(a.l)
  SetFlags(a.l)
  GetFlags(a.l)
  SetAccountInformation(a.l, b.l)
  GetAccountInformation(a.l)
  SetApplicationName(a.l)
  GetApplicationName(a.l)
  SetParameters(a.l)
  GetParameters(a.l)
  SetWorkingDirectory(a.l)
  GetWorkingDirectory(a.l)
  SetPriority(a.l)
  GetPriority(a.l)
  SetTaskFlags(a.l)
  GetTaskFlags(a.l)
  SetMaxRunTime(a.l)
  GetMaxRunTime(a.l)
EndInterface

CoInitialize_(0)

If CoCreateInstance_(?CLSID_CTaskScheduler, 0, 1, ?IID_ITaskScheduler, @TaskScheduler.ITaskScheduler) = #S_OK

  If TaskScheduler\Enum(@EnumTasks.IEnumWorkItems) = #S_OK
  
    While EnumTasks\Next(1, @*lpName.LONG, 0) = #S_OK
    
      length = WideCharToMultiByte_(#CP_ACP, 0, *lpName\l, -1, 0, 0, 0, 0)
      Filename$ = Space(length)
      WideCharToMultiByte_(#CP_ACP, 0, *lpName\l, -1, @Filename$, length, 0, 0)        
            
      ;==========================================
      ; Here you have the filename of a .job file
      ;==========================================
      ;
      Debug Filename$
      
      
      If TaskScheduler\Activate(*lpName\l, ?IID_ITask, @Task.ITask) = #S_OK
        
        If Task\GetApplicationName(@AppName) = #S_OK
          length = WideCharToMultiByte_(#CP_ACP, 0, AppName, -1, 0, 0, 0, 0)
          AppName$ = Space(length)
          WideCharToMultiByte_(#CP_ACP, 0, AppName, -1, @AppName$, length, 0, 0)        
          
          
          ;==================================
          ; Here you have the target filename
          ; =================================
          ;
          Debug "Target: "+AppName$
          
          
          CoTaskMemFree_(AppName)
        EndIf
      
        Task\Release()
      EndIf                    
    
      CoTaskMemFree_(*lpName\l)
      CoTaskMemFree_(*lpName)
    
    Wend    
  
    EnumTasks\Release()
  EndIf

  TaskScheduler\Release()
EndIf

CoUninitialize_()


DataSection

  IID_ITaskScheduler:  ; {148BD527-A2AB-11CE-B11F-00AA00530503}
    Data.l $148BD527
    Data.w $A2AB, $11CE
    Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03

  CLSID_CTaskScheduler: ;  {148BD52A-A2AB-11CE-B11F-00AA00530503}
    Data.l $148BD52A
    Data.w $A2AB, $11CE
    Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03
    
  IID_ITask:  ; {148BD524-A2AB-11CE-B11F-00AA00530503}
    Data.l $148BD524
    Data.w $A2AB, $11CE
    Data.b $B1, $1F, $00, $AA, $00, $53, $05, $03    

EndDataSection
[EDIT]

Extended the code to also display the target filename.

[/EDIT]

Timo
quidquid Latine dictum sit altum videtur
Phant0m``

Post by Phant0m`` »

Thanks Kris_a ….

Hey Freak

Thank you very much; I really appreciate your support on here.
If it isn’t much troubles when you have some time to tweak this up for me to provide two further info such as whether item is currently Active and what’s currently selected for "Schedule Task:" such like "At System Startup"...

Thanks again Freak :!: :)
SeregaZ
Enthusiast
Enthusiast
Posts: 628
Joined: Fri Feb 20, 2009 9:24 am
Location: Almaty (Kazakhstan. not Borat, but Triple G)
Contact:

Re: Windows Scheduled Tasks

Post by SeregaZ »

i have two questions:
1. how to make your own task with this code?
2. it will work with W7 and W8?
Post Reply