Sorry, I'm not all that good at windows programming here.
But if I am programming for windows 2000/XP the traditional way, how would I prevent the user from initiating multiple instances of my application?
Sorry, I'm not all that good at windows programming here.
But if I am programming for windows 2000/XP the traditional way, how would I prevent the user from initiating multiple instances of my application?
try
#include <windows.h>
HWND FindWindow(
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);
at startup of your programs try this
if it returns a window then the app is running close the program.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
Try this at the begining of your WinMain function
Code:hMapping = CreateFileMapping((HANDLE)0xffffffff, NULL, PAGE_READONLY, 0, 32, szAppName); if (hMapping) // Check to see if app is already running, if so exit { if (GetLastError() == ERROR_ALREADY_EXISTS) { sprintf(sBuffer,"%s is already running.",szAppName); MessageBox(NULL, sBuffer, "Error", MB_ICONERROR | MB_OK); ExitProcess(1); } } else { MessageBox(NULL, "Error creating file mapping.", "Error",MB_ICONERROR| MB_OK); ExitProcess(1); }
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
the normal way of doing this is with mutexes.
go to msdn and do a search for CreateMutex()
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
There's quite a good article here -
http://www.codeproject.com/cpp/avoidmultinstance.asp
about avoiding multiple instances.
Simplified way to do it, add the following to the begining of your WinMain.
HANDLE hMutex;
hMutex = CreateMutex(NULL, FALSE, "myAppMutex");
if (hMutex) {
if (GetLastError() == ERROR_ALREADY_EXISTS) {
return 0;
}
}
This is just a REAL SIMPLE way of not starting the app if its already running.
Thanks for all the help. I went with a FindWindow solution but set my class name to some really long and unique name. I know that this was discredited as having race conditions, but in all fairness, i think the likelihood of such conditions ever arising to be slim to nill. And I don't want to spend that much time on such a simple task.
Ofcourse, all help was appreciated.