Reputation: 67
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.
Upvotes: 3
Views: 243
Reputation: 6039
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;
Upvotes: 5