Limiting a dialog-based application to a single instance | CodeGuru

Limiting a dialog-based application to a single instance

Running only one instance of an SDI/MDI application is easy. You only have to follow the examples, but doing this in a dialog-based application is something else. 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 2, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Running only one instance of an SDI/MDI application is easy. You only
have to follow the examples, but doing this in a dialog-based
application is something else.

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.

Download demo project – 29 KB

Date Last Updated: February 3, 1999

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.