3 ms·
I looked at the title, then briefly glanced over the discussion here, and guessed it correctly --- "same mutex name". I didn't guess they'd get to the same name
by userbinator 7y ago
I looked at the title, then briefly glanced over the discussion here, and guessed it correctly --- "same mutex name". I didn't guess they'd get to the same name that way, however. I thought someone had copy-pasted example code from SO without changing a REPLACE_MUTEX_NAME or similar.
My preferred solution is simpler --- CreateMutex takes a mutex name of at most MAX_PATH characters. That's almost begging you to make it the path to the running executable.
char name[MAX_PATH];
GetModuleFileName(0, name, sizeof(name));
I know some of you may be thinking "but what if there are two copies of the application in different directories?" Yes, that would result in two instances, but how often is that a problem? If it is, then make it the binary name-only without the path
pname = strrchr(name,'\\');
and then unless you name your application something very generic like test.exe, there can only be one. I guess the lesson here is to not overthink things --- I see that often in SO answers too.