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 duplicate Exe running simultaneously

Avoiding duplicate Exe running simultaneously

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
9 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.
  • J Offline
    J Offline
    J_E_D_I
    wrote on last edited by
    #1

    Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks

    R J G 3 Replies Last reply
    0
    • J J_E_D_I

      Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks

      R Offline
      R Offline
      rp_suman
      wrote on last edited by
      #2

      You can use Mutex to do single instance check. An article here: How to limit 32-bit applications to one instance in Visual C++[^] Thanks, Suman :)

      -- "Programming is an art that fights back!"

      J 2 Replies Last reply
      0
      • R rp_suman

        You can use Mutex to do single instance check. An article here: How to limit 32-bit applications to one instance in Visual C++[^] Thanks, Suman :)

        -- "Programming is an art that fights back!"

        J Offline
        J Offline
        J_E_D_I
        wrote on last edited by
        #3

        Excellent advice, it seems very well explained. Are we sure it comes from microsoft?? :omg:

        1 Reply Last reply
        0
        • J J_E_D_I

          Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks

          J Offline
          J Offline
          Jim Crafton
          wrote on last edited by
          #4

          rp_suman's link is good, but there's a better article here on CP http://www.codeproject.com/KB/cpp/avoidmultinstance.aspx[^] This is particularly good to read if you have a GUI for your app.

          ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

          J 1 Reply Last reply
          0
          • R rp_suman

            You can use Mutex to do single instance check. An article here: How to limit 32-bit applications to one instance in Visual C++[^] Thanks, Suman :)

            -- "Programming is an art that fights back!"

            J Offline
            J Offline
            J_E_D_I
            wrote on last edited by
            #5

            I tried but it doesn't work. For it to work you'll need to use the old syntax (/clr:oldSyntax), which is incompatible with my application which uses a static library rather than dlls. Thanks anyway

            1 Reply Last reply
            0
            • J Jim Crafton

              rp_suman's link is good, but there's a better article here on CP http://www.codeproject.com/KB/cpp/avoidmultinstance.aspx[^] This is particularly good to read if you have a GUI for your app.

              ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

              J Offline
              J Offline
              J_E_D_I
              wrote on last edited by
              #6

              Thanks, but it seems a bit complex for me.. :doh: Is there an alternative without having to use an object? I'm relatively new to c++ and I haven't used objects in my program so far.

              J 1 Reply Last reply
              0
              • J J_E_D_I

                Hi. Any body could please advise on how to implement a simple c++ instruction to prevent another instance of the same program that is already running from being executed. Many thanks

                G Offline
                G Offline
                Gary R Wheeler
                wrote on last edited by
                #7

                Here's my solution:

                /* MultipleInstance.h */
                 
                class MultipleInstance {
                 
                public:
                 
                MultipleInstance();
                ~MultipleInstance();
                 
                static bool Active();
                 
                private:
                 
                static bool _Active;
                 
                static bool _Initialized;
                static HANDLE _Handle;
                static MultipleInstance *_Instance;
                 
                };
                 
                /* MultipleInstance.cpp */
                 
                static MultipleInstance _MultipleInstance;
                 
                bool MultipleInstance::_Active = false;
                 
                bool MultipleInstance::_Initialized = false;
                HANDLE MultipleInstance::_Handle = NULL;
                MultipleInstance *MultipleInstance::_Instance = &_MultipleInstance;
                 
                MultipleInstance::MultipleInstance()
                {
                if ((!_Initialized) && (this == _Instance)) {
                 
                // create security descriptor to let all users access the semaphore
                 
                SECURITY_DESCRIPTOR security_descriptor = { 0 };
                 
                InitializeSecurityDescriptor(&security_descriptor,
                SECURITY_DESCRIPTOR_REVISION);
                 
                SetSecurityDescriptorDacl(&security_descriptor,TRUE,NULL,FALSE);
                 
                // create semaphore
                 
                SECURITY_ATTRIBUTES security_attributes = { 0 };
                 
                security_attributes.nLength = sizeof(security_attributes);
                security_attributes.lpSecurityDescriptor = &security_descriptor;
                security_attributes.bInheritHandle = FALSE;
                 
                SetLastError(0);
                 
                _Handle = ::CreateSemaphore(&security_attributes,0,1,
                _T("Global\\InstanceSemaphore"));
                /* change "InstanceSemaphore" to your desired name */
                 
                if (GetLastError() == ERROR_ALREADY_EXISTS) {
                _Active = true;
                }
                 
                _Initialized = true;
                 
                }
                }
                 
                MultipleInstance::~MultipleInstance()
                {
                if (_Initialized && (this == _Instance)) {
                 
                if (_Handle != INVALID_HANDLE_VALUE) &&
                (_Handle != NULL)) {
                 
                CloseHandle(_Handle);
                _Handle = NULL;
                 
                }
                 
                }
                }
                 
                bool MultipleInstance::Active()
                {
                return _Active;
                }

                Software Zen: delete this;

                J 1 Reply Last reply
                0
                • J J_E_D_I

                  Thanks, but it seems a bit complex for me.. :doh: Is there an alternative without having to use an object? I'm relatively new to c++ and I haven't used objects in my program so far.

                  J Offline
                  J Offline
                  Jim Crafton
                  wrote on last edited by
                  #8

                  Well in that case you've got a lot of reading ahead of you! Yes, you can do this without using classes, *but* the core technique outlined in the article really is the only good way to do this, as it properly deals with concurrency issues that many of the other approaches don't deal with, at least for an app that has a GUI. If you don't have a GUI then you may not need something as sophisticated.

                  ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

                  1 Reply Last reply
                  0
                  • G Gary R Wheeler

                    Here's my solution:

                    /* MultipleInstance.h */
                     
                    class MultipleInstance {
                     
                    public:
                     
                    MultipleInstance();
                    ~MultipleInstance();
                     
                    static bool Active();
                     
                    private:
                     
                    static bool _Active;
                     
                    static bool _Initialized;
                    static HANDLE _Handle;
                    static MultipleInstance *_Instance;
                     
                    };
                     
                    /* MultipleInstance.cpp */
                     
                    static MultipleInstance _MultipleInstance;
                     
                    bool MultipleInstance::_Active = false;
                     
                    bool MultipleInstance::_Initialized = false;
                    HANDLE MultipleInstance::_Handle = NULL;
                    MultipleInstance *MultipleInstance::_Instance = &_MultipleInstance;
                     
                    MultipleInstance::MultipleInstance()
                    {
                    if ((!_Initialized) && (this == _Instance)) {
                     
                    // create security descriptor to let all users access the semaphore
                     
                    SECURITY_DESCRIPTOR security_descriptor = { 0 };
                     
                    InitializeSecurityDescriptor(&security_descriptor,
                    SECURITY_DESCRIPTOR_REVISION);
                     
                    SetSecurityDescriptorDacl(&security_descriptor,TRUE,NULL,FALSE);
                     
                    // create semaphore
                     
                    SECURITY_ATTRIBUTES security_attributes = { 0 };
                     
                    security_attributes.nLength = sizeof(security_attributes);
                    security_attributes.lpSecurityDescriptor = &security_descriptor;
                    security_attributes.bInheritHandle = FALSE;
                     
                    SetLastError(0);
                     
                    _Handle = ::CreateSemaphore(&security_attributes,0,1,
                    _T("Global\\InstanceSemaphore"));
                    /* change "InstanceSemaphore" to your desired name */
                     
                    if (GetLastError() == ERROR_ALREADY_EXISTS) {
                    _Active = true;
                    }
                     
                    _Initialized = true;
                     
                    }
                    }
                     
                    MultipleInstance::~MultipleInstance()
                    {
                    if (_Initialized && (this == _Instance)) {
                     
                    if (_Handle != INVALID_HANDLE_VALUE) &&
                    (_Handle != NULL)) {
                     
                    CloseHandle(_Handle);
                    _Handle = NULL;
                     
                    }
                     
                    }
                    }
                     
                    bool MultipleInstance::Active()
                    {
                    return _Active;
                    }

                    Software Zen: delete this;

                    J Offline
                    J Offline
                    J_E_D_I
                    wrote on last edited by
                    #9

                    Hi, many many thanks for the advice but not being familiar to oject programming I am missing some crucial bits to make it work. In MultipleInstance.h what name should I give to: #ifndef #define Is there anything else I need to specify? In MultipleInstance.cpp I have specified: #include <iostream> #include "MultipleInstance.h" Is anything else needed? In the main cpp file: What is the correct syntax I need to use to call the object? Should I insert it immediately after int _tmain(int argc, _TCHAR* argv[]){

                    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