Checking the execution of a given process

When I use the following function like isRunning ("example.exe"); it always returns 0 regardless of whether the process is running or not.

I tried to make it std :: cout <<pe.szExeFile; in a do-while loop and outputs all processes in the same format as when trying to pass a function.

The project is a multibyte character set, if that matters.

bool isRunning(CHAR process_[])
{
    HANDLE pss = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);

    PROCESSENTRY32 pe = { 0 };
    pe.dwSize = sizeof(pe);

    if (Process32First(pss, &pe))
    {
        do
        {
            if (pe.szExeFile == process_)  // if(!strcmp(pe.szExeFile, process_)) is the correct line here
                return true; // If you use this remember to close the handle here too with CloseHandle(pss);
        } while (Process32Next(pss, &pe));
    }

CloseHandle(pss);

return false;
}

      

I can't seem to find my error. Thank you for your time.

+3


source to share


1 answer


Used if (pe.szExeFile == process_)

, which compares pointer values. You should use something like strcmp

or _stricmp

for comparing actual string values.

eg.



if(strcmp (pe.szExeFile, process_) == 0)
  return true;

      

+5


source







All Articles