Limiting a dialog-based application to a single instance
For some reason, you can't use the PreCreateWindow of a dialog box, so you can't change the class name to your desired class name, in order to use it with conjunction with the AfxRegisterClass function in the InitInstance of your derived CWinApp class.
What you have to do is a little trickey:
After checking with SPY++, I found out that the class name of the dialog box application is "#32770". Now, in order to try to find previous instances, you use FindWindow( LPCTSTR lpszClassName, LPCTSTR lpszWindowName ). In the MDI/SDI application example, the lpszWindowName is set to NULL, because you have the class name and that is enough. However, in a dialog-based application, you must also use the Window title. The title is set up in the InitDialog function with the command SetWindowText. When you write your FirstInstance function in the CWinApp class (like in the known examples), you write something like this: FindWindow ("#32770", "My application "); Yes, I wrote "My application " with a space in the end on purpose, since this is the name that you write in the SetWindowText in your InitDialog function.
In order to undrstand this better, just download the example. It was tested successfully with MSVC 6.0, Win 98.
If it does to you any problems with other MSVC versions/operating systems, please let me know.
Date Last Updated: February 3, 1999

Comments
Using psapi
Posted by Legacy on 08/30/2001 12:00amOriginally posted by: Alex Kulish
To be sure looking for instances I test running module file name. It works but needs psapi.dll. I combined it in my own dll and use smthg like CompareWindows(hwnd1, hwnd2) == true if both windows belong to the same application. Mail me if you need a source
ReplyFindWindow( ) can be dangerous
Posted by Legacy on 07/04/2001 12:00amOriginally posted by: Jeff
Dangerous. FindWindow may or may not return. See http://www.pgh.net/~newcomer/nomultiples.htm
Reply
there is a very much simpler way
Posted by Legacy on 08/18/1999 12:00amOriginally posted by: Stefan Soltsien
ReplyLook at here!
Posted by Legacy on 08/18/1999 12:00amOriginally posted by: Haining Geng
I had the problem before.
Go to MSJ:
http://www.microsoft.com/msj/backissuestop.htm
and look at October 1997 issue, C++ Q & A, the answer is clear.
ReplyOne_line_of_code
Posted by Legacy on 04/10/1999 12:00amOriginally posted by: David_Shepherd
Pardon_the_underscores,_but_for_some_reason,_I_cannot_enter_spaces!!_:-(
Anyway,_consider_the_following_(Since_this_IS_an_MFC_forum):
if_(_m_hPrevInstance_!=NULL_)
{
____//That's_it!!!
}
-David
-
Reply
ReplyThe m_hPrevInstance parameter is a holdover from Win16. It is no longer in use, ... Mutex is a safe way to do it. m_hPrevInstance is no longer valid. ...
Posted by jauming on 11/06/2007 09:23pmThe best way to limit a dialog app to one instance!
Posted by Legacy on 04/09/1999 12:00amOriginally posted by: High Plains Software
ReplyEven better, activate the current instance...
Posted by Legacy on 03/03/1999 12:00amOriginally posted by: Jon Rizzo
ReplyAnother way...
Posted by Legacy on 02/28/1999 12:00amOriginally posted by: ljp
In BOOL CDialogApp::InitInstance()
HWND hWnd = ::FindWindow(NULL,"title Of your App");
Replyif (hWnd)
{
return FALSE;
}
Not allowing multiple instances of a dialog app...
Posted by Legacy on 02/10/1999 12:00amOriginally posted by: Chris Conn
I'd have to agree with Gunner on this one. The mutex method allows you to change the title of the application's main window without affecting whether an instance is running. I've actually done this using a primarily CPropertySheet based application, which can have up to four different property sheets displayed depending on command line switches. In the future, I also expect to allow only "n" number of instances of the application running.
ReplyA simpler way would be to use a global Mutex.
Posted by Legacy on 02/03/1999 12:00amOriginally posted by: Gunnar Roth
It works with every kind of application.
Example:
In InitInstance you add this code
CreateMutex(NULL,TRUE,_T("My Super Program Mutex")); // mutex will be automatically deleted when process ends.
ReplyBOOL bAlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS);
if(bAlreadyRunning)
return FALSE;