Originally Posted by
outlawbt
Allright, in context of this theme, I need a code that will check if there are two or more running processes with the same name ( for example, notepad.exe ) ... If there are two processes with same name, return true, otherwise, return false. ( I specify the process name ) .
Thanks in advance.
Code:
#pragma comment(lib, "advapi32.lib")
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
DWORD CountProcesses(CHAR *pProcessName)
{
DWORD dwCount = 0;
HANDLE hSnap = NULL;
PROCESSENTRY32 proc32;
if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
return -1;
proc32.dwSize=sizeof(PROCESSENTRY32);
while((Process32Next(hSnap, &proc32)) == TRUE)
if(stricmp(proc32.szExeFile,pProcessName) == 0)
++dwCount;
CloseHandle(hSnap);
return dwCount;
}
int main(int argc, char **argv)
{
DWORD dwReturn;
if(argc == 2)
{
dwReturn = CountProcesses(argv[1]);
if(dwReturn != -1)
printf("There are %d %s processes running\n",dwReturn, argv[1] );
else
printf("CreateToolhelp32Snapshot failed\n");
}
return 0;
}