private void QueryProcessIds() { NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST processList = new NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST(); processList.NumberOfAssignedProcesses = NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST.MaxProcessListLength; processList.NumberOfProcessIdsInList = 0; processList.ProcessIdList = null; int processListLength = Marshal.SizeOf(typeof(NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST)); IntPtr processListPtr = Marshal.AllocHGlobal(processListLength); try { Marshal.StructureToPtr(processList, processListPtr, false); bool success = NativeMethods.QueryInformationJobObject(this.jobHandle, NativeMethods.JobObjectInfoClass.JobObjectBasicProcessIdList, processListPtr, (uint)processListLength, IntPtr.Zero); if (success == false) { int error = Marshal.GetLastWin32Error(); throw new Win32Exception(error, "QueryInformationJobObject failed."); } processList = (NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST)Marshal.PtrToStructure(processListPtr, typeof(NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST)); List <Process> pss = new List <Process>(); for (int i = 0; i < processList.NumberOfProcessIdsInList; i++) { try { pss.Add(Process.GetProcessById((int)processList.ProcessIdList[i])); } catch (ArgumentException) { } } this.jobProcesses = pss.ToArray(); } finally { Marshal.FreeHGlobal(processListPtr); } }
private void QueryProcessIds() { NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST processList = new NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST(); processList.NumberOfAssignedProcesses = NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST.MaxProcessListLength; processList.NumberOfProcessIdsInList = 0; processList.ProcessIdList = null; int processListLength = Marshal.SizeOf(typeof(NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST)); IntPtr processListPtr = Marshal.AllocHGlobal(processListLength); try { Marshal.StructureToPtr(processList, processListPtr, false); bool success = NativeMethods.QueryInformationJobObject(this.jobHandle, NativeMethods.JobObjectInfoClass.JobObjectBasicProcessIdList, processListPtr, (uint)processListLength, IntPtr.Zero); if (success == false) { int error = Marshal.GetLastWin32Error(); throw new Win32Exception(error, "QueryInformationJobObject failed."); } processList = (NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST)Marshal.PtrToStructure(processListPtr, typeof(NativeMethods.JOBOBJECT_BASIC_PROCESS_ID_LIST)); List<Process> pss = new List<Process>(); for (int i = 0; i < processList.NumberOfProcessIdsInList; i++) { try { pss.Add(Process.GetProcessById((int)processList.ProcessIdList[i])); } catch (ArgumentException) { } } this.jobProcesses = pss.ToArray(); } finally { Marshal.FreeHGlobal(processListPtr); } }