コード例 #1
0
        public KillChildProcessJob()
        {
            //Return if the windows version is lower or equal to win7
            if (!IsSupportedWindowsVersion)
            {
                throw new NotSupportedException("KillChildProcessJob is not supported in your windows version.");
            }

            //Create default security attributes
            var securityAttributes = new Kernel32.SecurityAttributes
            {
                length = (uint)Marshal.SizeOf <Kernel32.SecurityAttributes>()
            };

            //Create a job handle
            _jobHandle = JobHandle.CreateJob(securityAttributes, "DM_KCP_" + Guid.NewGuid());

            //Create basic limit infos
            var jobBasicLimitInformation = new Kernel32.JobObjectBasicLimitInformation()
            {
                LimitFlags = Kernel32.JobObjectlimit.KillOnJobClose
            };

            //Create extended limit infos
            var jobExtendedLimitInformation = new Kernel32.JobObjectExtendedLimitInformation()
            {
                BasicLimitInformation = jobBasicLimitInformation
            };

            //Set the information for the job handle
            int    length = Marshal.SizeOf <Kernel32.JobObjectExtendedLimitInformation>();
            IntPtr jobExtendedLimitInformationHandle = Marshal.AllocHGlobal(length);

            try
            {
                Marshal.StructureToPtr(jobExtendedLimitInformation, jobExtendedLimitInformationHandle, false);

                _jobHandle.SetInformation(Kernel32.JobObjectInfoType.ExtendedLimitInformation, jobExtendedLimitInformationHandle, (uint)length);
            }
            finally
            {
                Marshal.FreeHGlobal(jobExtendedLimitInformationHandle);
            }
        }