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. Stopping multiple copies of application from opening

Stopping multiple copies of application from opening

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 6 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 Offline
    M Offline
    MikeRWinter
    wrote on last edited by
    #1

    Hi all I have a program written in VC6 (running on XP) that I want to stop from having multiple copies open. I want it to check whether there is another instance of itself running on start up and close if there is. Using FindWindow() doesnt work as it finds itself! Thanks for any help Mike

    P K D C I 5 Replies Last reply
    0
    • M MikeRWinter

      Hi all I have a program written in VC6 (running on XP) that I want to stop from having multiple copies open. I want it to check whether there is another instance of itself running on start up and close if there is. Using FindWindow() doesnt work as it finds itself! Thanks for any help Mike

      P Offline
      P Offline
      Peter Weyzen
      wrote on last edited by
      #2

      Consider creating a global named mutex. Each app does this: 1) Create mutex -- give it a unique name, possibly including a GUID 2) WaitForSingleObject -- to capture the mutex. If this fails, then there's someone else holding it. On exit, ReleaseMutex() to give it up -- and make it available again.

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

      1 Reply Last reply
      0
      • M MikeRWinter

        Hi all I have a program written in VC6 (running on XP) that I want to stop from having multiple copies open. I want it to check whether there is another instance of itself running on start up and close if there is. Using FindWindow() doesnt work as it finds itself! Thanks for any help Mike

        K Offline
        K Offline
        KingsGambit
        wrote on last edited by
        #3

        You can try creating a named mutex with CreateMutex() API and checking the result with GetLastError(). When the second instance of the app tries to create it, GetLastError() will return ERROR_ALREADY_EXISTS as the mutex is already created by the first instance. http://vcfaq.mvps.org/mfc/2.htm

        1 Reply Last reply
        0
        • M MikeRWinter

          Hi all I have a program written in VC6 (running on XP) that I want to stop from having multiple copies open. I want it to check whether there is another instance of itself running on start up and close if there is. Using FindWindow() doesnt work as it finds itself! Thanks for any help Mike

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

          See here.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          1 Reply Last reply
          0
          • M MikeRWinter

            Hi all I have a program written in VC6 (running on XP) that I want to stop from having multiple copies open. I want it to check whether there is another instance of itself running on start up and close if there is. Using FindWindow() doesnt work as it finds itself! Thanks for any help Mike

            C Offline
            C Offline
            Chandrasekharan P
            wrote on last edited by
            #5

            Use this code..

            if(NULL!=:CreateMutex(NULL,TRUE,_T("SingleInstanceApp")))
            {
            long DwError = ::GetLastError();
            if(dwError == ERROR_ALREDY_EXISTS)
            EndDialog(IDOK)
            }

            Hope it helps

            1 Reply Last reply
            0
            • M MikeRWinter

              Hi all I have a program written in VC6 (running on XP) that I want to stop from having multiple copies open. I want it to check whether there is another instance of itself running on start up and close if there is. Using FindWindow() doesnt work as it finds itself! Thanks for any help Mike

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #6

              I would also have a look at the articles here on codeproject... (shocking idea, I know...) eg: Limiting an application to a single Instance - the MFC way[^] is a great one. Just read my comments at the end to make it work. Iain.

              I have now moved to Sweden for love (awwww).

              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