diff --git a/main.cpp b/main.cpp index 32938e81..7dbdcbe1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,49 +1,86 @@ #include #include #include -#include #include #include +#include #include "CCThread.h" #include "QProcess" +#include "QSharedMemory" -bool isProcessRunning(const QString &processName) -{ + +bool IsProcessRunning(const std::string& processName) { +#ifdef _WIN32 HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (hSnapshot == INVALID_HANDLE_VALUE) + if (hSnapshot == INVALID_HANDLE_VALUE) { return false; + } PROCESSENTRY32 pe; pe.dwSize = sizeof(PROCESSENTRY32); - bool found = false; - if (Process32First(hSnapshot, &pe)) - { - do - { - if (QString::fromWCharArray(reinterpret_cast(pe.szExeFile)) == processName) - { - found = true; - break; - } - } while (Process32Next(hSnapshot, &pe)); + if (!Process32First(hSnapshot, &pe)) { + CloseHandle(hSnapshot); + return false; } + do { + if (processName == pe.szExeFile) { + CloseHandle(hSnapshot); + return true; + } + } while (Process32Next(hSnapshot, &pe)); + CloseHandle(hSnapshot); - return found; + return false; +#elif __linux__ + DIR* dir = opendir("/proc"); + if (!dir) { + return false; + } + + struct dirent* entry; + while ((entry = readdir(dir)) != nullptr) { + if (entry->d_type == DT_DIR) { + // Check if the directory name is a number + if (entry->d_name[0] >= '0' && entry->d_name[0] <= '9') { + std::string pid = entry->d_name; + std::ifstream commFile("/proc/" + pid + "/comm"); + if (commFile.is_open()) { + std::string comm; + std::getline(commFile, comm); + comm.pop_back(); // Remove newline character + if (comm == processName) { + closedir(dir); + return true; + } + } + } + } + } + + closedir(dir); + return false; +#endif } int main(int argc, char *argv[]) { - static QSharedMemory *shareMem = new QSharedMemory("IPBS_NSSM"); //创建“SingleApp”的共享内存块 - if (!shareMem->create(1))//创建大小1b的内存 - { + if(argc < 3){ + return -2; + } + std::cout << argv[0] << std::endl; + std::cout << argv[1] << std::endl; + std::cout << argv[2] << std::endl; + std::cout << argv[3] << std::endl; + static auto *shareMem = new QSharedMemory(argv[1]); //创建“SingleApp”的共享内存块 + if (!shareMem->create(1)){ //创建大小1b的内存 return -1; } QCoreApplication a(argc, argv); while (true){ - bool F = isProcessRunning("IPBS_Station.exe"); + bool F = IsProcessRunning(argv[2]); if(!F){ - QProcess::startDetached("./IPBS_Station.exe"); + QProcess::startDetached(argv[3]); } Threading::Sleep(1000 * 1000 * 5); }