In case it matters, I'm using Dev-C++ 4.9.8.7 (and LOVING it).
I'm looking for a way to prevent multiple instances of my application from running at the same time. Instead of starting a new instance of an application if one is already running, I need the original window to popup automatically.
I have found a few code examples for doing this, but most of them require using MFC. I did find an article on doing this on developerfusion titled "Avoiding Multiple Instances of an Application" (http://www.developerfusion.com/show/1716/), which has a very clear example using a named mutex to detect multiple instances, and a shared data segment to store the handle of the main window. This is done with the following code:
Using the code in this article works to the extent that a second instance of my application will NOT start, but I have been unable to have the original instance's window popup like it should. I suspect it has to do with the #pragma directives (which I freely admit, I do NOT understand).
Can anyone help me? Is there another way to do this or am I out of luck?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In case it matters, I'm using Dev-C++ 4.9.8.7 (and LOVING it).
I'm looking for a way to prevent multiple instances of my application from running at the same time. Instead of starting a new instance of an application if one is already running, I need the original window to popup automatically.
I have found a few code examples for doing this, but most of them require using MFC. I did find an article on doing this on developerfusion titled "Avoiding Multiple Instances of an Application" (http://www.developerfusion.com/show/1716/), which has a very clear example using a named mutex to detect multiple instances, and a shared data segment to store the handle of the main window. This is done with the following code:
#pragma comment(linker, "/SECTION:.shr,RWS")
#pragma data_seg(".shr")
HWND hGlobal = NULL;
#pragma data_seg
Using the code in this article works to the extent that a second instance of my application will NOT start, but I have been unable to have the original instance's window popup like it should. I suspect it has to do with the #pragma directives (which I freely admit, I do NOT understand).
Can anyone help me? Is there another way to do this or am I out of luck?
// Verify that we are not already running...
// Create mutex...
CreateMutex(NULL, TRUE, "MyProgram");
// Already running, terminate...
if(GetLastError() == ERROR_ALREADY_EXISTS)
ExitProcess(1);
=)
Kip
I implemented this myself just a few days ago.
HANDLE Mutex = CreateMutex(NULL, TRUE, "DBClip");
if (GetLastError() != ERROR_SUCCESS)
{
CloseHandle(Mutex);
return 0;
}
Change DBClip to what ever you want, as long as it's likely to be unique.
I put this at the start of WinMain, so the return terminates the program.
Derek
Two points:
The CloseHandle(Mutex); in my mine isn't really necessary, as Windows will delete the mutex when the app terminates.
Some people say check for ERROR_SUCCESS as I do, rather than ERROR_ALREADY_EXISTS, for example: http://tinyurl.com/23fsr
Derek
Your application will register its own window class, right? Then use something like this in WinMain:
HWND existingAppWindow = FindWindow("MyClassName", 0);
if (IsWindow(existingAppWindow)) {
if (IsIconic(existingAppWindow)) {
ShowWindow(existingAppWindow, SW_RESTORE);
}
BringWindowToTop(existingAppWindow);
SetForegroundWindow(existingAppWindow);
}
qWake
There are those that say use don't use FindWindow: http://tinyurl.com/2rz4o
Derek
Good point, I never thought of the race condition. I shall change my evil ways (and a couple of program lines).
qWake
So, does anyone know if it's possible to use a shared memory segment in Dev-C++, as the tutorial tries to do?
Yes. Just use GlobalAlloc() for a structure of some sort.
Kip
It seems that it's possible to use shared sections with gcc:
int foo __attribute__((section ("shared"), shared)) = 0;
quoted from
http://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/Variable-Attributes.html#Variable%20Attributes
Liviu
That's insanely usefull and exactly what I was looking for. I didn't even know that site existed. Thanks to everyone for the responses!
so cool!