Checking Previous instance of a program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Checking Previous instance of a program

  1. #1
    Usman Guest

    Checking Previous instance of a program


    I would like to know how to see a prevoius instance of a program is running
    if somebody tries to run the same program again.

  2. #2
    Ernesto from Cuba Guest

    Re: Checking Previous instance of a program


    "Usman" <usman@electronia.com> wrote:
    >
    >I would like to know how to see a prevoius instance of a program is running
    >if somebody tries to run the same program again.


    To know a previus instance of a program I use FindWindow,
    example:

    On a dialog application I do the following in the InitInstance to know if
    a instance of the application is running. I give the control to the first
    instance.


    CWnd * pwndFirst = CWnd::FindWindow((LPCSTR) (DWORD) WS_DIALOG, "Application
    name");

    if (pwndFirst){
    // Not the first instance, give control to the first instance
    CWnd * pwndPopup = pwndFirst -> GetLastActivePopup();
    pwndFirst -> SetForegroundWindow();
    if (pwndFirst -> IsIconic())
    pwndFirst -> ShowWindow(SW_SHOWNORMAL);
    if (pwndFirst != pwndPopup)
    pwndPopup -> SetForegroundWindow();
    return false;
    }
    else {
    // First instance
    ........
    }


  3. #3
    Andy Friedman Guest

    Re: Checking Previous instance of a program

    IMHO, using named kernal objects is the way to go because they don't depend
    on having a window like FindWindow does. Named kernal objects can only have
    one instance across the whole system...

    HANDLE h = CreateMutex(NULL, FALSE, "My App Name");
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
    // There is already an instance of this application running.
    return 0;
    }

    Andy

    "Ernesto from Cuba" <ernesto@tldatos.get.turcu> wrote in message
    news:3a37cb3d$1@news.devx.com...
    >
    > "Usman" <usman@electronia.com> wrote:
    > >
    > >I would like to know how to see a prevoius instance of a program is

    running
    > >if somebody tries to run the same program again.

    >
    > To know a previus instance of a program I use FindWindow,
    > example:
    >
    > On a dialog application I do the following in the InitInstance to know if
    > a instance of the application is running. I give the control to the first
    > instance.
    >
    >
    > CWnd * pwndFirst = CWnd::FindWindow((LPCSTR) (DWORD) WS_DIALOG,

    "Application
    > name");
    >
    > if (pwndFirst){
    > // Not the first instance, give control to the first instance
    > CWnd * pwndPopup = pwndFirst -> GetLastActivePopup();
    > pwndFirst -> SetForegroundWindow();
    > if (pwndFirst -> IsIconic())
    > pwndFirst -> ShowWindow(SW_SHOWNORMAL);
    > if (pwndFirst != pwndPopup)
    > pwndPopup -> SetForegroundWindow();
    > return false;
    > }
    > else {
    > // First instance
    > ........
    > }
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center