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. Making an app single instance

Making an app single instance

Scheduled Pinned Locked Moved C / C++ / MFC
delphibusiness
8 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.
  • S Offline
    S Offline
    sashoalm
    wrote on last edited by
    #1

    Hello, I have an app which I need to make single instance. When the second instance is called it needs to bring the window of the first instance to the front, and pass it its command line params in some way. And I need the program to be single-instance on session only - i.e. only one instance is allowed per user, but many users logged on can each have their instance. There are a lot of articles on this in codeproject, but I'm not sure which of them is the best and covers all the requirements.

    There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

    _ K C D 4 Replies Last reply
    0
    • S sashoalm

      Hello, I have an app which I need to make single instance. When the second instance is called it needs to bring the window of the first instance to the front, and pass it its command line params in some way. And I need the program to be single-instance on session only - i.e. only one instance is allowed per user, but many users logged on can each have their instance. There are a lot of articles on this in codeproject, but I'm not sure which of them is the best and covers all the requirements.

      There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Use the named mutex method. This is by default unique per session unless you prefix "Global\" to the mutex name.

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      1 Reply Last reply
      0
      • S sashoalm

        Hello, I have an app which I need to make single instance. When the second instance is called it needs to bring the window of the first instance to the front, and pass it its command line params in some way. And I need the program to be single-instance on session only - i.e. only one instance is allowed per user, but many users logged on can each have their instance. There are a lot of articles on this in codeproject, but I'm not sure which of them is the best and covers all the requirements.

        There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

        K Offline
        K Offline
        Kushagra Tiwari
        wrote on last edited by
        #3

        I think you can make your class as singleton by making you contructors and destructor as private and publically expose a static method which will return the same instance once an instance is created. That shud take care of your problem.. Google for singleton class examples ..u will get loads of them .. -Kushagra

        S 1 Reply Last reply
        0
        • S sashoalm

          Hello, I have an app which I need to make single instance. When the second instance is called it needs to bring the window of the first instance to the front, and pass it its command line params in some way. And I need the program to be single-instance on session only - i.e. only one instance is allowed per user, but many users logged on can each have their instance. There are a lot of articles on this in codeproject, but I'm not sure which of them is the best and covers all the requirements.

          There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

          C Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          another way, if you are certain about the window caption, is to use FindWindow() API to search for already running application. To make it unique, you may use your own name for your window class, and use it in FindWindow().

          S 1 Reply Last reply
          0
          • C Cool_Dev

            another way, if you are certain about the window caption, is to use FindWindow() API to search for already running application. To make it unique, you may use your own name for your window class, and use it in FindWindow().

            S Offline
            S Offline
            sashoalm
            wrote on last edited by
            #5

            I've used FindWindow before, but I remember it had a lot of issues, for example (I don't remember) but didn't it fail if the window was minimized?

            There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

            C 1 Reply Last reply
            0
            • S sashoalm

              Hello, I have an app which I need to make single instance. When the second instance is called it needs to bring the window of the first instance to the front, and pass it its command line params in some way. And I need the program to be single-instance on session only - i.e. only one instance is allowed per user, but many users logged on can each have their instance. There are a lot of articles on this in codeproject, but I'm not sure which of them is the best and covers all the requirements.

              There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

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

              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
              • K Kushagra Tiwari

                I think you can make your class as singleton by making you contructors and destructor as private and publically expose a static method which will return the same instance once an instance is created. That shud take care of your problem.. Google for singleton class examples ..u will get loads of them .. -Kushagra

                S Offline
                S Offline
                Sauro Viti
                wrote on last edited by
                #7

                The singleton pattern is not applicable in this case: its purpose is to have a unique instance of an object for each process. If you have two or more different instances of the same application, each of them runs on a different process with its own address space, then two applications cannot share the singleton object. Cheers, Sauro

                1 Reply Last reply
                0
                • S sashoalm

                  I've used FindWindow before, but I remember it had a lot of issues, for example (I don't remember) but didn't it fail if the window was minimized?

                  There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

                  C Offline
                  C Offline
                  Cool_Dev
                  wrote on last edited by
                  #8

                  no.. you can find out minimized windows also.

                  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