# Visual C++ & C++ Programming > Visual C++ Programming >  Is there a better way to check if an app is already running ?

## John E

I work on a cross-platform app, called Mixbus, which checks (at startup) in case there's already a running instance. Here's the code we use in Windows:-



```
#ifdef PLATFORM_WINDOWS
	CreateMutexA (0, 1, string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str ());
	if (GetLastError() == ERROR_ALREADY_EXISTS) {
		Gtk::Main main (argc, argv);
		Gtk::MessageDialog msg (string_compose (_("%1 is already running."), PROGRAM_NAME),
				false, Gtk::MESSAGE_ERROR , Gtk::BUTTONS_OK, true);
		msg.run ();
		exit (EXIT_FAILURE);
	}
#endif
```

Is *CreateMutex* a good way to test this?? It does correctly display the error message if it detects the app already running but one user's been complaining that he often sees a message box saying* Mixbus is already running* - even when it isn't already running. I can't reproduce it on my system but I'm wondering if the code *string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str ()* is sometimes returning NULL? If I understand the MSDN documentation, this would result in the mutex being unnamed (and ERROR_ALREADY_EXISTS only gets returned for a named mutex IIUC). So maybe we should be carrying out some initialisation using *SetLastError()*? Or maybe there's a better way to check, rather than using *CreateMutexA()* in the first place?

----------


## 2kaud

> I'm wondering if the code string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str () is sometimes returning NULL


What does string_compose do? What are PROGRAM_NAME and PROGRAM_VERSION defined as?

In general, yes, CreateMutex is a good way of testing for multiple running instances. It's what we use without any issues AFAIK.

From our code:



```
int main() {
    if (const auto mtxevt {CreateMutex(NULL, TRUE, unqhpevt)}; GetLastError() == ERROR_SUCCESS) {
        // Created OK so only this running
        try {
            // Code to process goes here - we just call the main processing function
        } catch (...) {
            std::cout << "Unhandled exception\n";
        }
        ReleaseMutex(mtxevt);
        CloseHandle(mtxevt);
    } else {
        // Error - already running
        std::cout << "Program already running!\n";
    }
}
```

Note that we check for success - not check for failure.

----------


## VictorN

AFAIK using the mutex was (and I believe is) the best way to check if an app is already running.
There are howwever some "features" conserning what means "app is already running"; in the same session, user account, in the system, ...
The full concept was discussed a lot of times. Some conclusions with the sample code you will find on the Joe Newcomer site: Avoiding Multiple Instances of an Application

----------


## salem_c

Some things to try.

Get your user to try process explorer from https://learn.microsoft.com/en-gb/sysinternals/
It should be possible to search for the name of the mutex.

Also, it can check whether the previous instance of Mixbus has truly gone, or some runt process remains.



> Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.


If it didn't completely exit, then the mutex would remain.

> but I'm wondering if the code string_compose ("%1%2", PROGRAM_NAME, PROGRAM_VERSION).c_str () is sometimes returning NULL?
One way to check is to store the result in a variable, and also print the name as part of the error message.

----------


## John E

Many thanks guys. I'm afraid CodeGuru isn't sending me notifications today but anyway...

*string_compose()* is a function which accepts 2 parameters (either *char** or *int*) and returns std::string. So if PROGRAM_NAME is *Mixbus* and PROGRAM_VERSION is *19* it'll return *Mixbus19*

I deliberately tried making it return an empty string (i.e. so that* CreateMutexA()*  would generate an unnamed mutex) and as expected, *GetLastError()* then returns zero.

Having said all that... I'd no idea that the term "multiple instance" could have different meanings!!  I'll do a bit more reading.

----------


## John E

> I'm afraid CodeGuru isn't sending me notifications today


Whaaa? I just got a notification for this post which got submitted over 6 weeks ago!!

----------


## 2kaud

It's a known issue....

From Steve Jones (admin) 12 Nov 2022:




> OH BOY....I just now got an email TWELVE DAYS LATE.... Anyway, the boss said it might be a while before they can get someone to fix the issue(s).

----------


## VictorN

> Whaaa? I just got a notification for this post which got submitted over 6 weeks ago!!


Yes, this CG Bug is already some months old. And it will be probably fixed in the future...  :Cool:  :Roll Eyes (Sarcastic):

----------

