.net中的CreateJobObject / SetInformationJobObject

我正在努力拼凑一个拼凑的CreateJobObject和SetInformationJobObject的工作示例。通过各种Google搜索(包括俄语和中文帖子!),我将以下代码拼凑在一起。我认为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 );

LEATH
浏览 934回答 3
3回答

qq_遁去的一_1

总而言之,亚历山大·叶祖托夫(Alexander Yezutov)构成的签名在x86和x64上均可工作。当应改用UIntPtr时,Matt Howells签名使用许多UInt32。我为CloseHandle使用了以下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>&nbsp; <!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->&nbsp; &nbsp; <!--The ID below indicates application support for Windows Vista -->&nbsp; &nbsp; <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>&nbsp; &nbsp; <!--The ID below indicates application support for Windows 7 -->&nbsp; &nbsp; <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/></application>最后,从Visual Studio启动时,这将不起作用(至少在Win 7下)。父进程必须从Windows资源管理器启动。
打开App,查看更多内容
随时随地看视频慕课网APP