CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2004
    Posts
    45

    Searching a Process

    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,

  2. #2
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    Re: Searching a Process

    With sufficient thrust, pigs fly just fine. However, this is not
    necessarily a good idea. It is hard to be sure where they are going to
    land, and it could be dangerous sitting under them as they fly
    overhead. -- RFC 1925

  3. #3
    Join Date
    Apr 2006
    Posts
    9

    Re: Searching a Process

    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.

    Code:
    #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;
    }

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Searching a Process

    Quote Originally Posted by Replica
    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.

    Code:
    #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
    Code:
    //get First Process
    Process32First()
    //Get Next Process
    Process32Next ()
    Thanx

  5. #5
    Join Date
    Feb 2004
    Posts
    45

    Re: Searching a Process

    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.

  6. #6
    Join Date
    Apr 2006
    Posts
    9

    Re: Searching a Process

    Quote Originally Posted by humptydumpty
    few more codes you have to use in your program to get all the running process
    have a look for
    Code:
    //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.

  7. #7
    Join Date
    May 2005
    Posts
    4,954

    Re: Searching a Process

    Quote Originally Posted by nfireman
    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:
    Code:
      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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  8. #8
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Searching a Process

    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

  9. #9
    Join Date
    Feb 2004
    Posts
    45

    Re: Searching a Process

    By the way, I'm using Windows2000. It's an organizational decision not mine :-) sorry ...

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Searching a Process

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

  11. #11
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Searching a Process

    BTW, is this babylon.exe one of your apps that you have code for , or is it third party app ?

  12. #12
    Join Date
    Feb 2004
    Posts
    45

    Re: Searching a Process

    Quote Originally Posted by kirants
    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.

    Quote Originally Posted by kirants
    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 :-)

  13. #13
    Join Date
    Mar 2006
    Location
    Bangalore, India
    Posts
    18

    Re: Searching a Process

    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/de...start_page.asp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured