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. Number of instances running

Number of instances running

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 4 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.
  • G Offline
    G Offline
    ganesh dp
    wrote on last edited by
    #1

    Is there any way to find how many instance of an applications are running?

    G S S 3 Replies Last reply
    0
    • G ganesh dp

      Is there any way to find how many instance of an applications are running?

      G Offline
      G Offline
      GAJERA
      wrote on last edited by
      #2

      using EnumWindows API. Which is give the handle of running applications . Get window text from from CWnd * wnd and compare text with your application name.

      1 Reply Last reply
      0
      • G ganesh dp

        Is there any way to find how many instance of an applications are running?

        S Offline
        S Offline
        ShilpiP
        wrote on last edited by
        #3

        Click here ->[^] May be this link will help you..

        Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

        1 Reply Last reply
        0
        • G ganesh dp

          Is there any way to find how many instance of an applications are running?

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

          As long as your code compiles to an exe, you can do it this way:

          1. Add the following lines on top of one of your cpp files:

            #pragma data_seg("shared")
            LONG g_counter = 0; // This variable is allocated on a page shared between processes...
            #pragma data_seg()

            #pragma comment(linker, "/section:shared,rws")

          2. Execute the following instruction as soon as possible at startup of your application:

            InterlockedIncrement(&g_counter);

          3. Execute the following instruction immediately before exiting from your application:

            InterlockedDecrement(&g_counter);

          4. When you need the number of instance of your application currently running, use this code:

            LONG instances = InterlockedExchangeAdd(&g_counter, 0);

          This method works properly only with exe because they are never relocated while loaded; the trick is to create a section whith the read, write and shared attributes and put there a variable used to count the instances. This way that variable is shared between all the instances of your application, and you can use the Interlocked Variable Access[^] functions to safely access the variable. See also data_seg (C/C++)[^] and /SECTION[^] for more details on how this code works.

          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