471,317 Members | 2,596 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

How to find users executing processes

Hi,

I am looking for a way to determine het user that is running a certain
process, I can retrieve the process ID by calling the
GetProcessByName("test") method which will return all processes named
'test'.
But how do I retrieve the user that is running the process ( the process
'test' is started using the 'run as ' function provided in the user
interface of windows)?

Any help would be appreciated,
Han


Jan 13 '06 #1
2 4192

"Han de Monnink" <ha************@hp.com> wrote in message
news:dq**********@news4.zwoll1.ov.home.nl...
| Hi,
|
| I am looking for a way to determine het user that is running a certain
| process, I can retrieve the process ID by calling the
| GetProcessByName("test") method which will return all processes named
| 'test'.
| But how do I retrieve the user that is running the process ( the process
| 'test' is started using the 'run as ' function provided in the user
| interface of windows)?
|
| Any help would be appreciated,
|
|
| Han
|
|
|
|

You can use System.Management and WMI for this. Just query the process list
using one of the process properties as serch criteria and call WMI's
GetOwner method on the instances returned.
Folling illustrates the process using a process name as query property.

GetProcessOwner("some.exe");

....
private static void GetProcessOwner(string procName)
{
SelectQuery selectQuery = new SelectQuery("select * from Win32_Process
where name='" + procName + "'");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(selectQuery))
{
ManagementObjectCollection processes = searcher.Get();
foreach(ManagementObject process in processes)
{
//out argument to return user and domain
string[] s = new String[2];
//Invoke the GetOwner method and populate the array with the user
name and domain
process.InvokeMethod("GetOwner",(object[])s);
Console.WriteLine("User: " + s[1]+ "\\" + s[0]);
}
}
}
}

Willy.
Jan 13 '06 #2
Thanks,

I tried it and it seems to work Great ...
Han
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

"Han de Monnink" <ha************@hp.com> wrote in message
news:dq**********@news4.zwoll1.ov.home.nl...
| Hi,
|
| I am looking for a way to determine het user that is running a certain
| process, I can retrieve the process ID by calling the
| GetProcessByName("test") method which will return all processes named
| 'test'.
| But how do I retrieve the user that is running the process ( the
process
| 'test' is started using the 'run as ' function provided in the user
| interface of windows)?
|
| Any help would be appreciated,
|
|
| Han
|
|
|
|

You can use System.Management and WMI for this. Just query the process
list
using one of the process properties as serch criteria and call WMI's
GetOwner method on the instances returned.
Folling illustrates the process using a process name as query property.

GetProcessOwner("some.exe");

...
private static void GetProcessOwner(string procName)
{
SelectQuery selectQuery = new SelectQuery("select * from
Win32_Process
where name='" + procName + "'");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(selectQuery))
{
ManagementObjectCollection processes = searcher.Get();
foreach(ManagementObject process in processes)
{
//out argument to return user and domain
string[] s = new String[2];
//Invoke the GetOwner method and populate the array with the user
name and domain
process.InvokeMethod("GetOwner",(object[])s);
Console.WriteLine("User: " + s[1]+ "\\" + s[0]);
}
}
}
}

Willy.

Jan 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Chakkaradeep | last post: by
8 posts views Thread by lovecreatesbea... | last post: by
reply views Thread by Jorre Meijrink | last post: by

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.