Solved

detect other program  is running

Posted on 2002-06-18
3
235 Views
Last Modified: 2013-11-20
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
 
0
Comment
Question by:pigangel
3 Comments
 
LVL 3

Accepted Solution

by:
ghimireniraj earned 80 total points
ID: 7091689
Hi pigangel,

Check out the topic "PROCESS WALKING" in MSDN.

Roughly this might be a way.

Using

Process32First(...)
Process32Next(...)

Iterate through the all the currently running processes.  Each time you will be retrieving process info into a structure  into PROCESSENTRY32 .

Check PROCESSENTRY32  to see if your desired process is running. look into that structure and find out how you can identify your the one you are looking for...One simple info  may be the szExeFile Path and filename of the executable file for the process.


--Niraj






0
 

Expert Comment

by:arunsrk
ID: 7091810
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
    }
0
 

Author Comment

by:pigangel
ID: 7091888
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
has22 challenge 11 106
NotAlone Challenge 20 87
Not needed 13 128
Increment column based of a FK 8 46
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question