Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if given process is running

Tags:

c++

winapi

When I use the following function as isRunning("example.exe"); it always returns 0 no matter if the process is running or not.

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

The project is multi-byte character set, in case that makes a difference.

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;
}

Can't seem to find my mistake. Thanks for your time.

like image 546
user-1289389812839 Avatar asked Jun 01 '26 11:06

user-1289389812839


1 Answers

You are using if (pe.szExeFile == process_) which compares the pointer values. You should be using something like strcmp or _stricmp to compare the actual string values instead.

e.g.

if(strcmp (pe.szExeFile, process_) == 0)
  return true;
like image 111
edtheprogrammerguy Avatar answered Jun 04 '26 13:06

edtheprogrammerguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!