在.NET中使用CreateJobObject/SetInformationJobObject的工作示例是什么?

42

我正在努力组合一个可用的CreateJobObject和SetInformationJobObject的PInvoke示例。通过各种谷歌搜索(包括俄语和中文帖子!),我拼凑出以下代码。我认为JOBOBJECT_BASIC_LIMIT_INFORMATION的定义根据平台(32/64位)而变化。CreateJobObject / AssignProcessToJobObject似乎可以工作。SetInformationJobObject失败 - 可能会出现错误24或87。

Process myProcess // POPULATED SOMEWHERE ELSE

// Create Job & assign this process and another process to the job
IntPtr jobHandle = CreateJobObject( null , null );
AssignProcessToJobObject( jobHandle , myProcess.Handle );
AssignProcessToJobObject( jobHandle , Process.GetCurrentProcess().Handle );

// Ensure that killing one process kills the others                
JOBOBJECT_BASIC_LIMIT_INFORMATION limits = new JOBOBJECT_BASIC_LIMIT_INFORMATION();
limits.LimitFlags = (short)LimitFlags.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
IntPtr pointerToJobLimitInfo = Marshal.AllocHGlobal( Marshal.SizeOf( limits ) );
Marshal.StructureToPtr( limits , pointerToJobLimitInfo , false );   
SetInformationJobObject( job , JOBOBJECTINFOCLASS.JobObjectBasicLimitInformation , pionterToJobLimitInfo ,  ( uint )Marshal.SizeOf( limits ) )
...


        [DllImport( "kernel32.dll" , EntryPoint = "CreateJobObjectW" , CharSet = CharSet.Unicode )]
        public static extern IntPtr CreateJobObject( SecurityAttributes JobAttributes , string lpName );

        public class SecurityAttributes
        {

            public int nLength; //Useless field = 0
            public IntPtr pSecurityDescriptor; //хз))
            public bool bInheritHandle; //Возможность наследования

            public SecurityAttributes()
            {
                this.bInheritHandle = true;
                this.nLength = 0;
                this.pSecurityDescriptor = IntPtr.Zero;
            }
        }

        [DllImport( "kernel32.dll" )]
        static extern bool SetInformationJobObject( IntPtr hJob , JOBOBJECTINFOCLASS JobObjectInfoClass , IntPtr lpJobObjectInfo , uint cbJobObjectInfoLength );

        public enum JOBOBJECTINFOCLASS
        {
            JobObjectAssociateCompletionPortInformation = 7 ,
            JobObjectBasicLimitInformation = 2 ,
            JobObjectBasicUIRestrictions = 4 ,
            JobObjectEndOfJobTimeInformation = 6 ,
            JobObjectExtendedLimitInformation = 9 ,
            JobObjectSecurityLimitInformation = 5
        }


        [StructLayout( LayoutKind.Sequential )]
        struct JOBOBJECT_BASIC_LIMIT_INFORMATION
        {
            public Int64 PerProcessUserTimeLimit;
            public Int64 PerJobUserTimeLimit;
            public Int16 LimitFlags;
            public UIntPtr MinimumWorkingSetSize;
            public UIntPtr MaximumWorkingSetSize;
            public Int16 ActiveProcessLimit;
            public Int64 Affinity;
            public Int16 PriorityClass;
            public Int16 SchedulingClass;
        }

        public enum LimitFlags
        {
            JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 0x00000008 ,
            JOB_OBJECT_LIMIT_AFFINITY = 0x00000010 ,
            JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x00000800 ,
            JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION = 0x00000400 ,
            JOB_OBJECT_LIMIT_JOB_MEMORY = 0x00000200 ,
            JOB_OBJECT_LIMIT_JOB_TIME = 0x00000004 ,
            JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000 ,
            JOB_OBJECT_LIMIT_PRESERVE_JOB_TIME = 0x00000040 ,
            JOB_OBJECT_LIMIT_PRIORITY_CLASS = 0x00000020 ,
            JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x00000100 ,
            JOB_OBJECT_LIMIT_PROCESS_TIME = 0x00000002 ,
            JOB_OBJECT_LIMIT_SCHEDULING_CLASS = 0x00000080 ,
            JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK = 0x00001000 ,
            JOB_OBJECT_LIMIT_WORKINGSET = 0x00000001
        }


        [DllImport( "kernel32.dll" )]
        [return: MarshalAs( UnmanagedType.Bool )]
        static extern bool AssignProcessToJobObject( IntPtr hJob , IntPtr hProcess );

        [StructLayout( LayoutKind.Sequential )]
        public struct SECURITY_ATTRIBUTES
        {
            public int nLength;
            public IntPtr lpSecurityDescriptor;
            public int bInheritHandle;
        }

JOBOBJECT_BASIC_LIMIT_INFORMATION 定义了 LimitFlagsPriorityClassSchedulingClassDWORD 类型,这对应于 Int32 - Kevin Smyth
4个回答

91

这可能有点晚了,但仍然可以用。

我尝试了这里的所有示例,但没有一个能够在32位和64位模式下同时工作。最终,我需要自己检查所有签名并创建相应的PInvoke例程。我认为,其他人可能会发现这很有帮助。

免责声明:解决方案基于Matt Howells的答案

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace JobManagement
{
    public class Job : IDisposable
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr CreateJobObject(IntPtr a, string lpName);

        [DllImport("kernel32.dll")]
        static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, UInt32 cbJobObjectInfoLength);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool CloseHandle(IntPtr hObject);

        private IntPtr handle;
        private bool disposed;

        public Job()
        {
            handle = CreateJobObject(IntPtr.Zero, null);

            var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION
            {
                LimitFlags = 0x2000
            };

            var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
            {
                BasicLimitInformation = info
            };

            int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
            IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length);
            Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

            if (!SetInformationJobObject(handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr, (uint)length))
                throw new Exception(string.Format("Unable to set information.  Error: {0}", Marshal.GetLastWin32Error()));
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing) { }

            Close();
            disposed = true;
        }

        public void Close()
        {
            CloseHandle(handle);
            handle = IntPtr.Zero;
        }

        public bool AddProcess(IntPtr processHandle)
        {
            return AssignProcessToJobObject(handle, processHandle);
        }

        public bool AddProcess(int processId)
        {
            return AddProcess(Process.GetProcessById(processId).Handle);
        }

    }

    #region Helper classes

    [StructLayout(LayoutKind.Sequential)]
    struct IO_COUNTERS
    {
        public UInt64 ReadOperationCount;
        public UInt64 WriteOperationCount;
        public UInt64 OtherOperationCount;
        public UInt64 ReadTransferCount;
        public UInt64 WriteTransferCount;
        public UInt64 OtherTransferCount;
    }


    [StructLayout(LayoutKind.Sequential)]
    struct JOBOBJECT_BASIC_LIMIT_INFORMATION
    {
        public Int64 PerProcessUserTimeLimit;
        public Int64 PerJobUserTimeLimit;
        public UInt32 LimitFlags;
        public UIntPtr MinimumWorkingSetSize;
        public UIntPtr MaximumWorkingSetSize;
        public UInt32 ActiveProcessLimit;
        public UIntPtr Affinity;
        public UInt32 PriorityClass;
        public UInt32 SchedulingClass;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SECURITY_ATTRIBUTES
    {
        public UInt32 nLength;
        public IntPtr lpSecurityDescriptor;
        public Int32 bInheritHandle;
    }

    [StructLayout(LayoutKind.Sequential)]
    struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
    {
        public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;
        public IO_COUNTERS IoInfo;
        public UIntPtr ProcessMemoryLimit;
        public UIntPtr JobMemoryLimit;
        public UIntPtr PeakProcessMemoryUsed;
        public UIntPtr PeakJobMemoryUsed;
    }

    public enum JobObjectInfoType
    {
        AssociateCompletionPortInformation = 7,
        BasicLimitInformation = 2,
        BasicUIRestrictions = 4,
        EndOfJobTimeInformation = 6,
        ExtendedLimitInformation = 9,
        SecurityLimitInformation = 5,
        GroupInformation = 11
    }

    #endregion

}

2
Win32.CloseHandle(handle); 是从 kernel32.dll 导入的,是吗?您的代码(除了转换为 VB.NET 之外没有修改)在没有外部引用的情况下无法编译通过,尽管我正在使用 .NET 2.0。我注意到您和 Matt 的答案都没有显式导入它,我想知道我错过了什么。 - Esoteric Screen Name
2
我在Win32.CloseHandle中使用了this。我仍然无法让它工作,诀窍是要添加一个app.manifest 如此描述。请注意,这在VS调试器中不起作用。您必须单独启动父进程。 - Mas
Alexander,你真是一个救命恩人!我使用了Matt的例子,但遇到了一些问题(可能是因为它是x64),而你的更新解决了这个问题!我正在使用JobObject从Windows服务中生成进程,效果很好。 - Adam Plocher
太棒了,tjamls、Alexander 和 Mas!替换 Win32.CloseHandle 的方法行得通了。 [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CloseHandle(IntPtr hObject);这个方法解决了问题。 - Eleasar
1
这个能在.NET Core中使用吗? - Psddp
显示剩余10条评论

9
改进了Alexander的答案,这里提供一个使用SafeHandle的版本。这是一个CriticalFinalizerObject,使得处理句柄更加安全。.NET API(如Process类)总是在P/Invoke时使用SafeHandle而不是IntPtr
internal sealed class ChildProcessManager : IDisposable
{
    private SafeJobHandle _handle;
    private bool _disposed;

    public ChildProcessManager()
    {
        _handle = new SafeJobHandle(CreateJobObject(IntPtr.Zero, null));

        var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION
        {
            LimitFlags = 0x2000
        };

        var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION
        {
            BasicLimitInformation = info
        };

        var length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
        var extendedInfoPtr = Marshal.AllocHGlobal(length);
        Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false);

        if (!SetInformationJobObject(_handle, JobObjectInfoType.ExtendedLimitInformation, extendedInfoPtr,
                (uint)length))
        {
            throw new InvalidOperationException($"Unable to set information.  Error: {Marshal.GetLastWin32Error()}");
        }
    }

    public void Dispose()
    {
        if (_disposed) return;

        _handle.Dispose();
        _handle = null;
        _disposed = true;
    }

    private void ValidateDisposed()
    {
        if (_disposed) throw new ObjectDisposedException(nameof(ChildProcessManager));
    }

    public void AddProcess(SafeProcessHandle processHandle)
    {
        ValidateDisposed();
        if (!AssignProcessToJobObject(_handle, processHandle))
        {
            throw new InvalidOperationException("Unable to add the process");
        }
    }

    public void AddProcess(Process process)
    {
        AddProcess(process.SafeHandle);
    }

    public void AddProcess(int processId)
    {
        using (var process = Process.GetProcessById(processId))
        {
            AddProcess(process);
        }
    }

    #region Safe Handle

    // ReSharper disable once ClassNeverInstantiated.Local
    private sealed class SafeJobHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        public SafeJobHandle(IntPtr handle) : base(true)
        {
            SetHandle(handle);
        }

        protected override bool ReleaseHandle()
        {
            return CloseHandle(handle);
        }

        [DllImport("kernel32", SetLastError = true)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        private static extern bool CloseHandle(IntPtr hObject);
    }

    #endregion

    #region Win32
    // ReSharper disable InconsistentNaming

    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    private static extern IntPtr CreateJobObject(IntPtr a, string lpName);

    [DllImport("kernel32")]
    private static extern bool SetInformationJobObject(SafeJobHandle hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);

    [DllImport("kernel32", SetLastError = true)]
    private static extern bool AssignProcessToJobObject(SafeJobHandle job, SafeProcessHandle process);

    [StructLayout(LayoutKind.Sequential)]
    internal struct IO_COUNTERS
    {
        public ulong ReadOperationCount;
        public ulong WriteOperationCount;
        public ulong OtherOperationCount;
        public ulong ReadTransferCount;
        public ulong WriteTransferCount;
        public ulong OtherTransferCount;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct JOBOBJECT_BASIC_LIMIT_INFORMATION
    {
        public long PerProcessUserTimeLimit;
        public long PerJobUserTimeLimit;
        public uint LimitFlags;
        public UIntPtr MinimumWorkingSetSize;
        public UIntPtr MaximumWorkingSetSize;
        public uint ActiveProcessLimit;
        public UIntPtr Affinity;
        public uint PriorityClass;
        public uint SchedulingClass;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SECURITY_ATTRIBUTES
    {
        public uint nLength;
        public IntPtr lpSecurityDescriptor;
        public int bInheritHandle;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
    {
        public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;
        public IO_COUNTERS IoInfo;
        public UIntPtr ProcessMemoryLimit;
        public UIntPtr JobMemoryLimit;
        public UIntPtr PeakProcessMemoryUsed;
        public UIntPtr PeakJobMemoryUsed;
    }

    public enum JobObjectInfoType
    {
        AssociateCompletionPortInformation = 7,
        BasicLimitInformation = 2,
        BasicUIRestrictions = 4,
        EndOfJobTimeInformation = 6,
        ExtendedLimitInformation = 9,
        SecurityLimitInformation = 5,
        GroupInformation = 11
    }

    // ReSharper restore InconsistentNaming
    #endregion
}

这在_handle.Dispose()处出现编译错误。 - Arundale Ramanathan

5
总结一下,Alexander Yezutov提供的签名适用于x86和x64。Matt Howells的签名在应该使用UIntPtr时使用了许多UInt32。我使用了以下P/Invoke签名来关闭句柄,看起来运行良好:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject);

以下内容必须按照Mas的帖子添加到app.manifest中:
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
  <!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->

    <!--The ID below indicates application support for Windows Vista -->
    <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>

    <!--The ID below indicates application support for Windows 7 -->
    <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>

</application>

最后,如果从Visual Studio启动,这个方法不会生效(至少在Win 7下)。父进程必须从Windows资源管理器中启动。

Andrew - 感谢您的贡献。我对这个问题有点远离,所以无法验证,但听起来您知道自己在说什么 =) - Suraj

4

奇怪。我尝试了这个,但是和所有其他的P/Invoke解决方案一样,我无法在64位上使其工作。 - user314104

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接