Link to home
Start Free TrialLog in
Avatar of ifzah
ifzah

asked on

How to check programatically , either a program is running or not.

Like in Task Manager, we can see a program runnnning or not, is there any way that we can check programaticallly , either the program is running or not.
Can we check from registry etc or from task manager.
Please tell me how.
i would be grateful
ifzah
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

The EnumProcesses function retrieves the process identifier for each process object in the system. You could call OpenProcess with PROCESS_QUERY_INFORMATION flag to get a HMODULE handle of each process. The handle could be used for GetModuleFileName that gives the filepath of the executable.

Regards, Alex

ASKER CERTIFIED SOLUTION
Avatar of OnegaZhang
OnegaZhang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grg99
grg99

You can get a list of process names, but if a process doesnt want to be found, there are lots of ways it can hide.
Try this.

HMODULE hModules[10];
TCHAR szFileName[MAX_PATH];

HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, nProcessID);

EnumProcessModules(process, hModules, sizeof(hModules), &nModuleNo);

DWORD nModuleNo /= sizeof(hModules[0]);

if ( GetModuleFileNameEx(process, hModules[0], szFileName, sizeof(szFileName)) )
{
        // EXE name will be got in szFileName
}
>  // EXE name will be got in szFileName

Note that IIRC there's nothing secure in the exe file name, it's not checksummed or MD5'ed or digitally signed or anything, so any program or spyware or virus or worm is free to change the name to anything at any instant.

You might have better luck checking the executable's current data segment for strings it HAS to have in it, like "/bin/sh" or in Windows some likely path or file name it works with.

Or even better, if you're looking for some nasty program, look in its code for calls or loaded images of the core system DLL's.



Avatar of jkr
>> Like in Task Manager

See http://support.microsoft.com/kb/175030/en-us ("How To Enumerate Applications Using Win32 APIs")