Skip to main content
GameDev.net gamedev.net
🔒 Locked

Finding a process

Started by Alkan May 25, 2013 at 3:13 PM 1 replies 1.3k views
Original Post
Alkan
Alkan

Hello

An application i'm wotking on (C++ on Windows) needs to check if a certain process is running or not. To do that, i use EnumProcesses and OpenProcess to find if the process exists. It works fine if the process i'm looking for is a 32 bits process but if it is a 64 bits process, it can't find it. Is there a solution to that ? The application that looks for the other process is a 32 bits app.

Laval B
Laval B
The problem doesn't come from EnumProcesses but from OpenProcess. Try using the snapshot api.
If all you need to do is to determine if the given process is running or not, you could just write a function like the following :

bool IsProcessRunning3264(LPCTSTR pProcessName)
{
    bool bRetVal        = false;
    HANDLE hSnapshot    = NULL;
    PROCESSENTRY32 pe32 = {0};

    //    Take a snapshot of all processes in the system (including system processes).
    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if(hSnapshot != INVALID_HANDLE_VALUE)
    {
        pe32.dwSize = sizeof(PROCESSENTRY32);

        //    Retrieve the informations of the first process of the snapshot. 
        if(Process32First(hSnapshot, &pe32))
        {
            do
            {
                CString str(pe32.szExeFile);

                if(str.CompareNoCase(pProcessName) == 0)
                {
                    bRetVal = true;
                    break;
                }

            }while(Process32Next(hSnapshot, &pe32));
        }
        CloseHandle(hSnapshot);
    }
    return bRetVal;
}

Hope this helps.

We think in generalities, but we live in details.
  - Alfred North Whitehead
Alkan
Alkan


Hope this helps.

Yes it works great.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.