Single Instance MFC Dialog APP

Posted on 1998-09-03
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.

Question by:cmain
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1321652
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.

Expert Comment

ID: 1321653
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

Author Comment

ID: 1321654
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.

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Author Comment

ID: 1321655
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

Accepted Solution

smartin103097 earned 200 total points
ID: 1321656
When the OLE object registers, via the ::CoRegisterClassObject function call it must specify REGCLS_MULTIPLEUSE. I have hard coded this in the past (never used MFC to handle COM), so I'm not sure where to place this code if you are using the MFC project with Automation support, but browsing MFC for this function call should give you what you want.


Author Comment

ID: 1321657
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.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question