Click to See Complete Forum and Search --> : Searching a Process


nfireman
May 24th, 2006, 05:30 AM
Actually what I'm trying to do is to check whether Babylon is running or not. I've tried using FindWindow and it worked but it didn't when babylon was minimized to the system tray. So I thought that I should try and search the process name of babylon which is "Babylon.exe" but I don't know how to do so in C++, any ideas?

Thanks,

dave2k
May 24th, 2006, 05:37 AM
try ::EnumProcesses (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/enumprocesses.asp)

Replica
May 24th, 2006, 08:14 AM
you can use tlhelp functions.
here is a code i wrote while ago that demonstrates how to get list of all process running in the system.


#include <windows.h>
#include <tlhelp32.H>
#include <iostream>

using namespace::std;

int main()
{
HANDLE process;
PROCESSENTRY32 ProcessList;


process = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

if (process == INVALID_HANDLE_VALUE)
{
return 0;
}


while(Process32Next(process, &ProcessList) != FALSE)
{

cout << ProcessList.szExeFile << " " << ProcessList.th32ProcessID << endl;



}
return 0;
}

humptydumpty
May 24th, 2006, 08:22 AM
you can use tlhelp functions.
here is a code i wrote while ago that demonstrates how to get list of all process running in the system.


#include <windows.h>
#include <tlhelp32.H>
#include <iostream>

using namespace::std;

int main()
{
HANDLE process;
PROCESSENTRY32 ProcessList;


process = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

if (process == INVALID_HANDLE_VALUE)
{
return 0;
}


while(Process32Next(process, &ProcessList) != FALSE)
{

cout << ProcessList.szExeFile << " " << ProcessList.th32ProcessID << endl;



}
return 0;
}


few more codes you have to use in your program to get all the running process
have a look for

//get First Process
Process32First()
//Get Next Process
Process32Next ()

Thanx

nfireman
May 24th, 2006, 08:28 AM
Sorry guys, all of these solutions looks very crude. Isn't there something more elegant for enumerating processes in C++.
I've tried the EnumProcesses in Psapi.h but it didn't compile, I needed to add the psapi.lib but it falls in runtime, GetProcessImageFileNameA is not found in PSAPI.dll, I can't continue from here ...

I'll try later the second solution and see if it is more usefull.

Thank's anyway.

Replica
May 24th, 2006, 08:31 AM
few more codes you have to use in your program to get all the running process
have a look for

//get First Process
Process32First()
//Get Next Process
Process32Next ()

Thanx

actually, it dosen't really matter.
in both ways it shows all the running processes, and i've already checked it.

golanshahar
May 24th, 2006, 08:36 AM
Sorry guys, all of these solutions looks very crude. Isn't there something more elegant for enumerating processes in C++.
I've tried the EnumProcesses in Psapi.h but it didn't compile, I needed to add the psapi.lib but it falls in runtime, GetProcessImageFileNameA is not found in PSAPI.dll, I can't continue from here ...

I'll try later the second solution and see if it is more usefull.

Thank's anyway.

::GetProcessImageFileName() is part of the PSDK so either install it, or use dynamic loading:

typedef DWORD (WINAPI *tGetProcessImageFileNameA )(HANDLE,LPTSTR,DWORD );

tGetProcessImageFileNameA pGetProcessImageFileNameA=0;
HINSTANCE handle = ::LoadLibrary("Psapi.dll");
if ( handle == 0 )
return;

if (handle)
pGetProcessImageFileNameA = (tGetProcessImageFileNameA) ::GetProcAddress(handle,"GetProcessImageFileNameA");


if ( pGetProcessImageFileNameA)
{
// call function
//pGetProcessImageFileNameA();
}
::FreeLibrary(handle);


BTW: it is available only on XP.

Cheers

humptydumpty
May 24th, 2006, 08:39 AM
Yes i know .did u checked my post i never said it's wrong
But i just want to say .that if u explain a code to someone it sould be easy to understand

that'all


thanx

nfireman
May 24th, 2006, 11:39 AM
By the way, I'm using Windows2000. It's an organizational decision not mine :-) sorry ...

kirants
May 24th, 2006, 11:50 PM
Interesting, why does FindWindow fail ? Are you sure when you minimize to tray you are not destroying the "babylon" window ?

kirants
May 24th, 2006, 11:52 PM
BTW, is this babylon.exe one of your apps that you have code for , or is it third party app ?

nfireman
May 25th, 2006, 04:09 AM
BTW, is this babylon.exe one of your apps that you have code for , or is it third party app ?

Babylon is a comercial program (www.babylon.com) for translation, my mistake that I assumed it's known.


Interesting, why does FindWindow fail ? Are you sure when you minimize to tray you are not destroying the "babylon" window ?

I don't know whether the window is destroyed or not!

Anyway I've liked much the solution of "Replica" it worked great for me without the need to add any libraries or to load externally dll.

Actually I don't understand why this issue is so problematic and unintuitive. In the .NET its a breeze to do this things :-)

aneesh.joy
May 26th, 2006, 06:45 AM
There is another alternative.. WMI.

You can issue a WMI query like,

SELECT ProcessId FROM Win32_Process WHERE Name = 'babylon.exe'

That will return process id's associated with each instance of babylon.exe running

create a pid_array from the result set

foreach pid in pid_array
{
p = OpenProcess( ...., pid )
kill(p);
}

Note: this is not actual code just to show the method.

For more info about WMI see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_start_page.asp