I do not understand why you do not want p / invoke. If you look at System.Diagnostics in Reflector, you will see that it uses p / invokes inside. In any case, the Process class has no way of getting the parent PID of the process. Instead of this:
Structure Definition:
[StructLayout(LayoutKind.Sequential)] struct PROCESS_BASIC_INFORMATION { public int ExitStatus; public int PebBaseAddress; public int AffinityMask; public int BasePriority; public int UniqueProcessId; public int InheritedFromUniqueProcessId; }
Import function (simplified):
[DllImport("ntdll.dll")] static extern int NtQueryInformationProcess( IntPtr ProcessHandle, int ProcessInformationClass, out PROCESS_BASIC_INFORMATION ProcessInformation, int ProcessInformationLength, out int ReturnLength );
Code:
Process p = Process.GetProcessById(1234); PROCESS_BASIC_INFORMATION pbi; int size; NtQueryInformationProcess(p.Handle, 0, out pbi, Marshal.SizeOf(typeof(PROCESS_BASIC_INFORMATION)), out size);
You will need to insert these headers at the beginning of the file:
using System.Runtime.InteropServices; using System.Diagnostics;
If you want to list processes, you would be better off using NtQuerySystemInformation - although this code is too long to post here.
source share