C++获取指定进程数量 用于判断指定exe程序是否打开
/******************************************************************************************
Function: GetProcessCount
Description: 获取指定进程数量
Calls: lstrcpy,CreateToolhelp32Snapshot,Process32First,CharLowerBuff,lstrcmp,Process32Next
CloseHandle
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)
{
std::cout << e.what() << std::endl;
}
return count;
}