Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Activating Single instance

Activating Single instance

Scheduled Pinned Locked Moved C / C++ / MFC
12 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laiju
    wrote on last edited by
    #1

    Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

    S B T P J 7 Replies Last reply
    0
    • L laiju

      Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

      S Offline
      S Offline
      shiraztk
      wrote on last edited by
      #2

      Hi if your instance is already running you should call showwindow() with maximise option. Hope it works The Best Relligion is Science. Once you understand it, you will know God.

      L 1 Reply Last reply
      0
      • S shiraztk

        Hi if your instance is already running you should call showwindow() with maximise option. Hope it works The Best Relligion is Science. Once you understand it, you will know God.

        L Offline
        L Offline
        laiju
        wrote on last edited by
        #3

        I had tried that option but the application crashes when I do that. Given below is the implementation.. CLimitSingleInstance g_SingleInstanceObj(TEXT("{A79AAE6A-62E1-4c0e-B678-72F127AD4B03}")); BOOL CKolOSDBuilderApp::InitInstance() { // InitCommonControls() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. if (g_SingleInstanceObj.IsAnotherInstanceRunning()) { AfxMessageBox("An instance is already running"); return FALSE; } laiju

        1 Reply Last reply
        0
        • L laiju

          Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          Hello, You could post a message to the application that is already running and maximize the application from there. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          L 1 Reply Last reply
          0
          • L laiju

            Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

            T Offline
            T Offline
            Ted Ferenc
            wrote on last edited by
            #5

            I sometimes use the following method to only run a single instance, I simply use FindWindow to look for the window's title. Yes, I know if two programs have the same title this logic will fail, but that is 'unlikely' if((hWnd = FindWindow(NULL, szTitle)) != NULL)    {    ShowWindow(hWnd, SW_RESTORE);    SetForegroundWindow(hWnd);    MessageBeep(MB_ICONEXCLAMATION);    return(0);    }


            "An education isn't how much you have committed to memory, or even how much you know. It's being able to differentiate between what you do know and what you don't." - Anatole France

            P 1 Reply Last reply
            0
            • B Bob Stanneveld

              Hello, You could post a message to the application that is already running and maximize the application from there. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              L Offline
              L Offline
              laiju
              wrote on last edited by
              #6

              i tried using the variable theApp.m_pMainWnd to post a message ,but i think this doesnt have a valid pointer which crashes the application. BOOL CKolOSDBuilderApp::InitInstance() { // InitCommonControls() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. if (g_SingleInstanceObj.IsAnotherInstanceRunning()) { theApp.m_pMainWnd->SendMessage(WM_CLOSE_PROJECT,0,0); return FALSE; } laiju

              B 1 Reply Last reply
              0
              • L laiju

                i tried using the variable theApp.m_pMainWnd to post a message ,but i think this doesnt have a valid pointer which crashes the application. BOOL CKolOSDBuilderApp::InitInstance() { // InitCommonControls() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. if (g_SingleInstanceObj.IsAnotherInstanceRunning()) { theApp.m_pMainWnd->SendMessage(WM_CLOSE_PROJECT,0,0); return FALSE; } laiju

                B Offline
                B Offline
                Bob Stanneveld
                wrote on last edited by
                #7

                Remember that you post the message to your own application. You'll have to find a way to obtain the process handle of the previous application. You can always write a DLL that is shared among your applications and store various data there... There are a lot of ways to solve your problem! Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                1 Reply Last reply
                0
                • L laiju

                  Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

                  P Offline
                  P Offline
                  PJ Arends
                  wrote on last edited by
                  #8

                  http://www.codeproject.com/cpp/avoidmultinstance.asp[^] laiju wrote: Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I used the technique described in Dr Newcomer's article to do just that in two of my articles, for an MFC example see here[^] and for a Win32 API example see here[^].


                  "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

                  1 Reply Last reply
                  0
                  • T Ted Ferenc

                    I sometimes use the following method to only run a single instance, I simply use FindWindow to look for the window's title. Yes, I know if two programs have the same title this logic will fail, but that is 'unlikely' if((hWnd = FindWindow(NULL, szTitle)) != NULL)    {    ShowWindow(hWnd, SW_RESTORE);    SetForegroundWindow(hWnd);    MessageBeep(MB_ICONEXCLAMATION);    return(0);    }


                    "An education isn't how much you have committed to memory, or even how much you know. It's being able to differentiate between what you do know and what you don't." - Anatole France

                    P Offline
                    P Offline
                    PJ Arends
                    wrote on last edited by
                    #9

                    It will also fail if the app is an MDI or SDI app that has the opened file name in the caption.


                    "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

                    1 Reply Last reply
                    0
                    • L laiju

                      Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #10

                      One way to do it is to create a mutex and an event in the application. The mutex is used to determine whether the application is already running or not. The event is used to signal the process which created it to show itself. All you need to do in the "single instance" process, is to listen to the event and respond to signals sent on it. See CreateEvent(), SetEvent(), and ResetEvent() in the MSDN docs. Good music: In my rosary[^]

                      1 Reply Last reply
                      0
                      • L laiju

                        Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        laiju wrote: Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. See here.


                        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                        1 Reply Last reply
                        0
                        • L laiju

                          Using the class CLimitSingleInstance I have managed to enusure that only one instance of my application is active. I would like to do one more enhancement. Whenever there is an attempt to run multiple instance the current running instance should be restored to full view. I think it has to be done in the InitInstance() of my appln. Can anyone suggest the procedure. laiju

                          P Offline
                          P Offline
                          PraxUnited
                          wrote on last edited by
                          #12

                          hi, Obviously u know the caption of your application..[I mean caption on the tittle bar of parent window] use FindWindow API to search with that window caption...if FindWindow API succeeds u will get the handle for that.. then u send the window resize message with that handle otherwise go as normal... try this in InitInstance... With Regards Prabhu.S www.PraxUnited.com

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups