Process User

  • Thread starter Thread starter Noah Coad [MVP .NET/C#]
  • Start date Start date
N

Noah Coad [MVP .NET/C#]

Howdy! I need to know what user is using which process on the system.

"System.Diagnostics.Process.GetProcesseses" will return an array of
processes, but I need to know if the process returned is owned by the
current user or other users on the system (Windows 2003 Server). How is
this accomplished?

Thanks!

- Noah Coad -
Microsoft MVP
 
Noah Coad said:
Howdy! I need to know what user is using which process on the system.

"System.Diagnostics.Process.GetProcesseses" will return an array of
processes, but I need to know if the process returned is owned by the
current user or other users on the system (Windows 2003 Server). How is
this accomplished?

Thanks!

- Noah Coad -
Microsoft MVP

Or you can use the System.Management classes (and WMI), or you can call the
TS API's (WTSEnumerateProcesses and friends) using PInvoke.

Here's how to use the former.

using System.Management;

class Sample_SelectQuery
{
public static void Main() {
SelectQuery selectQuery = new SelectQuery("Win32_Process");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(selectQuery);

foreach (ManagementObject proc in searcher.Get()) {
Console.WriteLine(proc["Name"].ToString());
//out argument to return user and domain
string[] s = new String[2];
//Invoke the method and populate the array with the user name
and domain
proc.InvokeMethod("GetOwner",(object[])s);
Console.WriteLine("User: " + s[1]+ "\\" + s[0]);
}
}

Willy.
 
Wow! Now that is a solution from someone who really knows what they're
doing! :)

Thank you!!

- Noah Coad -

P.S. Will you be at the Global Summitt? I'd like to thank you in person.


Willy Denoyette said:
Noah Coad said:
Howdy! I need to know what user is using which process on the system.

"System.Diagnostics.Process.GetProcesseses" will return an array of
processes, but I need to know if the process returned is owned by the
current user or other users on the system (Windows 2003 Server). How is
this accomplished?

Thanks!

- Noah Coad -
Microsoft MVP

Or you can use the System.Management classes (and WMI), or you can call the
TS API's (WTSEnumerateProcesses and friends) using PInvoke.

Here's how to use the former.

using System.Management;

class Sample_SelectQuery
{
public static void Main() {
SelectQuery selectQuery = new SelectQuery("Win32_Process");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(selectQuery);

foreach (ManagementObject proc in searcher.Get()) {
Console.WriteLine(proc["Name"].ToString());
//out argument to return user and domain
string[] s = new String[2];
//Invoke the method and populate the array with the user name
and domain
proc.InvokeMethod("GetOwner",(object[])s);
Console.WriteLine("User: " + s[1]+ "\\" + s[0]);
}
}

Willy.
 
Noah Coad said:
Wow! Now that is a solution from someone who really knows what they're
doing! :)

Thank you!!

- Noah Coad -

P.S. Will you be at the Global Summitt? I'd like to thank you in person.

Sorry, not this time, thanks anyway.

Willy.
 
Back
Top