栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

.net中的CreateJobObject / SetInformationJobObject pinvoke的工作示例?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net中的CreateJobObject / SetInformationJobObject pinvoke的工作示例?

这可能会晚一点,但仍然如此。

我在这里尝试了所有示例,但是没有人同时在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.Unipre)]        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}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号