Detecting that an executable is already running (so duplicates don't run)
-
Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P
:-D Go look at the FAQs on CP. This is an old question and I am sure it is covered in one of them. INTP “Testing can show the presence of errors, but not their absence.” Edsger Dijkstra
-
Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P
There are lots of ways of doing this. One is to use a mutex (see the
CreateMutex
API): You callCreateMutex
with a unique name (a name which contains a GUID is the smartest choice) to create a mutex. Then if the call succeeds you callGetLastError
and if it returnsERROR_ALREADY_EXISTS
you're not the first one so you bail. There are ways you can do it using COM also. Steve -
There are lots of ways of doing this. One is to use a mutex (see the
CreateMutex
API): You callCreateMutex
with a unique name (a name which contains a GUID is the smartest choice) to create a mutex. Then if the call succeeds you callGetLastError
and if it returnsERROR_ALREADY_EXISTS
you're not the first one so you bail. There are ways you can do it using COM also. SteveI think any named kernel object will do, it does not have to be a mutex.
-
I think any named kernel object will do, it does not have to be a mutex.
Yes, there is no need to use a mutex if you prefer some other option. As I said, there are many ways this can be done. Steve
-
Hey folks: If you only want to have one instance of your .exe running, how can you detect that it is already running, and then exit gracefully? (MFC is OK, but no .NET please), and I'm a noobster so please keep that in mind when answering :P
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb
-
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb
If I had a dollar for every time this question was asked here I could pay off my kitchen remodel ... :rolleyes: People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks