I’m trying to get Target Info from Windows Scheduled Tasks .job files.
%SystemRoot%\Tasks
Is this easily possible with PureBasic?
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;
}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