Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
I am stucked with a small problem in C++ please can any one help me out in this. what i want is : if in case vcshost.exe is running in task manager and its multiple instances are also present in task manager thus i want their total count as output.

basically in my task manager currently 10 vcshost are running henceforth it must display output as 10.

My code is :
// ConsoleApplication41.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>

using namespace std;

DWORD FindProcessId(const std::wstring& processName);

int main(int argc, char* argv[])
{

	bool BackupProcess = true;
	//DWORD dwHandleCount;
	DWORD dwHandleCount = 0;
	 
	GetProcessHandleCount(GetCurrentProcess(), &dwHandleCount); 


	if (FindProcessId(L"vcshost.exe"))   //.Length.ToString();
	{
		cout << "total instance if vcshost running are :" << GetProcessHandleCount(GetCurrentProcess(), &dwHandleCount);
		
    }

	if (FindProcessId(L"SDRSVC.exe") || FindProcessId(L"sdclt.exe"))
	{
				cout << "\n Windows backup Running";
	}

	
	else if (FindProcessId(L"BackupExecJobEngine.exe") || FindProcessId(L"BackupExecAgentBrowser.exe") || FindProcessId(L"BackupExecDeviceMediaService.exe") || FindProcessId(L"BackupExecManagementService.exe") || FindProcessId(L"BackupExecAgentAccelerator.exe") || FindProcessId(L"BackupExecRPCService.exe"))
	{
		
		cout << "\n Symmantec Backup running";
	}

	else if (FindProcessId(L"ShadowProtectSvc.exe"))
	{
		
		cout << "\n Shadow Protect Running";
	}
	else if (FindProcessId(L"ib_service.exe"))
	{
		cout << "\n iBackup Running";
	}
	else if (FindProcessId(L"bschJW.exe"))
	{
		
		cout << "\n AhsayOBM Backup Running";
	}

	else if (FindProcessId(L"SymTrackServicex64.exe"))
	{
	
		cout << "\n SymTrackServicex64 Backup Running";
	}

	else if (FindProcessId(L"vxmon.exe"))
	{
		
		cout << "\n vxmon Backup Running";
	}

	else if (FindProcessId(L"Vproconsole.exe"))
	{
		cout << "\n Vproconsole Backup Running";
	}

	else if (FindProcessId(L"mozyproconf.exe"))
	{
		cout << "\n mozyproconf Backup Running";
	}
	else if (FindProcessId(L"NtBackup.exe"))
	{
		
		cout << "\n NtBackup Running";
	}
	
	else 
	{

		BackupProcess = false;	
		cout << "\n No backups Running";
	}
	
	return 0;
}


DWORD FindProcessId(const std::wstring& processName)
{
	PROCESSENTRY32 processInfo;
	processInfo.dwSize = sizeof(processInfo);

	HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
	if (processesSnapshot == INVALID_HANDLE_VALUE)
		return 0;

	Process32First(processesSnapshot, &processInfo);
	if (!processName.compare(processInfo.szExeFile))
	{
		CloseHandle(processesSnapshot);
		return processInfo.th32ProcessID;
	}

	while (Process32Next(processesSnapshot, &processInfo))
	{
		if (!processName.compare(processInfo.szExeFile))
		{
			CloseHandle(processesSnapshot);
			return processInfo.th32ProcessID;
		}
	}

	CloseHandle(processesSnapshot);
	return 0;
}



Output that i get by executing this code is:
total instance of vcshost running are : 1 (wrong output) because 10 instance are running henceforth it must display 10
Posted

Your FindProcessId function is returning when the first process with the specified name is found. To count the number of instances you must write a function that iterates over all handles and counts the processes with matching names.

Example (untested):
unsigned CountProcesses(const std::wstring& processName)
{
    unsigned nCount = 0;
    PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);

    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (processesSnapshot == INVALID_HANDLE_VALUE)
        return 0;

    if (Process32First(processesSnapshot, &processInfo))
    {
        do
        {
            if (!processName.compare(processInfo.szExeFile))
                ++nCount;
        }
        while (Process32Next(processesSnapshot, &processInfo));
    }
    CloseHandle(processesSnapshot);
    return nCount;
}
 
Share this answer
 
hey but how can i call this funnction in main.
i am trying something like this but i am not getting output.
if (CountProcesses(L"SDRSVC.exe"))
	{
			cout << "virtual machine : " << CountProcesses;
	}



i guess i am wrong somewhere calling in main function. please can any one correct me
 
Share this answer
 

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