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

前端 未结 13 1141
清酒与你
清酒与你 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:21

    The solution provided by @user152949, as it was noted in commentaries, skips the first process and doesn't break when "exists" is set to true. Let me provide a fixed version:

    #include 
    #include 
    #include 
    
    bool IsProcessRunning(const TCHAR* const executableName) {
        PROCESSENTRY32 entry;
        entry.dwSize = sizeof(PROCESSENTRY32);
    
        const auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    
        if (!Process32First(snapshot, &entry)) {
            CloseHandle(snapshot);
            return false;
        }
    
        do {
            if (!_tcsicmp(entry.szExeFile, executableName)) {
                CloseHandle(snapshot);
                return true;
            }
        } while (Process32Next(snapshot, &entry));
    
        CloseHandle(snapshot);
        return false;
    }
    

提交回复
热议问题