C++, How to determine if a Windows Process is running?

前端 未结 13 1159
清酒与你
清酒与你 2020-11-27 15:27

This is concerning Windows XP processes.

I have a process running, let\'s call it Process1. Process1 creates a new process, Process2, and saves its id.

Now,

13条回答
  •  日久生厌
    2020-11-27 16:08

    #include 
    #include 
    #include 
    
    /*!
    \brief Check if a process is running
    \param [in] processName Name of process to check if is running
    \returns \c True if the process is running, or \c False if the process is not running
    */
    bool IsProcessRunning(const wchar_t *processName)
    {
        bool exists = false;
        PROCESSENTRY32 entry;
        entry.dwSize = sizeof(PROCESSENTRY32);
    
        HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    
        if (Process32First(snapshot, &entry))
            while (Process32Next(snapshot, &entry))
                if (!wcsicmp(entry.szExeFile, processName))
                    exists = true;
    
        CloseHandle(snapshot);
        return exists;
    }
    

提交回复
热议问题