UserName of a process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In asp.net(C#) we can get the Process Name of a process using the following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
 
Prisy,

In order to do that, you would have to get a handle to the process you
want to get the username for first. Assuming you have that, you will have
to do a bit of P/Invoke calls to get it.

First, you would call OpenProcessToken. Once you have the token for the
process, you would call GetTokenInformation passing the value TokenUser for
the TokenInformationClass parameter. This will return a TOKEN_USER
structure which contains the SID for the user that the process was launched
under. The only field that the TOKEN_USER structure has is of type
SID_AND_ATTRIBUTES, which has two fields, Sid and Attributes. The Sid field
is the pointer to the SID. With that, you can call the LookupAccountSid
function, passing the pointer to the SID and getting the domain and user
that the process was started under.

You should be able to find most of the definitions for these functions
at http://www.pinvoke.net. If they are not there, post back, and we can
help you with the definitions.

Hope this helps.
 
How can i check to see if the UserName of the process is
System.Environment.UserName?

--
Thanks
Prisy


Frisky said:
I think you are looking for System.Environment.UserName.

Frisky
 
Prisy said:
In asp.net(C#) we can get the Process Name of a process using the
following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?

Yep, using System.Management classes, here is how....

using System.Management;
using System.Diagnostics;
....
Process p = Process.GetCurrentProcess();
GetProcessIdentity(p.Id);
}

static void GetProcessIdentity(int Id)
{
using(ManagementObject process = new
ManagementObject("win32_process.handle=" + Id))
{
foreach (ManagementObject logonSession in
process.GetRelated("win32_logonSession"))
{
foreach(ManagementBaseObject account in
logonSession.GetRelated("win32_UserAccount"))
{
PropertyDataCollection processProperties = account.Properties;
Console.WriteLine("Name: {0} ,Domain: {1} ,Fullname: {2}, ,SID:
{3}" ,
processProperties["Name"].Value,
processProperties["Domain"].Value,
processProperties["FullName"].Value,
processProperties["SID"].Value);
}
}
}
}

Willy.
 
Nicholas,

I am trying to follow the steps you have mentioned. What do i have to import
in order to be able to use the methods like OpenProcessToken,
GetTokenInformation, etc...? I will be searching online as i wait for your
reply.

--
Thanks
Prisy


Nicholas Paldino said:
Prisy,

In order to do that, you would have to get a handle to the process you
want to get the username for first. Assuming you have that, you will have
to do a bit of P/Invoke calls to get it.

First, you would call OpenProcessToken. Once you have the token for the
process, you would call GetTokenInformation passing the value TokenUser for
the TokenInformationClass parameter. This will return a TOKEN_USER
structure which contains the SID for the user that the process was launched
under. The only field that the TOKEN_USER structure has is of type
SID_AND_ATTRIBUTES, which has two fields, Sid and Attributes. The Sid field
is the pointer to the SID. With that, you can call the LookupAccountSid
function, passing the pointer to the SID and getting the domain and user
that the process was started under.

You should be able to find most of the definitions for these functions
at http://www.pinvoke.net. If they are not there, post back, and we can
help you with the definitions.

Hope this helps.
 
Back
Top