473,473 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Enumerataing processes in C#

It's easy enough to enumerate the current processes in a system, but how can
I find the parent process for a given process?
Nov 16 '05 #1
3 9160

"Peter Steele" <ps*****@z-force.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
It's easy enough to enumerate the current processes in a system, but how
can I find the parent process for a given process?


With the framework this information is available through the process
performance counters, but it's slow. It's also available from Win32 apis
CreateToolhelp32Snapshot, Process32First, Process32Next, etc.

Here's a VB example:

http://groups.google.com/groups?hl=e...TNGP12.phx.gbl
David
Nov 16 '05 #2
Easy enough when using System.Management...
Here's a sample:

public static void Main() {
Process p = Process.GetCurrentProcess();
int parentPid = GetParentProcess(p.Id);
Console.WriteLine(parentPid);
}
static int GetParentProcess(int Id)
{
int parentPid=0;
using(ManagementObject mo = new ManagementObject("win32_process.handle='"
+ Id.ToString() + "'"))
{
mo.Get();
parentPid = Convert.ToInt32(mo["ParentProcessId"]);
DumpProcessProperties(parentPid);
}
return parentPid;
}
// dump proces properties to the console
static void DumpProcessProperties(int Id)
{
using(ManagementObject mo = new ManagementObject("win32_process.handle='"
+ Id.ToString() + "'"))
{
mo.Get();
foreach(PropertyData pd in mo.Properties)
{
Console.WriteLine("Property: {0}, Value: [{1}]",pd.Name, pd.Value);
}
}
}

Willy.

"Peter Steele" <ps*****@z-force.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
It's easy enough to enumerate the current processes in a system, but how
can I find the parent process for a given process?

Nov 16 '05 #3
Thanks, I thought there'd be a newer approach to the problem.

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
Easy enough when using System.Management...
Here's a sample:

public static void Main() {
Process p = Process.GetCurrentProcess();
int parentPid = GetParentProcess(p.Id);
Console.WriteLine(parentPid);
}
static int GetParentProcess(int Id)
{
int parentPid=0;
using(ManagementObject mo = new ManagementObject("win32_process.handle='"
+ Id.ToString() + "'"))
{
mo.Get();
parentPid = Convert.ToInt32(mo["ParentProcessId"]);
DumpProcessProperties(parentPid);
}
return parentPid;
}
// dump proces properties to the console
static void DumpProcessProperties(int Id)
{
using(ManagementObject mo = new ManagementObject("win32_process.handle='"
+ Id.ToString() + "'"))
{
mo.Get();
foreach(PropertyData pd in mo.Properties)
{
Console.WriteLine("Property: {0}, Value: [{1}]",pd.Name, pd.Value);
}
}
}

Willy.

"Peter Steele" <ps*****@z-force.com> wrote in message
news:un**************@TK2MSFTNGP14.phx.gbl...
It's easy enough to enumerate the current processes in a system, but how
can I find the parent process for a given process?


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Markus Franz | last post by:
Hi. I created a little script: for currenturl in sys.argv: pid = os.fork() if pid == 0: signal.alarm(10) do_something() # placeholder for the download and print routine
6
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
15
by: Dirk Reske | last post by:
Hello, why doesn't this code work correctly? private int GetCpuUsage(Process proc) { DateTime time1,time2; TimeSpan timediff; double cpu1,cpu2,cpudiff;
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
4
by: AN | last post by:
Greetings, We make an ASP.NET web application and we host it for our customers. We have provisioned hardware and hope to be able to service around 200 customers on this hardware. The web...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
3
by: xxs | last post by:
I have writen some codes as follow: #include <windows.h> #include <tlhelp32.h> #include <stdio.h> // Forward declarations: BOOL GetProcessList( ); BOOL ListProcessModules( DWORD dwPID...
1
by: jazon | last post by:
Let me start by saying this for an Operating Systems class. No, I don't expect the work to be done for me. The assignment is as follows: To be honest, I feel like a fish out of water, like...
6
by: Tim Arnold | last post by:
I have a bunch of processes to run and each one needs its own working directory. I'd also like to know when all of the processes are finished. (1) First thought was threads, until I saw that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.