zoukankan      html  css  js  c++  java
  • QT_获取正在运行程序的进程id(判断程序是否正在运行)

    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] = '';
            if (!strcmp(des, processName.toStdString().c_str()))
            {
                listProcessId.append(pe.th32ProcessID);
                res = true;
                break;
            }
            free(des);
            isSuccess = Process32Next(hToolHelp32Snapshot, &pe);
        }
        CloseHandle(hToolHelp32Snapshot);
        return res;
    #elif defined Q_OS_MAC
        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[512] = { 0 };
            while (fgets(name, sizeof(name), pPipe) != NULL)
            {
                int nLen = strlen(name);
                if (nLen > 0
                    && name[nLen - 1] == '
    ')
                    //&& name[0] == '/')
                {
                    name[nLen - 1] = '';
                    listProcessId.append(atoi(name));
                    res = true;
                    break;
                }
            }
            pclose(pPipe);
        }
        return res;
    #endif
    }
  • 相关阅读:
    安卓渗透测试环境搭建笔记
    spring boot Thymeleaf 模板注入 测试实践
    分析activity安全检测实践
    xposed的使用实践
    android组件安全测试实践
    Apache Dubbo Provider默认反序列漏洞复现实践(CVE-2020-1948)
    java设计模式--策略模式
    spring 发送email
    简单介绍
    有意义的礼物——英语小短文
  • 原文地址:https://www.cnblogs.com/amwuau/p/9776820.html
Copyright © 2011-2022 走看看