Solved

Single Instance MFC Dialog APP

Posted on 1998-09-03
6
508 Views
Last Modified: 2013-11-25
Hi

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.

Thanks
-Regards
-craig.
0
Comment
Question by:cmain
6 Comments
 

Expert Comment

by:nz75c2
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.
0
 
LVL 3

Expert Comment

by:Norbert
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.
Solution:
1 Use a flag inside of your App class that shows you that the dialog is already there.
or
2 move the creation of the dialog to an other place that is called only once (for example InitApplication)

I hope that helps
Norbert
0
 
LVL 1

Author Comment

by:cmain
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.

Thanks
Craig.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:cmain
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
-craig.
0
 

Accepted Solution

by:
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.

0
 
LVL 1

Author Comment

by:cmain
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.

Regards
-craig.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

805 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