显示使用相同名称运行的进程的总数 [英] display total count of process running with the same name
问题描述
Hello Everyone,
我在C ++中遇到一个小问题,请任何人帮我解决这个问题。我想要的是:如果vcshost.exe在任务管理器中运行并且其多个实例也存在于任务管理器中,因此我希望他们的总计数作为输出。
基本上在我的任务管理器中当前10个vcshost正在运行从此以后它必须显示输出为10.
我的代码是:
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;
}
通过执行此代码得到的输出是:
运行vcshost的总实例是:1(输出错误)因为10个实例正在运行从此它必须显示10
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
推荐答案
你的FindProcessId
函数在找到具有指定名称的第一个进程时返回。要计算实例数,必须编写一个迭代所有句柄的函数,并使用匹配的名称对进程进行计数。
示例(未经测试):
YourFindProcessId
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;
}
嘿但我怎么能在main中调用这个funnction。
i正在尝试这样的事情,但我没有得到输出。
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;
}
我想我错了调用main函数的地方。请任何人纠正我
i guess i am wrong somewhere calling in main function. please can any one correct me
这篇关于显示使用相同名称运行的进程的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!