Click to See Complete Forum and Search --> : Single Instance of an Application Running


April 28th, 1999, 01:43 AM
I have a C++ application which must have only one instance of it running at any time.
If the application (.exe) is invoked when one instance is already running, then
it must not start up the application.

Jason Teagle
April 28th, 1999, 02:04 AM
In your InitInstance() method, check the m_hPrevInstance member of your app. It will be NULL if this is the first instance, or non-NULL if there is an instance already running. You should return FALSE from InitInstance() to terminate the app before it gets going. You could do something like this:

---
BOOL CMyApp::InitInstance()
{
if (m_hPrevInstance != NULL)
{
// Here you might want to find the existing instance. You would do this by
// calling the CWnd::FindWindow() function with the name of your app's
// title bar (and if you can access it, its main window's class name), and
// then activating that window with ShowWindow().

return FALSE ; // Terminate this instance immediately.
}

// The usual garbage...

return TRUE ;
}


---

Does this help?

Roger Osborn
April 28th, 1999, 02:12 AM
I generally enforce this by using a mutex.

const LPCTSTR MutexName=_T("amutexname");

m_hApplicationMutex=CreateMutex(NULL,FALSE,MutexName);
if(NULL!=m_hApplicationMutex)
{
// Connected up to the Mutex OK.
// Can we get ownership of it ok?
DWORD dwResult=WaitForSingleObject(m_hApplicationMutex,0);
if(WAIT_TIMEOUT==dwResult)
{
// Couldn't get the Mutex, Another instance is running...

You should be careful to use a statistically unique name for the mutex (e.g. one containing an ascii form of a CLSID).
You should release the mutex at the end of the program, but the O.S. will actually do that for you anyway.


You can also do it by using FindWindow to look for another window with the same title, but this fails if the window title can change within the same application. Also there's probably a unlocked out gap before the window is created at startup.

I'd be interested if anyone knows better ways.


Cheers,
Roger

Roger Osborn
April 28th, 1999, 02:17 AM
I thought that m_hPrevInstance was always NULL in Win32? But I could be wrong...

Roger

Jason Teagle
April 28th, 1999, 02:46 AM
Oops, you are correct. I'm a die-hard 16-bit programmer still, so I was thinking from that point of view. It's a bit stupid for Windows to no longer use that parameter, that's what the bloody thing is for.

My apologies to the original author.

Roger Osborn
April 28th, 1999, 02:55 AM
: It's a a bit stupid for Windows to no longer use that parameter

Absolutely, you'd think there'd be some way it could be connected up under the WIN32 regime. Maybe it's something to do with security, but can't immediately think what.

Craig Pittaway
April 28th, 1999, 03:24 AM
Another way to enforce a single instance is to use the following method (more details can be found in the Codeguru site).

In the app.cpp place the following code :

#pragma data_seg("Shared")
bool g_bIsLoaded=false;
#pragma data_seg()
#pragma comment(linker,"/section:Shared,rws")

Then in the InitInstance function :

if (g_bIsLoaded)
{
return NULL;
}
else
g_bIsLoaded = true;

Hope that this helps.