Count specific process name

Hi
I want to count specific title in process . for example when I run twice calc.exe
in my system and then run my c++ program I show calc.exe (or specific file name)
is run twice
Thanks
I don't know if there is any function in the Windows API that would do that for you, but you could issue a system command like

tasklist /NH /FI "IMAGENAME eq firefox.exe"

(replacing firefox.exe by whatever program name you want).

On my machine this gives (the rather excessive):
firefox.exe                   3868 Console                    1    265,936 K
firefox.exe                   3844 Console                    1     47,352 K
firefox.exe                   4312 Console                    1    106,632 K
firefox.exe                   4604 Console                    1    156,852 K
firefox.exe                   4756 Console                    1     71,136 K
firefox.exe                   2240 Console                    1     52,112 K



EDIT
Scrub that, you can just do
tasklist /FI "IMAGENAME eq firefox.exe" | find /C "firefox.exe"
Last edited on
thank you . There's sure to be a better way . count with c++ code and print to console window .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
   string toFind = "firefox.exe";
   int N;

   string cmd = "tasklist /FI \"IMAGENAME eq " + toFind + "\" | find /C \"" + toFind + "\" > tmp.txt";
   system( cmd.c_str() );
   ifstream in( "tmp.txt" );
   in >> N;

   cout << "There are " << N << " versions of " << toFind << " currently running\n";
}


There are 6 versions of firefox.exe currently running
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
   string toFind = "firefox.exe";
   int N;

   string cmd = "tasklist /FI \"IMAGENAME eq " + toFind + "\" | find /C \"" + toFind + "\" > tmp.txt";
   system( cmd.c_str() );
   ifstream in( "tmp.txt" );
   in >> N;

   cout << "There are " << N << " versions of " << toFind << " currently running\n";
}

That is good answer but this code create a text file ?
is it c++ code dont creat txt file ?
use c++ code insted of directly cmd code !
Last edited on
If I assume you are writing a Windows application, then what you're looking for is the Windows API function called EnumProcesses

https://docs.microsoft.com/en-us/windows/desktop/api/Psapi/nf-psapi-enumprocesses

For Linux and UNIX, the method is quite different. Linux creates an entry in the directory /proc for every process currently running, from which you can inquire about the details with various API, but the basic usage pattern is like listing a directory.
Last edited on
While the following link is about enumerating windows, the way to enumerate processes uses a near-identical structure. You can use it as a basis for code to get all the processes (and process names) currently active, if you wish.
http://www.cplusplus.com/forum/general/170775/#msg851518
Topic archived. No new replies allowed.