473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4285

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Morrison | last post by:
SQL Server 2000 - MSDE 2000 Is there a way to get the number of current users logged into a SQL 2000 Server (also MSDE)? Cant be distinct users as most users are logged into the database using the...
13
by: BK | last post by:
Can someone point me to a code sample that illustrates executing long running tasks (asynchronous) from a web application in ASP.NET? I assume that Web Services might come into play at some point,...
1
by: DD | last post by:
I'm not sure that this msg made it out, the first time I sent it, so I am trying again. -- Win XP Home Edition I use System.Diagnostics.Process.GetProcesses()) to get info about the processes...
15
by: Chakkaradeep | last post by:
Hi all, i have written a Service,now i want to execute another application (for eg;calc.exe) in the service....how will i perform it??... i tried using this.... /**************Executing a...
1
by: Jason | last post by:
I've tried executing processes under Visual Basic.NET-based Windows services in the past but to no avail. Is it possible to execute a process via System.Diagnostics in a Windows Service? The...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
0
by: Jorre Meijrink | last post by:
Dear reader, We have two Win2003 webfarms each consisting of 5 servers. Which we call cluster1 and cluster2. Cluster2 is the bussiest of these two and periodicaly (varies from 2 times per day to...
2
by: Gary | last post by:
Hi i need to work with windows of processes that are running on my pc from a certain programme. I've figured out how to build an array of processes that contain just the processes i'm interested in...
3
by: evenrik | last post by:
An a redhat box I have root, apache and other normal users run code that uses the logging module to write to the same log file. Since umasks are set to 2 or 022 this gets permission errors. I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.