C / C ++ How do I know if a program is running?

In a Windows environment, I do not want two instances of my program running at the same time.

Related

Does Mutex use to prevent multiple instances of the same program from working safely?

+2
source share
10 answers

I think you need to think a little about your script before you go ahead. There are many different interpretations of “launching the same program” more than once. For example, you

  • Once per car
  • Once per login session
  • Once per user

They all have different, albeit similar, solutions.

The easiest way to describe for each machine. In this case, you want to create a named Mutex. One start of each program should receive this mutex, if they are successful, they are launched and held on Mutex throughout the entire life cycle of the process. Otherwise, some other program starts and they exit immediately.

Unfortunately, this approach also has its drawbacks. If I want to ruin your program, I can create a mutex with the same name. This will not allow your program to run any instance, because they cannot determine who is holding Mutex, just something is holding the mutex.

+10
source

You can create a mutex when the first instance of your application begins. To prevent the second instance, all you have to do is check if the mutex is used.

Actually the question arose regarding the use of mutexes for this purpose here , to check out JaredPar's answer.

Note. You can use the local mutex if you want the "single instance" to be used only in the user session (and not for all users).

+9
source

If this is your program, then the shortest possible version under the windows:

int main(int argc, char** argv) { CreateMutexA(0, FALSE, "Local\\$myprogram$"); // try to create a named mutex if(GetLastError() == ERROR_ALREADY_EXISTS) // did the mutex already exist? return -1; // quit; mutex is released automatically // ... program code ... } 

No need to go crazy if all you need is a simple check ...

+4
source

The best way is to use a mutex. See Using Mutex Objects .

+3
source

An alternative simple solution is to create a suitable unique global named event (possibly a GUID string), and then check for its existence at startup. If it exists, an instance of your application is already running. If not, you automatically created an event and can continue execution, for example:

 // for brevity, a complete set of error handling has been omitted m_hEvent = CreateEvent(NULL, TRUE, FALSE, szMyUniqueNamedEvent); switch (GetLastError) { // app is already running case ERROR_ALREADY_EXISTS: { CloseHandle(m_hEvent); // now exit break; } // this is the first instance of the app case ERROR_SUCCESS: { // global event created and new instance of app is running, // continue on, don't forget to clean up m_hEvent on exit break; } } 
+3
source

this is class I, using boost.interrprocess , I use it to synchronize between GUI and CLI versions.

You may find it helpful:

 #pragma once #include <boost/interprocess/windows_shared_memory.hpp> #include <boost/interprocess/mapped_region.hpp> #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif using boost::interprocess::windows_shared_memory; using boost::interprocess::mapped_region; using boost::interprocess::open_or_create; using boost::interprocess::read_write; using boost::interprocess::interprocess_exception; class CProcessMutex { public: CProcessMutex() : m_region() , m_status(false) { initSharedMemory(); Increment(); } ~CProcessMutex() { Decrease(); } public: int GetCount() { return m_status ? *(static_cast<unsigned char*>(m_region.get_address())) : 0; } private: void initSharedMemory() { try { //Create a native windows shared memory object. windows_shared_memory shm (open_or_create, "shared_memory", read_write, 1); //Map the whole shared memory in this process m_region.swap(mapped_region(shm, read_write)); m_status = true; } catch(interprocess_exception &ex) { ex.what(); m_status = false; } } void Increment() { if(m_status) (*(static_cast<unsigned char*>(m_region.get_address())))++; } void Decrease() { if(m_status) (*(static_cast<unsigned char*>(m_region.get_address())))--; } private: mapped_region m_region; bool m_status; }; 

use is simple:

 CProcessMutex pm; size_t current_process_count = pm.GetCount(); if(current_process_count > 1) { ... } 

so you can easily limit the number of processes in parallel.

+2
source

When you use Qt, you can download QtSingleApplication .

+2
source

Use a mutex as others have suggested.

This MS CreateMutex () documentation contains a lot of useful information and specifically addresses the use of mutexes to prevent more than one instance of a running program. In particular:

  • Call CreateMutex() with bInitialOwner = FALSE , then call the wait function (e.g. WaitForSingleObject() ) to make sure that only one instance gets the mutex.
  • Instead, consider using a locked file if you are worried about denial of service attacks.
+1
source

When you start your program, you can list the processes running on your computer

Then, if you see that you are already working, close

0
source

You can check if the window class is registered. Take a look at this MSDN entry.

-one
source

All Articles