How can we detect that the currnet instance of an exe file is running already or not?
How can we detect that the currnet instance of an exe file is running already or not?
there is an easy way and a correct way....
1) the correct way is to use mutexes..... not too easy.
2) the easy way is to get your program to open a dummy file and check beforehand whether that file is already open.If it is you have a previous instance running and can exit or whatever!
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Why are mutexes hard? Just call CreateMutex() with a specific name then call GetLastError() if the mutex alredy existed, then GetLastError() returns ERROR_ALREADY_EXISTS, so you know another copy is running and you can exit. Easy.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
Try,
hMapping = CreateFileMapping((HANDLE)0xffffffff, NULL, PAGE_READONLY, 0, 32, "MyExeMAP");
if (hMapping)// Check to see if app is already running, if so exit
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
MessageBox(NULL, "My Exe is already running.", "Error", MB_OK);
ExitProcess(1);
}
}
else
{
MessageBox(NULL, "Error creating file mapping.", "Error", MB_OK);
ExitProcess(1);
These are the first lines of code in your WINMAIN function.
}