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. Detecting that an executable is already running (so duplicates don't run)

Detecting that an executable is already running (so duplicates don't run)

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++question
7 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.
  • C Offline
    C Offline
    chasetoys
    wrote on last edited by
    #1

    Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P

    J S D 3 Replies Last reply
    0
    • C chasetoys

      Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      :-D Go look at the FAQs on CP. This is an old question and I am sure it is covered in one of them. INTP “Testing can show the presence of errors, but not their absence.” Edsger Dijkstra

      1 Reply Last reply
      0
      • C chasetoys

        Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        There are lots of ways of doing this. One is to use a mutex (see the CreateMutex API): You call CreateMutex with a unique name (a name which contains a GUID is the smartest choice) to create a mutex. Then if the call succeeds you call GetLastError and if it returns ERROR_ALREADY_EXISTS you're not the first one so you bail. There are ways you can do it using COM also. Steve

        N 1 Reply Last reply
        0
        • S Stephen Hewitt

          There are lots of ways of doing this. One is to use a mutex (see the CreateMutex API): You call CreateMutex with a unique name (a name which contains a GUID is the smartest choice) to create a mutex. Then if the call succeeds you call GetLastError and if it returns ERROR_ALREADY_EXISTS you're not the first one so you bail. There are ways you can do it using COM also. Steve

          N Offline
          N Offline
          Nick_Kisialiou
          wrote on last edited by
          #4

          I think any named kernel object will do, it does not have to be a mutex.

          S 1 Reply Last reply
          0
          • N Nick_Kisialiou

            I think any named kernel object will do, it does not have to be a mutex.

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Yes, there is no need to use a mutex if you prefer some other option. As I said, there are many ways this can be done. Steve

            1 Reply Last reply
            0
            • C chasetoys

              Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P

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

              See here.


              "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

              "We will be known forever by the tracks we leave." - Native American Proverb

              B 1 Reply Last reply
              0
              • D David Crow

                See here.


                "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                "We will be known forever by the tracks we leave." - Native American Proverb

                B Offline
                B Offline
                Blake Miller
                wrote on last edited by
                #7

                If I had a dollar for every time this question was asked here I could pay off my kitchen remodel ... :rolleyes: People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks

                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