QT MingW 编译的程序判断MFC程序是否已运行
下面展示一些 内联代码片
。
//szProcessName 为可执行文件路径 如C:\Test.exe
static bool IsExistProcess(QString szProcessName)
{
PROCESSENTRY32 processEntry32;
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
bool bRet = false;
if (((int)handle) != -1)
{
processEntry32.dwSize = sizeof(processEntry32);
int bResult = Process32First(handle, &processEntry32);
if(!bResult)
{
return bRet;
}
while(bResult)
{
// 获取全路径
WCHAR chPath[MAX_PATH];
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processEntry32.th32ProcessID);
GetModuleFileNameEx(hProcess,NULL, chPath, MAX_PATH);
QString strFileName = QString::fromStdWString(chPath);
strFileName = QDir::toNativeSeparators(strFileName);
szProcessName = QDir::toNativeSeparators(szProcessName);
if(szProcessName == strFileName)
{
bRet = true;
break;
}
bResult = Process32Next(handle, &processEntry32);
}
}
CloseHandle(handle);
return bRet;
}