This project has moved. For the latest updates, please go here.

Can't Enumerate Tasks as non-Admin User in XP

Nov 11, 2009 at 6:49 PM
Edited Nov 11, 2009 at 7:36 PM

I was hoping someone could help point me in the right direction. I'm trying to enumerate all my currently scheduled Tasks as a standard user in XP. This code seems to work fine in Vista, and as an Administrator on XP, but not when I'm logged in with a standard user account. My code snippet look like the following:

 

TaskService s = new TaskService();
foreach (Task t in s.RootFolder.Tasks)
{
  if (string.Compare(((ExecAction)t.Definition.Actions[0]).Path, Application.ExecutablePath, true) == 0)
    myTaskList.Add(t);
}

 

The error I'm getting is:

Error Message: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at V1Interop.ITaskScheduler.Activate(String Name, Guid& riid)
at TaskCollection.V1TaskEnumerator.get_Current() in C:\Test\TaskScheduler\TaskCollection.cs:line 82

Any ideas?

Thanks in advance.
Coordinator
Nov 12, 2009 at 9:36 PM

If the task is created as administrator user, standard users will not have permission to access it. If it is created in a standard user environment, other standard users will not have permission to access it.

If the task with given name exists and user doesn't have access permisions to the job, Activate(..) call returns E_ACCESSDENIED. So the user cannot reschedule or delete it.

You must ensure the ACL of the job file under 1.0 has granted “full permission” for “everyone”. The Task.Path property will return the path of that file.

Nov 13, 2009 at 2:20 PM

Great, thanks for getting back to me.

I understand not being able to see/edit other users' tasks, but is there a way I can get a list of tasks that the current user has permission to? It seems that the very call to the Tasks property throws the exception, so I don't have a chance to look at any part of the collection. (Or perhaps there's something more fundamental that I don't understand.)

Basically what I'm trying to do is allow the user to schedule one or more instances of the application I'm working on. Multiple users may schedule it on a single PC. I want to provide each user with a list of their scheduled instances (so each user only sees his/her view on the universe). Can this be done?

Thanks again for any insight, and thanks for writing this excellent library.

Coordinator
Nov 16, 2009 at 7:57 PM
Edited Nov 16, 2009 at 8:05 PM

There was a problem with the enumerator for tasks under V1 for users without Admin rights. It is really a problem with the native library in that it will allow you to enumerate the names, but will provide names of tasks that you can't activate. I have modified the enumerator in the code (not the released build for now) so that it checks for access before allowing the enumeration.

Using the current library, you could modify your code to not use foreach and catch the exception, like this:

System.Collections.Generic.IEnumerator<Task> tasks = ts.RootFolder.Tasks.GetEnumerator();
while (tasks.MoveNext())
{
  Task t = null;
  try
  {
    t = tasks.Current;
    if (string.Compare(((ExecAction)t.Definition.Actions[0]).Path, Application.ExecutablePath, true) == 0)
      myTaskList.Add(t);
  }
  catch { }
}