Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear experts
Sorry it seems I have a blackout. I cannot find a way to get the user name under which a process is running and from which I know the PID. I googled for it, but without success.

N.B: PID I get by the help of CreateToolhelp32Snapshot.
And my thinking using OpenProcess and GetStartupInfo seems to be to naiv.

[Edit]
The tags seem to be not valid. I'm working with W32-API.
[/Edit]

Can you give me please an hint/idea?
Thank you very much in advance.
Posted
Updated 27-Jan-16 0:04am
v3
Comments
Jochen Arndt 27-Jan-16 6:29am    
Not a C++ solution but a quick one to check it:
Use the tasklist command (which might be also executed from code parsing the output):

tasklist /v /nh /fi "PID eq <pid>"

/v = verbose to include the user name
/nh = no headers
/fi = filter (here PID match)
[no name] 27-Jan-16 7:49am    
Thank you for your tip, but this you can do much more easy with taskmanager :)
Thank you anyway for your attention.

1 solution

Use this sequence of methods...
1. Get a handler for the process
OpenProcess function (Windows)[^]
2. Open a token - based on the process handler - to gain access to the info
OpenProcessToken function (Windows)[^]
3. Get the relevant info (use TOKENUSER flag)
GetTokenInformation function (Windows)[^]
4. Get user info
LookupAccountSid function (Windows)[^]
 
Share this answer
 
v3
Comments
[no name] 27-Jan-16 6:06am    
Wow, thank you a Million time for this speedy, short and clear answer. I trust in you and accept and 5 it.
Thank you again.
Kornfeld Eliyahu Peter 27-Jan-16 6:07am    
You should check it first...
[no name] 27-Jan-16 7:46am    
Now I asked for a hint/idea and you showed me the right way, even the final solution Looks Little bit different to your way. This works for me:

Given is PID of a propcess
a.) OpenProcess to get a process Handle
b.) OpenProcessToken to get a Token Handle to query process information
c.) GetTokenInformation to query data (by the help of the above Token Handle). In this case Sid.
d.) Finally with LookupAccountSid one gets the user Name
Kornfeld Eliyahu Peter 27-Jan-16 7:51am    
Ops...Now I see that the first and second link accidentally point to the same method...The first of course should be OpenProcess...fixing it in the solution...
[no name] 27-Jan-16 7:58am    
But the words you are using are a little bit misleading. It would be better to distinguish consequently between Process Handle and Tokan Handle.
Thank you anyway for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900