/**
* @brief 检测制定的进程是否正在运行
*
* @param processName 进程名称
* @param listProcessId 正在运行的进程的Id
* @return 如果有进程正在运行返回true,否则返回false
*/
bool checkProcessRunning(const QString &processName,
QList<quint64> &listProcessId)
{
#ifdef Q_OS_WIN
bool res = false;
HANDLE hToolHelp32Snapshot;
hToolHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = { sizeof(PROCESSENTRY32) };
BOOL isSuccess = Process32First(hToolHelp32Snapshot, &pe);
while (isSuccess)
{
size_t len = WideCharToMultiByte(CP_ACP, 0, pe.szExeFile, wcslen(pe.szExeFile), NULL, 0, NULL, NULL);
char *des = (char *)malloc(sizeof(char) * (len + 1));
WideCharToMultiByte(CP_ACP, 0, pe.szExeFile, wcslen(pe.szExeFile), des, len, NULL, NULL);
des[len] = '\0';
if (!strcmp(des, processName.toStdString().c_str()))
{
listProcessId.append(pe.th32ProcessID);
res = true;
}
free(des);
isSuccess = Process32Next(hToolHelp32Snapshot, &pe);
}
CloseHandle(hToolHelp32Snapshot);
return res;
#else
bool res(false);
QString strCommand = "ps -ef|grep " + processName + " |grep -v grep |awk '{print $2}'";
const char* strFind_ComName = convertQString2char(strCommand);
FILE * pPipe = popen(strFind_ComName, "r");
if (pPipe)
{
std::string com;
char name[PATH_MAX_LEN];
memset(name, 0, sizeof(PATH_MAX_LEN));
while (fgets(name, sizeof(name), pPipe) != NULL)
{
int nLen = strlen(name);
if (nLen > 0
&& name[nLen - 1] == '\n')
//&& name[0] == '/')
{
name[nLen - 1] = '\0';
listProcessId.append(atoi(name));
res = true;
//break;
}
}
pclose(pPipe);
}
return res;
#endif
}