Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function CreateToolhelp32Snapshot(ByVal dwFlags As SnapshotFlags, ByVal th32ProcessID As UInteger) As IntPtr
End Function
// get the parent process given a pid
public static Process GetParentProcess(int pid)
{
Process parentProc = null;
IntPtr handleToSnapshot = IntPtr.Zero;
try
{
PROCESSENTRY32 procEntry = new PROCESSENTRY32();
procEntry.dwSize = (UInt32)Marshal.SizeOf(typeof(PROCESSENTRY32));
handleToSnapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.Process, 0);
if (Process32First(handleToSnapshot, ref procEntry))
{
do
{
if (pid == procEntry.th32ProcessID)
{
parentProc = Process.GetProcessById((int)procEntry.th32ParentProcessID);
break;
}
} while (Process32Next(handleToSnapshot, ref procEntry));
}
else
{
throw new ApplicationException(string.Format("Failed with win32 error code {0}", Marshal.GetLastWin32Error()));
}
}
catch (Exception ex)
{
throw new ApplicationException("Can't get the process.", ex);
}
finally
{
// Must clean up the snapshot object!
CloseHandle(handleToSnapshot);
}
return parentProc;
}
// get the specific parent process
public static Process CurrentParentProcess
{
get
{
return GetParentProcess(Process.GetCurrentProcess().Id);
}
}
static void Main()
{
Process pr = CurrentParentProcess;
Console.WriteLine("Parent Proc. ID: {0}, Parent Proc. name: {1}", pr.Id, pr.ProcessName);
}
}
An enumeration of the Snapshot flags for the CreateToolhelp32Snapshot function.
6/3/2015 2:17:20 AM - -87.82.247.18
The CreateToolhelp32Snapshot API
5/16/2012 10:18:22 AM - -87.82.247.18
Click to read this page
1/15/2013 2:20:34 AM - -74.212.48.23
The Process32First API
6/24/2009 10:40:02 AM - decebal mihailescu-192.193.221.142
The Process32Next API
6/24/2009 10:44:58 AM - decebal mihailescu-192.193.221.142
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).