How to get the parent process ID of a process, thanks

L

linuxfedora

Hi All,

How to get the parent process ID of a process, thanks

Beside using performanceCounter

i found that we can use lpfCreateToolhelp32Snapshot, but i cannot
convert it to win32 version correctly, what is the problem?Thanks

[DllImport("Psapi.dll")]
private static extern IntPtr lpfCreateToolhelp32Snapshot(int
value, int value1);

[DllImport("Kernel32.dll")]
private static extern bool lpfProcess32First(IntPtr pointer,
out PROCESSENTRY32 value);

[DllImport("Kernel32.dll")]
private static extern bool lpfProcess32Next(IntPtr pointer,
out PROCESSENTRY32 value);


[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
public string szExeFile;
}

private uint GetProcessParentID(int pid)
{
IntPtr hSnapShot = lpfCreateToolhelp32Snapshot(0x2, 0);

PROCESSENTRY32 procentry = new PROCESSENTRY32();
bool bContinue = lpfProcess32First(hSnapShot, out
procentry);

while (bContinue)
{
if (pid == procentry.th32ProcessID)
{
return procentry.th32ParentProcessID;
}

bContinue = lpfProcess32Next(hSnapShot, out
procentry);
}

return 0;
}
 
P

Peter Duniho

linuxfedora said:
sorry, [DllImport("toolhelp.dll")]
private static extern IntPtr lpfCreateToolhelp32Snapshot(int
value, int value1);

How is that different from what you posted before?
but still not work?

What, specifically, is not working for you? It would be helpful if
you'd post a concise-but-complete code example that reliably
demonstrates whatever problem you're having.

In the meantime, try setting the "dwSize" field of your PROCESSENTRY32
structure before calling lpfProcess32First().

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top