Solved

Why NetScheduleJobAdd and NetScheduleJobEnum Lib "netapi32.dll" will use "SYSTEM" and not a User account

Posted on 2004-10-14
8
1,491 Views
Last Modified: 2013-12-03
I'm desperatly trying to enumerate sceduled tasks/jobs on a local computer.
But all aproaches i've tried will end up in only creating and enumerating jobs by an owner called "SYSTEM".
I cann't get them to use the User account as the shortcut "Add Job" in Windows\Tasks will.
The enumeration will only return jobs sceduled by "SYSTEM" and not by other user accounts.
I've tried different programming languages (Delphi, VB 6.0), searched at Microsoft.com, and questioned a lot of search engines.
I'v defined prototypes of NetScheduleJobEnum and NetScheduleJobAdd API using "netapi32.dll" running under Windows2000 or XP
This issue was already on ExpertsExchange, but no explanation nor solution is provided yet...
Please help...
0
Comment
Question by:Hebels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12310271
'NetScheduleJobAdd ()' does not take any information about users or accounts. If you want to do that, you'll have to use COM interfaces. See e.g. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/taskschd/taskschd/c_c_code_example_setting_task_account_information.asp ("C/C++ Code Example: Setting Task Account Information"):

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


int main(int argc, char **argv)
{
  HRESULT hr = S_OK;
 
  ///////////////////////////////////////////////////////////////////
  // Call CoInitialize to initialize the COM library and then
  // CoCreateInstance to get the Task Scheduler object.
  ///////////////////////////////////////////////////////////////////
  ITaskScheduler *pITS;
 
  hr = CoInitialize(NULL);
  if (SUCCEEDED(hr))
  {
    hr = CoCreateInstance(CLSID_CTaskScheduler,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_ITaskScheduler,
                          (void **) &pITS);
    if (FAILED(hr))
    {
      CoUninitialize();
      return 1;
    }
  }
  else
  {
    return 1;
  }
 
 
  ///////////////////////////////////////////////////////////////////
  // Call ITaskScheduler::Activate to get the Task object.
  ///////////////////////////////////////////////////////////////////
 
  ITask *pITask;
  LPCWSTR lpcwszTaskName;
  lpcwszTaskName = L"Test Task";
  hr = pITS->Activate(lpcwszTaskName,
                      IID_ITask,
                      (IUnknown**) &pITask);
 
  // Release the ITaskScheduler interface.
  pITS->Release();  
 
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITaskScheduler::Activate: ");
    wprintf(L"error = 0x%x\n",hr);
    CoUninitialize();
    return 1;
  }
 
 
  ///////////////////////////////////////////////////////////////////
  // Call ITask::SetAccountInformation to specify the account name
  // and the account password for Test Task.
  ///////////////////////////////////////////////////////////////////
  LPCWSTR pwszAccountName = L"myAccount";
  LPCWSTR pwszPassword = L"myPassword";
  hr = pITask->SetAccountInformation(pwszAccountName,
                                     pwszPassword);
 
  if (FAILED(hr))
  {
    wprintf(L"Failed calling ITask::SetAccountInformation: ");
    wprintf(L"error = 0x%x\n",hr);
    CoUninitialize();
    return 1;
  }
 
 
  ///////////////////////////////////////////////////////////////////
  // Call IPersistFile::Save to save the modified task to disk.
  ///////////////////////////////////////////////////////////////////
  IPersistFile *pIPersistFile;
 
  hr = pITask->QueryInterface(IID_IPersistFile,
                              (void **)&pIPersistFile);
 
  // Release the ITask interface.
  pITask->Release();  
 
  hr = pIPersistFile->Save(NULL,
                           TRUE);
 
  // Release the IPersistFile interface.
  pIPersistFile->Release();
 
  wprintf(L"Set the account name to %s and ", pwszAccountName);
  wprintf(L"and the account password to %s.\n", pwszPassword);  
  CoUninitialize();
  return 0;
}
0
 

Author Comment

by:Hebels
ID: 12310793
Thanks jkr,
for your quick responce.
I've tried your approach, but it will not modify the creator as listed in <Explorer> C:\Windows\Tasks\ column "Creator" it will modify the user account under wich the task will be executed as listed under Properties when doubleclicking a scheduled Task instead!
Now my Problem was that i couldn't enumerate all Tasks listed by the Windows explorer in the folder Windows\Tasks\ .
I'm interested in enumeration of all tasks or only those tasks that were created by "User"
I can only enumerate tasks that are created using the API NetScheduleJobAdd. Those tasks show a creator named "SYSTEM" and not the name of the logged in user
I suspect that's the reason why i can not, but how can i enumerate tasks that are created under the normal user account?
Please advise...
0
 

Author Comment

by:Hebels
ID: 12340629
While still looking for an answer i found that the following code will list all tasks of all users including "SYSTEM".
Now i'm not very familiar with C++ and from all languages i know this would be the last choise!
Can anyone help me do something like this in Delphi or VisualBasic?
Please advise how to implement this object class "CTaskScheduler" in another language than C++...

Working code fragment as console application:

// Schedule_Console.cpp : Definiert den Einsprungpunkt für die Konsolenanwendung.
//

#include "stdafx.h"
#include <objbase.h>
#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)
{
      printf("Hallo Welt!\n");

  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;
}
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Hebels
ID: 12357748
I've found a solution myself at http://www.delphi-jedi.org/APILIBRARY#windows "Taskscheduler.ZIP" by Marcel van Brakel.
so it will close this question...
0
 
LVL 86

Expert Comment

by:jkr
ID: 12534446
Just happened to come across http://msdn.microsoft.com/library/en-us/taskschd/taskschd/ischeduledworkitem_setcreator.asp ("IScheduledWorkItem::SetCreator") - maybe that's what you're looking for.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13023238
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question