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. avoiding multiple instances

avoiding multiple instances

Scheduled Pinned Locked Moved C / C++ / MFC
c++toolshelpquestionannouncement
19 Posts 6 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.
  • M Offline
    M Offline
    Mark Donkers
    wrote on last edited by
    #1

    I'm using the code from following posting to avoid running multiple instances of my app. I'm running an MFC SDI application. Where is the ideal location to create and release the mutex? //======================================================= Subject: Re: Avoiding multiple instances of an application. Sender: John Uhlenbrock Date: 15:53 7 Aug '01 I've seen a few examples to avoid multiple instances of an application. In fact, there are a few of them here on code project. However, they all seem like a lot of code, when there is an easy solution like: HANDLE hMutex = CreateMutex( NULL, TRUE, "MVD Load Utility" ); if( (hMutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS) ) { AfxMessageBox( "Two copies of this program are not allowed to run at the same time." ); return FALSE; } Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

    N 1 Reply Last reply
    0
    • M Mark Donkers

      I'm using the code from following posting to avoid running multiple instances of my app. I'm running an MFC SDI application. Where is the ideal location to create and release the mutex? //======================================================= Subject: Re: Avoiding multiple instances of an application. Sender: John Uhlenbrock Date: 15:53 7 Aug '01 I've seen a few examples to avoid multiple instances of an application. In fact, there are a few of them here on code project. However, they all seem like a lot of code, when there is an easy solution like: HANDLE hMutex = CreateMutex( NULL, TRUE, "MVD Load Utility" ); if( (hMutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS) ) { AfxMessageBox( "Two copies of this program are not allowed to run at the same time." ); return FALSE; } Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      CWinApp::InitInstance Nish p.s. Of course that will be the CWinApp derived class in your case... Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

      M 1 Reply Last reply
      0
      • N Nish Nishant

        CWinApp::InitInstance Nish p.s. Of course that will be the CWinApp derived class in your case... Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

        M Offline
        M Offline
        Mark Donkers
        wrote on last edited by
        #3

        Nish, thanks for the quick response. do I make the mutex's handle a member of my CWinApp derived class. then how (and where) should I realease it? Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

        J 1 Reply Last reply
        0
        • M Mark Donkers

          Nish, thanks for the quick response. do I make the mutex's handle a member of my CWinApp derived class. then how (and where) should I realease it? Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          No need to release it. The system does it for you when the program exits. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          M 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            No need to release it. The system does it for you when the program exits. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            M Offline
            M Offline
            Mark Donkers
            wrote on last edited by
            #5

            great... thanks. As a side note, I have to say THANKS to everyone at CodeProject, and everyone that uses CodeProject. I started using VC++ in September on my first real project at work. Coming from Visual Basic, I was a little intimidated making the transition, but CP has really made it less painless, and even enjoyable experience. Thanks for help everyone. Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

            J 1 Reply Last reply
            0
            • M Mark Donkers

              great... thanks. As a side note, I have to say THANKS to everyone at CodeProject, and everyone that uses CodeProject. I started using VC++ in September on my first real project at work. Coming from Visual Basic, I was a little intimidated making the transition, but CP has really made it less painless, and even enjoyable experience. Thanks for help everyone. Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

              J Offline
              J Offline
              Joaquin M Lopez Munoz
              wrote on last edited by
              #6

              Marcus, I think there's a flaw in your piece of code. Instead of

              CreateMutex( NULL, TRUE, "MVD Load Utility" )

              you should use

              CreateMutex( NULL, FALSE, "MVD Load Utility" );

              Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

              M 1 Reply Last reply
              0
              • J Joaquin M Lopez Munoz

                Marcus, I think there's a flaw in your piece of code. Instead of

                CreateMutex( NULL, TRUE, "MVD Load Utility" )

                you should use

                CreateMutex( NULL, FALSE, "MVD Load Utility" );

                Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                M Offline
                M Offline
                Mark Donkers
                wrote on last edited by
                #7

                thanks. I read the MSDN on CreateMutex, but I can't make heads or tails of what the difference is. BTW - in your previous example, how did you get the code to appear in beige? Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

                J N 2 Replies Last reply
                0
                • M Mark Donkers

                  thanks. I read the MSDN on CreateMutex, but I can't make heads or tails of what the difference is. BTW - in your previous example, how did you get the code to appear in beige? Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

                  J Offline
                  J Offline
                  Joaquin M Lopez Munoz
                  wrote on last edited by
                  #8

                  thanks. I read the MSDN on CreateMutex, but I can't make heads or tails of what the difference is. The difference is that when bInitialOwner is set to TRUE, the system gives ownership to the first process that creates the mutex, as if it has subsequently called WaitForSingleObject on the mutex handle. As ownership is not needed for your purposes (you are only resorting to mutexes as a convenient way to share global identifier across proceses), bInitialOwner is best left as FALSE (moreover, you'd have to expliciy call RelaseMutex later, which is not the same as closing the handle). Pff, what a messy explanation, hope you've understand something of it :) To get the beige background enclose your code between <pre> and </pre> tags. Regards Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                  N M 2 Replies Last reply
                  0
                  • M Mark Donkers

                    thanks. I read the MSDN on CreateMutex, but I can't make heads or tails of what the difference is. BTW - in your previous example, how did you get the code to appear in beige? Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #9

                    Use the <pre> tags!

                    this is between two 'pre' tags

                    Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                    1 Reply Last reply
                    0
                    • J Joaquin M Lopez Munoz

                      thanks. I read the MSDN on CreateMutex, but I can't make heads or tails of what the difference is. The difference is that when bInitialOwner is set to TRUE, the system gives ownership to the first process that creates the mutex, as if it has subsequently called WaitForSingleObject on the mutex handle. As ownership is not needed for your purposes (you are only resorting to mutexes as a convenient way to share global identifier across proceses), bInitialOwner is best left as FALSE (moreover, you'd have to expliciy call RelaseMutex later, which is not the same as closing the handle). Pff, what a messy explanation, hope you've understand something of it :) To get the beige background enclose your code between <pre> and </pre> tags. Regards Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #10

                      If his program exits, he need not call ReleaseMutex as the mutex object itself gets destroyed [or am I highly erroneous here?] Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                      J J 2 Replies Last reply
                      0
                      • N Nish Nishant

                        If his program exits, he need not call ReleaseMutex as the mutex object itself gets destroyed [or am I highly erroneous here?] Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                        J Offline
                        J Offline
                        Jon Hulatt
                        wrote on last edited by
                        #11

                        "highly erroneous"? that sounds painful! Sorry to dissapoint you all with my lack of a witty or poignant signature.

                        N 1 Reply Last reply
                        0
                        • J Joaquin M Lopez Munoz

                          thanks. I read the MSDN on CreateMutex, but I can't make heads or tails of what the difference is. The difference is that when bInitialOwner is set to TRUE, the system gives ownership to the first process that creates the mutex, as if it has subsequently called WaitForSingleObject on the mutex handle. As ownership is not needed for your purposes (you are only resorting to mutexes as a convenient way to share global identifier across proceses), bInitialOwner is best left as FALSE (moreover, you'd have to expliciy call RelaseMutex later, which is not the same as closing the handle). Pff, what a messy explanation, hope you've understand something of it :) To get the beige background enclose your code between <pre> and </pre> tags. Regards Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                          M Offline
                          M Offline
                          Mark Donkers
                          wrote on last edited by
                          #12

                          Joaquín... You have shed some light in this dark world of mine. X| It made as much sense as it could for a beginner C++ programmer.

                          where can I find the docs on how to use other tags (or are there any other)

                          thanks again. Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

                          J M 2 Replies Last reply
                          0
                          • N Nish Nishant

                            If his program exits, he need not call ReleaseMutex as the mutex object itself gets destroyed [or am I highly erroneous here?] Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                            J Offline
                            J Offline
                            Joaquin M Lopez Munoz
                            wrote on last edited by
                            #13

                            Hi Nish, Well, the thing is a little tricky, because the documentation is not conclusive on this. I haven't found nowehere that closing a mutex handle implies also releasing it, so to the best of my knowledge not calling ReleaseMutex is undefined behavior (though it is perfectly possible that everything works nevertheless). All in all, as the guy didn't need ownership at all, I think it is better to remain on the safe side and simply setting bInitialOwner to FALSE Regards, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                            N T 2 Replies Last reply
                            0
                            • M Mark Donkers

                              Joaquín... You have shed some light in this dark world of mine. X| It made as much sense as it could for a beginner C++ programmer.

                              where can I find the docs on how to use other tags (or are there any other)

                              thanks again. Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

                              J Offline
                              J Offline
                              Joaquin M Lopez Munoz
                              wrote on last edited by
                              #14

                              Apart from <pre> you can use <code> to get an effect like this. If you're including code of yours that may have the character '<', replace those with the escape sequence < Also, you can use regular HTML tags as well. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                              1 Reply Last reply
                              0
                              • J Jon Hulatt

                                "highly erroneous"? that sounds painful! Sorry to dissapoint you all with my lack of a witty or poignant signature.

                                N Offline
                                N Offline
                                Nish Nishant
                                wrote on last edited by
                                #15

                                LOL Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                                1 Reply Last reply
                                0
                                • J Joaquin M Lopez Munoz

                                  Hi Nish, Well, the thing is a little tricky, because the documentation is not conclusive on this. I haven't found nowehere that closing a mutex handle implies also releasing it, so to the best of my knowledge not calling ReleaseMutex is undefined behavior (though it is perfectly possible that everything works nevertheless). All in all, as the guy didn't need ownership at all, I think it is better to remain on the safe side and simply setting bInitialOwner to FALSE Regards, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                                  N Offline
                                  N Offline
                                  Nish Nishant
                                  wrote on last edited by
                                  #16

                                  Thanks for your thoughts and time Joaquín! I guess you were correct in your advice to him :-) Regards Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut

                                  1 Reply Last reply
                                  0
                                  • J Joaquin M Lopez Munoz

                                    Hi Nish, Well, the thing is a little tricky, because the documentation is not conclusive on this. I haven't found nowehere that closing a mutex handle implies also releasing it, so to the best of my knowledge not calling ReleaseMutex is undefined behavior (though it is perfectly possible that everything works nevertheless). All in all, as the guy didn't need ownership at all, I think it is better to remain on the safe side and simply setting bInitialOwner to FALSE Regards, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                                    T Offline
                                    T Offline
                                    Tim Smith
                                    wrote on last edited by
                                    #17

                                    Mutexes are automatically released. Here is a quote from Helen Custler's Inside Windows NT book which talks about this history of mutants and mutexes in NT. "The name /mutant/ has a colorful [sic] history. Early in Windows NT's development, Dave Cutler created a kernel mutex object that implemented low-level mutual exclusion. Later he discovered that OS/2 required a version of the mutual-exclusion semaphore with additional semantics, which Dave considered "brain-damaged" and which was incompatible with the original object. (Specifically, a thread could abandon the object and leave it inaccessible.) So he created an OS/2 version of the mutex and gave it the name /mutant/. Later Dave modified the mutant object to remove the OS/2 semantics, allowing the Win32 subsystem to use the object. The Win32 API calls the modified object /mutex/, but the native services retain the name /mutant/." Tim Smith Descartes Systems Sciences, Inc.

                                    J 1 Reply Last reply
                                    0
                                    • T Tim Smith

                                      Mutexes are automatically released. Here is a quote from Helen Custler's Inside Windows NT book which talks about this history of mutants and mutexes in NT. "The name /mutant/ has a colorful [sic] history. Early in Windows NT's development, Dave Cutler created a kernel mutex object that implemented low-level mutual exclusion. Later he discovered that OS/2 required a version of the mutual-exclusion semaphore with additional semantics, which Dave considered "brain-damaged" and which was incompatible with the original object. (Specifically, a thread could abandon the object and leave it inaccessible.) So he created an OS/2 version of the mutex and gave it the name /mutant/. Later Dave modified the mutant object to remove the OS/2 semantics, allowing the Win32 subsystem to use the object. The Win32 API calls the modified object /mutex/, but the native services retain the name /mutant/." Tim Smith Descartes Systems Sciences, Inc.

                                      J Offline
                                      J Offline
                                      Joaquin M Lopez Munoz
                                      wrote on last edited by
                                      #18

                                      Beautiful piece of wisdom :) Anyway, I wouldn't rely on only this paragraph to merrily abandon mutexes unreleased, withouth stronger confirmation from the documentation. Regards, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

                                      1 Reply Last reply
                                      0
                                      • M Mark Donkers

                                        Joaquín... You have shed some light in this dark world of mine. X| It made as much sense as it could for a beginner C++ programmer.

                                        where can I find the docs on how to use other tags (or are there any other)

                                        thanks again. Marcus Make no little plans; they have no magic to stir your blood to action. Make big plans, aim high in work and hope -- Daniel Burnham

                                        M Offline
                                        M Offline
                                        Michael Dunn
                                        wrote on last edited by
                                        #19

                                        Marcus2001 wrote: where can I find the docs on how to use other tags In the VC forum FAQ :) --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?"  -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

                                        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