3

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.

2
  • You seem to be comparing char* not the actual name of the process, you'll need to use strcmp Commented Apr 21, 2015 at 15:44
  • Note: You do not close the handle on pss in the case of a match. I don't know if this is intended. It very much looks like a leak. You should either make sure that every code path performs clean-up manually, or use a scope guard. Commented Apr 21, 2015 at 17:10

1 Answer 1

5

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; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.