Example #1
0
        public WindowsProcessJob(Process process)
        {
            IntPtr newHandle = Native.CreateJobObject(IntPtr.Zero, null);

            if (newHandle == IntPtr.Zero)
            {
                throw new InvalidOperationException("Unable to create a job.  Error: " + Marshal.GetLastWin32Error());
            }

            this.jobHandle = new SafeJobHandle(newHandle);

            Native.JOBOBJECT_BASIC_LIMIT_INFORMATION info = new Native.JOBOBJECT_BASIC_LIMIT_INFORMATION
            {
                LimitFlags = 0x2000 // JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
            };

            Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION extendedInfo = new Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION
            {
                BasicLimitInformation = info
            };

            int length = Marshal.SizeOf(typeof(Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION));

            if (!Native.SetInformationJobObject(this.jobHandle, Native.JobObjectInfoType.ExtendedLimitInformation, ref extendedInfo, (uint)length))
            {
                throw new InvalidOperationException("Unable to configure the job.  Error: " + Marshal.GetLastWin32Error());
            }

            if (!Native.AssignProcessToJobObject(this.jobHandle, process.Handle))
            {
                throw new InvalidOperationException("Unable to add process to the job.  Error: " + Marshal.GetLastWin32Error());
            }
        }
Example #2
0
        /// <summary>
        /// Assign a process to a new Job.
        /// </summary>
        /// <param name="hProcess">Process handle</param>
        /// <param name="name">Optional - Job name</param>
        /// <returns>Job handle or IntPtr.Zero if failed</returns>
        private static bool AssignProcessToNewJob(IntPtr hProcess, string name, out IntPtr hJob)
        {
            //Create a new Job
            hJob = Native.CreateJobObject(IntPtr.Zero, name);
            if (hJob == IntPtr.Zero)
            {
                return(false);
            }

            var jeli = new Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION();

            jeli.BasicLimitInformation.LimitFlags  = Native.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
            jeli.BasicLimitInformation.LimitFlags |= Native.JOB_OBJECT_LIMIT_BREAKAWAY_OK;

            bool success = Native.SetInformationJobObject(hJob
                                                          , Native.JOB_EXTENDED_LIMIT_INFORMATION
                                                          , ref jeli
                                                          , Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION.SIZE);

            //Assign the process to the Job
            if (!success || !Native.AssignProcessToJobObject(hJob, hProcess))
            {
                Native.CloseHandle(hJob);
                hJob = IntPtr.Zero;
                return(false);
            }

            return(true);
        }
Example #3
0
        /// <summary>
        /// Assign a process to a new Job.
        /// </summary>
        /// <param name="hProcess">Process handle</param>
        /// <param name="name">Optional - Job name</param>
        /// <returns>Job handle or IntPtr.Zero if failed</returns>
        private static IntPtr AssignProcessToNewJob(IntPtr hProcess, string name) {

            //Create a new Job
            IntPtr hJob = Native.CreateJobObject(IntPtr.Zero, name);
            if (hJob == IntPtr.Zero)
                return IntPtr.Zero;

            var jeli = new Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION();
            jeli.BasicLimitInformation.LimitFlags = Native.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
            jeli.BasicLimitInformation.LimitFlags |= Native.JOB_OBJECT_LIMIT_BREAKAWAY_OK;

            bool success = Native.SetInformationJobObject(hJob
                , Native.JOB_EXTENDED_LIMIT_INFORMATION
                , ref jeli
                , Native.JOBOBJECT_EXTENDED_LIMIT_INFORMATION.SIZE);

            //Assign the process to the Job
            if (!success || !Native.AssignProcessToJobObject(hJob, hProcess)) {
                Native.CloseHandle(hJob);
                return IntPtr.Zero;
            }

            return hJob;
        }