How to determine whether specific processes are running or not

Let’s see how we can check if specific processes are already running in the system and then decide what we would like to do (exit for example).

Useful links
CreateToolhelp32Snapshot

Process32First

Process32Next

//..
static char* const ProcessesNotAllowed[] = { "blahblah1", "blahblah2" };
//..
void CheckIfProcsRunning()
{
	PROCESSENTRY32 pe;
	HANDLE h;
	int i = 0;
	int l = 0;
 
	h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (h == INVALID_HANDLE_VALUE) return;
 
	if (!Process32First(h, &pe))
	{
		CloseHandle( h );
		return;
	}
 
	l = sizeof(ProcessesNotAllowed) / sizeof(*ProcessesNotAllowed);
 
	while(Process32Next(h, &pe))
		for(i=0;i<l;i++)
			if(strcmp (ProcessesNotAllowed[i], pe.szExeFile)==0) ExitProcess(0);
 
	CloseHandle(h);
}
Categories: C Tags: , ,
%d bloggers like this: