C++ 获取指定进程数量 用于判断指定exe程序是否打开

/******************************************************************************************
Function:        GetProcessCount
Description:     获取指定进程数量
Input:           szExeName:进程名称,带.exe后缀
Return:          进程数量
*******************************************************************************************/
int dxir::Base::GetProcessCount(const TCHAR* szExeName)
{
	int count = 0;
	try
	{
		TCHAR sztarget[MAX_PATH];
		lstrcpy(sztarget, szExeName);
		CharLowerBuff(sztarget, MAX_PATH);
		PROCESSENTRY32 my;
		HANDLE l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		if (((int)l) != -1)
		{
			my.dwSize = sizeof(my);
			if (Process32First(l, &my))
			{
				do
				{
					CharLowerBuff(my.szExeFile, MAX_PATH);
					if (lstrcmp(sztarget, my.szExeFile) == 0)
					{
						count++;
					}
				} while (Process32Next(l, &my));
			}
			CloseHandle(l);
		}
	}
	catch (std::exception e)
	{
	}

	return count;
}

你可能感兴趣的:(C++)