Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I write a code and using MFC.
I want to check if process exists or not.

for example:
C++
if (myProcess.exe exists)
  do something
else
  do something else


How can I do it?

Thanks
Posted

If the process you're looking for has a GUI then you can use FindWindow to see if the window exists. If it does, the process is running. If not it doesn't necessarily mean it's not - it could be running in a different window station.

If you can modify the source code for the process you want to detect you could create a named semaphore and raise it everytime the process is launched. Then the detecting process can look for the semaphore by name.

If you don't have control over the source code you can use the toolhelp API to dredge through all the processes running using CreateToolHelp32Snapshot, Process32First and Process32First. This[^] article has a code example, bit crap but still usable.
 
Share this answer
 
I did process enumeration and searching for a process in my article here:

Professional System Library: Introduction[^]

You will see that the demo itself lists processes, and allows you to kill one.
 
Share this answer
 
 
Share this answer
 
Comments
user_code 10-Jun-12 4:28am    
Hi, Thanks for your answer.
I did it but got an error: "the procedure entry point k32 enumprocesses could not be located in the dynamic link library KERNEL32.dll". How Can I solve it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900