判断当前进程是否已经打开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)
{
  //进程已打开。      
}

  

posted @ 2020-05-23 07:37  李家的小猴子  阅读(596)  评论(0编辑  收藏  举报