run tasklist in c++

Is there a function in C PLUS Plus to know which programs are running on the other user?
like this:
tasklist /FI "IMAGENAME eq notbad.exe" /FI "USERNAME eq HWLONG"

HOWLONG = other user in my pc .
Last edited on
You say "which programs", but your example seems to ask whether HWLONG does run notbad.exe.

On a different* operating system I could ask:
pgrep -u HWLONG notbad
and answer would be "yes" if any pids are listed, while
ps ax -u HWLONG
lists all processes of that user.


*Different OS is a key point. The C++ is OS agnostic. If you want to do something OS-specific, like get info about processes, then you have to use tha API of the OS to get that info. That is, not call functions from C++ Standard Library, but from the OS.
I don't think there is portable way in C++, so you probably have to use OS-specific functions 😏

On Windows, have a look at this to get a list of running processes:
https://learn.microsoft.com/en-us/windows/win32/toolhelp/taking-a-snapshot-and-viewing-processes

To get the "user" (owner) from the handle of an existing process, you can use:
https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken

...followed by:
https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-gettokeninformation

...with TokenInformationClass set to TokenUser, and TokenInformation pointing to a TOKEN_USER structure:
https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-token_user

In other words, get a list of all running processes, then, for each process, open a handle to the process, open the process' access token (via its handle), and finally query the "user" from the process' access token. This allows you to filter the process list by user.
Last edited on
its probably the 'right' ^^ way to do it, but
system("tasklist /v >somefile.txt"); will dump a snapshot that will be a half second or so out of date if you want a quick fix.. easy to parse out the info from there. There may be a way to get it without an intermediate file, not sure. Or if you can pipe the output of that into your program with a batch file or whatever?

Is there a function in C PLUS Plus to know which programs are running on the other user?


Yes - as per kigar64551's post above. But if you can get the info you want from tasklist, why write a new program rather than just using tasklist (as part of a batch file if the tasklist params need to change)? if the output from tasklist isn't in a needed format, then more simply write a C++ program that takes as input the output from tasklist and produces the required output.

Pipe (| in a command) is designed so that the standard output from the program to the left of | becomes the standard input to the program to its right. See:
https://ss64.com/nt/syntax-redirection.html

Last edited on
You could also use popen from C++ (Windows uses names this function _popen)
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen?view=msvc-170
thanks all so much , solved
Topic archived. No new replies allowed.