Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to check whether an exe is running or not??
Posted

BOOL CBaseOperation::IsExeRunning(CString strExe)
{
	HANDLE hProcessSnap;
	BOOL bRet = FALSE;
	CString csProcessList = _T("");
	DWORD dwID = 0;
	PROCESSENTRY32 pe32;
	// Take a snapshot of all processes in the system
	hProcessSnap=CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS,0);
	if(hProcessSnap == INVALID_HANDLE_VALUE)
	{
		bRet = FALSE;
		return bRet;
	}
	// Set the size of the structure before using it.
	pe32.dwSize =sizeof(PROCESSENTRY32);
	// Retrieve information about the first process,
	// and exit if unsuccessful
	if(!Process32First (hProcessSnap,&pe32))
	{
		CloseHandle( hProcessSnap );
		bRet = FALSE;
		return bRet;
	}
	// Now walk the snapshot of processes, and
	// display information about each process in turn
	while( Process32Next( hProcessSnap, &pe32 ))
	{
		if(strExe.Compare(pe32.szExeFile)==0)
		{
			return TRUE;
		}
	}
	CloseHandle( hProcessSnap );
	return bRet;
}
 
Share this answer
 
Comments
Gokulnath007 27-Jul-11 7:27am    
Thats great!!! My 5
ShilpiP 27-Jul-11 7:29am    
:)
poth2 28-Jul-11 2:26am    
I think it is necessary to compare strExe with the first process, too. Something like this:

if(!Process32First (hProcessSnap,&pe32))
{
CloseHandle( hProcessSnap );
bRet = FALSE;
return bRet;
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
if(strExe.Compare(pe32.szExeFile)==0)
{
return TRUE;
}
}while( Process32Next( hProcessSnap, &pe32 ));
CloseHandle( hProcessSnap );
bool isRunning = Process.GetProcessesByName("test").FirstOrDefault(p => p.MainModule.FileName.StartsWith(@"c:\loc1")) != default(Process);
 
Share this answer
 
Comments
Gokulnath007 27-Jul-11 7:13am    
What is Process here???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900