views:

124

answers:

3

how do you find if a exe is running (By name, i.e program.exe).

Edit: Sorry im using it on windows

A: 

Assumptions: since you mention '.exe', you want this for some flavor of Windows. You want to write a program in C++ to determine whether a program with a particular executable name is running (regardless of the language used to implement the target program).

Enumerate the running processes using either the Toolhelp API or the process status API. Compare the name of the executable for each running process to the one you're looking for (and be aware that there may be more than one process with that executable name).

Jerry Coffin
+4  A: 

The C++ standard library has no such support. You need an operating system API to do this. If this is Windows then you'd use CreateToolhelp32Snapshot(), followed by Process32First and Process32Next to iterate the running processes. Beware of the inevitable race condition, the process could have exited by the time you found it.

Hans Passant
Note: CreateToolhelp32Snapshot will not operate as expected in some cases; it often reports the wrong number of svchost.exes. :/ It should be fine for most uses though.
Billy ONeal
:| there has to be a way
blood
@blood: this was a "this is the way to do it on Windows" answer. Your "there has to be a way" response is puzzling and defies any attempt to come up with a better answer.
Hans Passant
o sorry i did not read it right i thought he said it would not work in most cases :D sorry
blood
A: 
hProcessInfo = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );

            do{
                if(strcmp(pe32.szExeFile,"process.exe") == 0)
                {
                    processfound = true;
                    break;
                }
}while( Process32Next( hProcessSnap, &pe32 ) );

If you don't want to get process detail from code just press Ctrl+Alt+Del and check process list.

Arman
the code does not seem to work :O
blood
This is tested code, what is the problem with this code?
Arman