C++ Get Username from Process

C++ Get Username From Process

Use OpenProcessToken to get the token (obviously), then GetTokenInformation with the TokenOwner flag to get the SID of the owner. Then you can use LookupAccountSid to get the username.

Get Process Username c++

IIRC there's a separate pseudo account called LocalService, but its not in the normal security system (hence you get a null string). There's also a NetworkService account.

How can I get the active user name in a SYSTEM process using C++?

You need to enumerate through all the running processes using EnumProcesses

Then see this answer to get the username from the process:

https://stackoverflow.com/a/2686150/203244

Detecting user name from process ID

There is not an API function that does this directly, however you can combine a few API calls to do this. Of course your program will need to satisfy any ACLs that are applied to the process that you are interested in examining.

First, given the process ID, you'll need to open a handle to the process. You can use OpenProcess for that, requesting the PROCESS_QUERY_INFORMATION access right.

Once you have that handle, you can call OpenProcessToken, requesting the TOKEN_QUERY access right.

Finally, you can then call GetTokenInformation, requesting the TokenUser information class, which will give you the user account of the token. This information is provided to you in the form of a SID. To convert the SID to the actual name of the account, you can call LookupAccountSid.

Don't forget to call CloseHandle on both the process handle and the token handle once you're finished with them.

How can I get the username of the person executing my program?

Windows

GetUserName()

Example:

 char user_name[UNLEN+1];
DWORD user_name_size = sizeof(user_name);
if (GetUserName(user_name, &user_name_size))
cout << "Your user name is: " << user_name << endl;
else
/* Handle error */

Linux

Look at getpwuid:

The getpwuid() function shall search
the user database for an entry with a
matching uid.

The getpwuid() function shall return a
pointer to a struct passwd

The struct passwd will contain char *pw_name.

Use getuid to get the user id.

How do I determine the owner of a process in C#?

You can use WMI to get the user owning a certain process. To use WMI you need to add a reference to the System.Management.dll to your project.

By process id:

public string GetProcessOwner(int processId)
{
string query = "Select * From Win32_Process Where ProcessID = " + processId;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();

foreach (ManagementObject obj in processList)
{
string[] argList = new string[] { string.Empty, string.Empty };
int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
if (returnVal == 0)
{
// return DOMAIN\user
return argList[1] + "\\" + argList[0];
}
}

return "NO OWNER";
}

By process name (finds the first process only, adjust accordingly):

public string GetProcessOwner(string processName)
{
string query = "Select * from Win32_Process Where Name = \"" + processName + "\"";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();

foreach (ManagementObject obj in processList)
{
string[] argList = new string[] { string.Empty, string.Empty };
int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
if (returnVal == 0)
{
// return DOMAIN\user
string owner = argList[1] + "\\" + argList[0];
return owner;
}
}

return "NO OWNER";
}

How do you get the UserName of the owner of a process?

The CodeProject article How To Get Process Owner ID and Current User SID by Warlib describes how to do this using both WMI and using the Win32 API via PInvoke.

The WMI code is much simpler but is slower to execute. Your question doesn't indicate which would be more appropriate for your scenario.

C++ - GetUserName() when process is run as administrator

I believe the question you want to ask Windows is "which user is logged into the current session".

To do this, call ProcessIdToSessionId() with your own process's ID to determine the current session ID.

Then call WTSQuerySessionInformation() with the WTSUserName option to fetch the user name.



Related Topics



Leave a reply



Submit