Solved

Single Instance MFC Dialog APP

Posted on 1998-09-03
6
503 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DB Mail Export to Excel some row wrap to new row 30 108
Example of WebAPI 7 32
sum13 challenge 24 90
Is AWS.RDS considered a part of "Big Data"? 4 94
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now