Click to See Complete Forum and Search --> : How can I limit my application to one instance?
Ness
June 4th, 2004, 11:11 AM
[Andreas]: The following question was asked in regard to the following FAQ: Application: How can I limit my application to one instance? (http://www.codeguru.com/forum/showthread.php?s=&threadid=231178)...
How would you go about doing it with Win32?
Andreas Masur
June 6th, 2004, 04:30 PM
Basically the same way...the code shown in the FAQ for protecting multiple instances of your application itself uses already a standard Win32 API function...the only difference would be the place where you do it...in an application different than a MFC one it would be 'main()'/'WinMain()' instead...
hephaestusp
October 29th, 2004, 08:51 AM
bool isRunning = FALSE ;
isRunning = FindWindow( szClassName , hName);
if ( isRunning )
{
return 0;
}
Where "szClassName" is a char array, string, which contains the name of your main window. The easier way I have found to do this. You might even try to replace the "return 0" line with a message window notifing you for what you want.
Bond
October 29th, 2004, 10:36 AM
Another:
BOOL IsAlreadyRunning()
{
HANDLE hMutex = CreateMutex(NULL, TRUE, g_szAppName);
BOOL bAlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS);
ReleaseMutex(hMutex);
return bAlreadyRunning;
}
Andreas Masur
October 30th, 2004, 11:39 AM
bool isRunning = FALSE ;
isRunning = FindWindow( szClassName , hName);
if ( isRunning )
{
return 0;
}
Where "szClassName" is a char array, string, which contains the name of your main window. The easier way I have found to do this. You might even try to replace the "return 0" line with a message window notifing you for what you want.
Well....the above method has some drawbacks and thus is not reliable...
Andreas Masur
October 30th, 2004, 11:40 AM
Another:
BOOL IsAlreadyRunning()
{
HANDLE hMutex = CreateMutex(NULL, TRUE, g_szAppName);
BOOL bAlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS);
ReleaseMutex(hMutex);
return bAlreadyRunning;
}
Which is the way shown in the FAQ...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.