判断当前进程是否已经打开C++

int foundProcessByName(const char *filename)
{
	int foundProcessTimes = 0;
	HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
	PROCESSENTRY32 pEntry;
	pEntry.dwSize = sizeof(pEntry);
	BOOL hRes = Process32First(hSnapShot, &pEntry);
	while (hRes)
	{
		_bstr_t b(pEntry.szExeFile);
		const char* c = b;
		if (strcmp(c, filename) == 0)
		{
			std::wstring ws(pEntry.szExeFile);
			std::string test(ws.begin(), ws.end());
			//qDebug() << QString("Found Process %1").arg(QString::fromStdString(test));
			foundProcessTimes++;
		}
		hRes = Process32Next(hSnapShot, &pEntry);
	}
	CloseHandle(hSnapShot);
	return foundProcessTimes;
}

if(foundProcessByName("ThreadName") > 1)
{
  //进程已打开。      
}

  

猜你喜欢

转载自www.cnblogs.com/foreversdf/p/12900519.html
今日推荐