We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Single Instance MFC Dialog APP

Medium Priority
Last Modified: 2013-11-25

I have created an MFC Dialog application with automation support. I have added functionality to the automation object which manipulates the dialog.

I can now use VB / Delphi or whatever to create the ole object, unfortunately the MFC creates a new dialog each time I create a new OLE object. I think the application is being re-executed each time I request a new OLE object.
I want the server to hand out multiple OLE objects to multiple clients, not re-execute each time.

How do I make each instance of the CCmdTarget (OLE automation object) talk to one dialog (the same one).
I am basically writing a type of event log window that will display messages from clients that call it. The clients will be separate processes. The first client will be the one that creates the dialog for the first time.

I have done this before in delphi, so I know it can be done, but I am struggling to get the VC++/MFC version working.

Watch Question

In your client application you should check if the automation server is already active before instanciating the Automation object. You can achieve this by using the GetActiveObject call. If this succeeds, it will return a reference to the active object, if it fails this means the server is not running and then you can call the CreatInstance call to start it and to create a new instance.

nz 75c2 may be will right but because you can not be sure that all client creators will use First the GetActiveObject call try this:
As I know InitInstance is called for each Connection so if you create the Dialog inside of InitInstance you will get a new dialog.
1 Use a flag inside of your App class that shows you that the dialog is already there.
2 move the creation of the dialog to an other place that is called only once (for example InitApplication)

I hope that helps


I will re-evaluate your answer if I cannot get this correct,
I have done the dialog creation in InitApplication, and the same thing happens.

The application is physically being run more than once.
How to I make the OLE object single instance. I managed to get it working like this before, but not in VC.

I am happy to have each client collect a new instance of the OLE automation object, but they must get them from the same application. i.e. One instance of the class factory. There are currently two completely separate applications running.

I am convinced that CWinApp has two instances. There must be a way of having one instance of CWinApp, with multiple clients using it's OLE automation objects.

Sorry to reject your answer, but I really need to be sure that the application is only running once. I am convinced that a single application can hand out multiple automation objects to several connected clients.

I have a suspicion that you have to change the instancing mechanism in the class factory to single instance, no appartments, but I am not sure.



I have solved the problem.
I will leave this here for a while, and then remove the question.
(Give you guys a chance to read the answer).

Here it is.
There is a macro called IMPLEMENT_OLECREATE2
The macro is located in stdafx.h
The macro passes a parameter (true, or false) to the IClassFactory Implementation for the MFC project. The parameter controls how the instances are created. Either multiple or single. Seems my guess at class factory registration was somewhat correct.

For futher information I refer you to KB article Q141154
"Using Single/Multiple Instances of an OLE Object in MFC"

Thanks anyway for your prompt assistance.
Kind Regards
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


The reason I have given you an average answer is that I cannot delete the question once someone has already answered,
and as you will see above, I solved the problem myself before you answered, otherwise I would have given you an excellent rating.

Thanks for your assistance, I do appreciate the time you took to answer the question.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.