Solved

Single Instance MFC Dialog APP

Posted on 1998-09-03
6
495 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:cmain
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 68
Problem to DocumentFormat 8 67
sum13 challenge 24 77
Thin secure Windows 10 5 47
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

744 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

8 Experts available now in Live!

Get 1:1 Help Now