Problem with RunJobProcessAs

Mar 24, 2009 at 5:27 PM
Hi,

I would like to create a job object being executed with a different user account. However, I get an exception on Windows XP SP3 when calling RunJobProcessAs.

Does the current user account need any certain privilege? I tried around by giving everyone the permission to replace a process level token but with no success.

Is anyone using this successfully? And does anyone know, if this project is still maintained?

This is the code (it is actually the sample provided with the current release):

namespace ConsoleApplicationTestJob
{
    class Program
    {
        static void Main(string[] args)
        {
            JobManagement.JobObject jo = new JobManagement.JobObject("Hello");
            try
            {
                // This line throws an error
                jo.Limits.RunJobProcessesAs(WindowsIdentity.GetCurrent().Token);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                jo.Limits.IsAdminProcessAllow = false;
                jo.CreateProcessSecured(new System.Diagnostics.ProcessStartInfo("calc.exe"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

And this is the exception stack trace:

   at JobManagement.JobObject.AssignProcessToJob(Process process) in c:\tmp\jobobjectwrapper sources\jobmanagement\jobmanagement.cpp:line 289
   at JobManagement.JobObject.InJobProcessActivationServiceClient.CreateActivationService() in c:\tmp\jobobjectwrapper sources\jobmanagement\jobmanagement.cpp:line 536
   at JobManagement.JobObject.InJobProcessActivationServiceClient..ctor(JobObject job) in c:\tmp\jobobjectwrapper sources\jobmanagement\jobmanagement.h:line 437
   at JobManagement.JobObject.CreateProcessSecured(ProcessStartInfo startupInfo) in c:\tmp\jobobjectwrapper sources\jobmanagement\jobmanagement.cpp:line 199
   at ConsoleApplicationTestJob.Program.Main(String[] args) in C:\tmp\JobObjectWrapper Sources\ConsoleApplicationTestJob\Program.cs:line 65
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

Thanks for any help,
divo
Mar 24, 2009 at 6:53 PM
I fixed the problem above by giving the starting user the permission to replace a process level token and the log off and log in again (Starting a new session did the thing)

However, now I receive an unhandled exception from InJobProcessCreator.exe.

The code only works when I pass WindowsIdentity.GetCurrent().Token into RunJobProcessesAs, but I want to start the child process as a different user. To do so I create a token by  calling LogonUser:

    LogonUser(username, domain, password, (int)LogonType.LOGON32_LOGON_BATCH, (int)LogonProvider.LOGON32_PROVIDER_DEFAULT, out _tokenHandle)

Do I have to set up any special permissions for this user account as well?

Thanks for any help
divo