Sometimes, in the Task Manager, you may be interested only in viewing the processes which are created by a specific user instead of all the processes. When you open the Task Manager, it will normally list all the processes and the users who created them. The user names would include your user name, "SYSTEM", "LOCAL SERVICE" etc.
I created a function called
GetProcessUserName which can retrieve the user name information of a process so that you can list all the processes created by a specific user.
I have a project which needs me to gather information about processes, like below:
8:07:05 am, April 06, 2007 : OUTLOOK.exe, <Firefox.exe>, cmd.exe, WINWORD.exe...
8:07:10 am, April 06, 2007 : OUTLOOK.exe, Firefox.exe, <cmd.exe>, WINWORD.exe...
8:07:15 am, April 06, 2007 : OUTLOOK.exe, Firefox.exe, <cmd.exe>, WINWORD.exe...
This list would show all the processes created by a user and the angle brackets would show the process which the user is focusing on.
Use the code
Followed is the main function which can retrieve the user information from a PID (Process ID). I used the Unicode version of all the functions. Therefore, remember to compile the program in UNICODE mode.
LPWSTR GetProcessUserName(DWORD dwPID)
PTOKEN_USER tuUser=new TOKEN_USER;
LPWSTR UserName=new WCHAR;
LPWSTR lpReferencedDomainName=new WCHAR;
The second important procedure is to get the PID of the focused window.
We can use ToolHelp functions to get the process list. Then, we can check if the focused window's PID equals to the members in the list. If you don't know how to get the focused window's process ID, please read the following code:
Points of interest
I first looked in MSDN for an API which could realize this function, but failed. The key point is to get the information from the process. Fortunately, I found that the
TokenInformation of a process can show us a lot about the process. You can search the function in MSDN so that many interesting ideas can be realized!