为了账号安全,请及时绑定邮箱和手机立即绑定

.net中的CreateJobObject / SetInformationJobObject

.net中的CreateJobObject / SetInformationJobObject

LEATH 2019-10-29 10:35:08
我正在努力拼凑一个拼凑的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 jobIntPtr 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 );
查看完整描述

3 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

总而言之,亚历山大·叶祖托夫(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>

  <!-- 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资源管理器启动。


查看完整回答
反对 回复 2019-10-29
  • 3 回答
  • 0 关注
  • 933 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信