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;
}