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. Only One Instance to be opened...

Only One Instance to be opened...

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
11 Posts 4 Posters 1 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.
  • M Manfred Ramosch

    Does anybody know how I can force my MFC-Dialogbased-Application to be opened only once. No second instance should be possible. I tried it with a flag BOOL MyInstance = FALSE; in the InitApplication() - and in the InitInstance() I wrote if(!MyInstance) { MyInstance = TRUE; CZones dlg; m_pMainWnd = &dlg; dlg.DoModal(); } I thought that InitApplication() is called only once and every further Instance is just calling InitInstance(). But it looks like every Instance is also calling InitApplication()... Any Ideas ? Manfred

    L Offline
    L Offline
    l a u r e n
    wrote on last edited by
    #2

    manfred ... the win32 apps all run in their own process space so they dont get to see the others and the concept of a global flag across all instances doesn't hold what you should do in the initinstance (or what i would do) is use the enumerate thru all the active windows and see if another instance of your app is in the list ... if it is just terminate gracefully from the second instance :suss: --- "every year we invent better idiot proof systems and every year they invent better idiots"

    M 1 Reply Last reply
    0
    • L l a u r e n

      manfred ... the win32 apps all run in their own process space so they dont get to see the others and the concept of a global flag across all instances doesn't hold what you should do in the initinstance (or what i would do) is use the enumerate thru all the active windows and see if another instance of your app is in the list ... if it is just terminate gracefully from the second instance :suss: --- "every year we invent better idiot proof systems and every year they invent better idiots"

      M Offline
      M Offline
      Manfred Ramosch
      wrote on last edited by
      #3

      And how can I enumerate through all active windows? Any function() available?:confused: Manfred

      L 1 Reply Last reply
      0
      • M Manfred Ramosch

        And how can I enumerate through all active windows? Any function() available?:confused: Manfred

        L Offline
        L Offline
        l a u r e n
        wrote on last edited by
        #4

        EnumWindows() will go thruogh any top level windows in the system and call a user-supplied callback function passing the window handle of each active window ... from that you can get the window title and check against your app name :) --- "every year we invent better idiot proof systems and every year they invent better idiots"

        R J 2 Replies Last reply
        0
        • L l a u r e n

          EnumWindows() will go thruogh any top level windows in the system and call a user-supplied callback function passing the window handle of each active window ... from that you can get the window title and check against your app name :) --- "every year we invent better idiot proof systems and every year they invent better idiots"

          R Offline
          R Offline
          RockNix
          wrote on last edited by
          #5

          Hi, here is a another way to check out if your application is already running. I used this in my API-code but it should also run in MFC. First create a mutex-object on startup. -> CreateMutex(NULL,true,"SomeName"); Than ask about last-error -> if( GetLastError()==ERROR_ALREADY_EXISTS) { // do exit stuff } Hope it helps Mario /// ---------------------- www.klangwerker.de mario@klangwerker.de ----------------------

          L 1 Reply Last reply
          0
          • R RockNix

            Hi, here is a another way to check out if your application is already running. I used this in my API-code but it should also run in MFC. First create a mutex-object on startup. -> CreateMutex(NULL,true,"SomeName"); Than ask about last-error -> if( GetLastError()==ERROR_ALREADY_EXISTS) { // do exit stuff } Hope it helps Mario /// ---------------------- www.klangwerker.de mario@klangwerker.de ----------------------

            L Offline
            L Offline
            l a u r e n
            wrote on last edited by
            #6

            i always used to get worried in case my app terminated abnormally and left a dangling mutex (not a pretty sight let me tell you) so i opted for a more 'passive' approach prolly just me being paranoid :cool: --- "every year we invent better idiot proof systems and every year they invent better idiots"

            R 1 Reply Last reply
            0
            • L l a u r e n

              i always used to get worried in case my app terminated abnormally and left a dangling mutex (not a pretty sight let me tell you) so i opted for a more 'passive' approach prolly just me being paranoid :cool: --- "every year we invent better idiot proof systems and every year they invent better idiots"

              R Offline
              R Offline
              RockNix
              wrote on last edited by
              #7

              Hi, first of all the function CreateMutex() returns a handle to an object when creation succeeds. On the other side the mutex-object will be created by the process which started first. Calling CreateMutex with same "Name" again won`t succeeds because it already exists so the worst case is to have 1 object at the same time and no more. Mario /// ---------------------- www.klangwerker.de mario@klangwerker.de ----------------------

              J 1 Reply Last reply
              0
              • L l a u r e n

                EnumWindows() will go thruogh any top level windows in the system and call a user-supplied callback function passing the window handle of each active window ... from that you can get the window title and check against your app name :) --- "every year we invent better idiot proof systems and every year they invent better idiots"

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #8

                > from that you can get the window title and check against your app name [...]    Hmmm...  You might want to be looking for a specific window class, not just a title.  Titles can be duplicated, and since a window title is visible to the user, they tend to not be very complex.    A Window Class, however, can be a very complex name, and is not generally shown to the user.  Since the Window Class can be more complex, there is less chance of it being duplicated.  For example, you can use a GUID (as long as you have a NIC in your machine) as (part of) the Window Class. -=- James.

                1 Reply Last reply
                0
                • R RockNix

                  Hi, first of all the function CreateMutex() returns a handle to an object when creation succeeds. On the other side the mutex-object will be created by the process which started first. Calling CreateMutex with same "Name" again won`t succeeds because it already exists so the worst case is to have 1 object at the same time and no more. Mario /// ---------------------- www.klangwerker.de mario@klangwerker.de ----------------------

                  J Offline
                  J Offline
                  James R Twine
                  wrote on last edited by
                  #9

                  > so the worst case is to have 1 object at the same time and no more.    I think Lauren is trying to say that if your app AbEnds, and leaves the Mutex active, other instances of your application will not be able to launch, as they will see that the Mutex is active, and will not come up.    A fix is to combine the Mutex with the "Window Search": Check for the Mutex, and then search for your app's window by it's *Window Class*.  Then:       o If you find the window, use ::SendMessageTimeout(...) to see if your app is not hung.  If it responds, use that window handle to bring the app to the foreground (always a good idea, so the user gets some feedback).       o If you find the window, but your app is hung, you can do whatever you want (kill it and relaunch, wait for it, etc.).       o If you do not find the window, your app likely AbEnded, and left the Mutex lying around.  You can then launch another instance.    Peace! -=- James.

                  R 1 Reply Last reply
                  0
                  • J James R Twine

                    > so the worst case is to have 1 object at the same time and no more.    I think Lauren is trying to say that if your app AbEnds, and leaves the Mutex active, other instances of your application will not be able to launch, as they will see that the Mutex is active, and will not come up.    A fix is to combine the Mutex with the "Window Search": Check for the Mutex, and then search for your app's window by it's *Window Class*.  Then:       o If you find the window, use ::SendMessageTimeout(...) to see if your app is not hung.  If it responds, use that window handle to bring the app to the foreground (always a good idea, so the user gets some feedback).       o If you find the window, but your app is hung, you can do whatever you want (kill it and relaunch, wait for it, etc.).       o If you do not find the window, your app likely AbEnded, and left the Mutex lying around.  You can then launch another instance.    Peace! -=- James.

                    R Offline
                    R Offline
                    RockNix
                    wrote on last edited by
                    #10

                    >>I think Lauren is trying to say that if your app AbEnds, and leaves the >>Mutex active, other instances of your application will not be able to >>launch, as they will see that the Mutex is active, and will not come up. Yes in fact you`re right. That´s a point I haven`t thought about. The question is what will happen with a mutex-object which owner is no longer running ? Is it still reserved or is it free to get ? That`s worth a try I think. Mario /// ---------------------- www.klangwerker.de mario@klangwerker.de ----------------------

                    J 1 Reply Last reply
                    0
                    • R RockNix

                      >>I think Lauren is trying to say that if your app AbEnds, and leaves the >>Mutex active, other instances of your application will not be able to >>launch, as they will see that the Mutex is active, and will not come up. Yes in fact you`re right. That´s a point I haven`t thought about. The question is what will happen with a mutex-object which owner is no longer running ? Is it still reserved or is it free to get ? That`s worth a try I think. Mario /// ---------------------- www.klangwerker.de mario@klangwerker.de ----------------------

                      J Offline
                      J Offline
                      James R Twine
                      wrote on last edited by
                      #11

                      > The question is what will happen with a mutex-object which owner is no longer running ?    The documentation says that the handle to a created/obtained Mutex object is automatically closed when the process terminates.  I am not sure if that includes abnormal termination.  I am *fairly* certain I have had Mutex objects get "orphaned" on me in the past.    But the thing I would concerned about is not really abnormal termination of your process, but your process trying to shut down, but cannot, because of a rogue/blocked thread, or something like that.  You app will *appear* to have shut down to the user, but will still be sitting in the background someplace.  Of course, you should try to prevent stuff like this from happening, but Sh*t Happens! :eek:    MSDEV tends to do that to me at least twice a day (goes to sleep in the background after a shutdown)! :)    Peace! -=- James.

                      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