Link to home
Start Free TrialLog in
Avatar of pigangel
pigangel

asked on

detect other program is running

Hi,Experts:  
  my program has some functions only can process when one other independent program is running.
  I have a question about this, it is possible to detect if other program is already running in the system.
  please help me, simple sample code will be very appreciated. I am using VC++ 6 and win2k.
  thank you very much!


--pigangel
 
ASKER CERTIFIED SOLUTION
Avatar of ghimireniraj
ghimireniraj

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 arunsrk
arunsrk

First of all
Create your own class name for your window
if you are using MFC use the Follwing Code in
InitInstance() Function of your application if you are following classic sdk style then WNDLCASS is already there..

// Register your unique class name that you wish to use
   
    WNDCLASS wndcls;
    memset(&wndcls, 0, sizeof(WNDCLASS));
    wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndcls.lpfnWndProc = ::DefWindowProc;
    wndcls.hInstance = AfxGetInstanceHandle();
    wndcls.hIcon = LoadIcon(IDR_MAINFRAME);
    wndcls.hCursor = LoadCursor( IDC_ARROW );
    wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wndcls.lpszMenuName = NULL;

//Specify your own class name for using FindWindow later
   
    wndcls.lpszClassName = _T("PIGANGEL'S_CLASS");
    if(!AfxRegisterClass(&wndcls))
    {
         AfxMessageBox(_T("Class Registration Failed"));
         return FALSE;
    }

In the Second Application where you want to detect whether the window is running use the following code ..
here you get the handle of the application if your window is running.. You get the handle if the window is hidden too..
    HWND hWnd = ::FindWindow(_T("PIGANGEL'S_CLASS"),NULL);
    if(hWnd != NULL)
    {
     // Window Present You can do anything you want here
    }
    else
    {
       // Application not running
    }
Avatar of pigangel

ASKER

Hi,Niraj, your solution works perfect!

Arunsrk, the independent program is not handled by me, so your suggestion is not appliable to my qunestion, but I believe it will work on other scenario.

thank you very much!

-- pigangel