Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
hey guys i am trying to count total instance of particular process running. below is my code. problem i am facing over here is i am getting 1 as output instead my vmwp.exe has 4 instances running currently. can any one tell me where i am making mistake.

XML
#include "stdafx.h"

#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>

bool IsProcessRunning(const wchar_t *ProcessName);

int main()
{
    int count = 0;

    if (IsProcessRunning(L"vmwp.exe"))
    {       count++;
    }
        std::cout << "vmwp.exe " << count;
        return 0;
    }

bool IsProcessRunning(const wchar_t *ProcessName)
{
    PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if (Process32First(hSnapshot, &pe32))
    {
        do
        {
            if (_wcsicmp(pe32.szExeFile, ProcessName) == 0)
            {
                CloseHandle(hSnapshot);
                return true;
            }
        } while (Process32Next(hSnapshot, &pe32));
    }

    CloseHandle(hSnapshot);
    return false;
}
Posted

hi,
the problem is, right after you find the first instance of the process, you are closing the handle and coming out of do-while loop. Instead, you have to wait for the whole loop to complete i.e when all the processes of the snapshot have been checked. So, when the comparison succeeds (the line of _wcsicmp) then increment some counter variable and return that variable .

something like this :
    int tempCounter = IsProcessRunning(L"vmwp.exe"))
    
    std::cout << "vmwp.exe " << count;
        return 0;
    }
 //take care to change the return type of this function in your forward declaration also
int IsProcessRunning(const wchar_t *ProcessName)
{
    int temp = 0;
    PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) };
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 
    if (Process32First(hSnapshot, &pe32))
    {
        do
        {
            if (_wcsicmp(pe32.szExeFile, ProcessName) == 0)
            {
                temp++;
               //if process name matches then increment the counter once instead of closing the handle and returning 
               // CloseHandle(hSnapshot);
                //return true;
            }
        } while (Process32Next(hSnapshot, &pe32));
    }
 
    CloseHandle(hSnapshot);
    return temp;
}


hope this helps!!
 
Share this answer
 
It is absolutely clear. Your code can only return 0 or 1. You have your count of 0 and then either increment it once or not.

This is how can you enumerate the processes in your system: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682629%28v=vs.85%29.aspx[^].

The top answer to this question explains how to get the processes main module file path, which gives you the most reliable criteria for "the same process": http://stackoverflow.com/questions/1933113/c-windows-how-to-get-process-path-from-its-pid[^].

Here is where you face a problem. You did not explain the criteria for "instance of particular process". All the processes are, by definition, different processes. If two processes have the same name, this is not a reliable criterion that this is "the same application", because two applications can have the same name. If you know the path name of the application files, you can say that all processes running this application are "the same", but if you, say, copy the application files in some other location and run it, you loose this criterion. If the process was yours, you could use some IPC to communicate between these processes to tell each other "I'm the same thing", but, in principle, even this could be duplicated by some unrelated application. Essentially, the system has no exact criterion of "the same application", and none of two processes are "the same".

—SA
 
Share this answer
 
v2
Comments
Member 11460314 25-May-15 2:28am    
hey i rather understood what you trying to say. but i guess my criteria wasnt clear enough. all i want is the exe name vmwp.exe running in task manager represents total number of hyper v vm machines running. henceforth if there are 2 vmwp.exe in task manager then it says that there are 2 virtual machines are running . now the thing is using c++ code i only want to display the integer value counting how many times vmwp.exe is running in task manager
Sergey Alexandrovich Kryukov 25-May-15 2:39am    
I answered the question for a single machine, not counting virtual machines. Of course, on each virtual machine you have to run the same application to count processes.

Anyway, I fully answered you initial question. Will you accept the answer formally?

—SA
Raje_ 25-May-15 9:27am    
@Member 11460314 : Is this a gentle way to talk someone who is helping you? Be polite it will help you a lot. :)

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