Solved

OLE Control fails in a dialog in a DLL.

Posted on 1998-06-06
5
824 Views
Last Modified: 2013-11-25
I have a DLL. There is a dialog in this DLL which I call from the application. The dialog has a MSComm OLE control. Everything works fine if the dialog resources is in app. If it is in the DLL, CoCreateInstance of the control fails which makes the dialog creation failing. Can OLE control be invoked in a dialog in DLL?
0
Comment
Question by:chacko
  • 2
  • 2
5 Comments
 

Expert Comment

by:t004024
ID: 1316123
if it helps, I had to do something similar, create an ole control in a DLL.

The DLL uses the context state of the application, so if U have not initialized Ole in the application, the DLL call fails. If U check the help, he says, U need to call AfxEnableControlContainer() to enable activex control containment. This can be done either in the application or the DLL. If U are initializing and enabling control containment in  the DLL, then U need to call AFX_MANAGE_STATE(AfxGetStaticModuleState()) in every exported function, so that the DLL, uses U'r DLL state and not the state of the application.

My problem was, even if though the CoCreateInstance created the control correctly, any other call, like setting a property or invoking a method failed, this was because, when I created an instance I was not providing the control with a control site!!! But I was trying to create the control as any other automation object and that is not possible I think.
0
 
LVL 1

Accepted Solution

by:
IgorGrebnev earned 50 total points
ID: 1316124
Dear chacko

You need to call AfxOleInit( ) in the InitInstance of your DLL. Calling of AfxEnableControlContainer() in DLL is not enough, I found it on experience.
You need to do this of you are using MFC Regular DLL ( I guess you use it ).
Sincerely yours,
Igor.

0
 
LVL 1

Author Comment

by:chacko
ID: 1316125
Dear Igor,

I tried AfxOleInit() in the calling application InitInstance not in DLL InitInstance. Could u please clarify?

thanks,
chacko
0
 
LVL 1

Expert Comment

by:IgorGrebnev
ID: 1316126
Dear chacko

You need to call AfxOleInit() for regular ( not extension ) DLLs.
I do not know exactly why.
If you create extension DLL it use the same enviromen block of MFC as applicatoin, so you do not need to call AFX_MANAGE_STATE(AfxGetStaticModuleState()) in every exported and fo not need specific initialisation of MFC. In regular DLL with shared MFC you need it.
Just add the call to AfxOleInit() and AfxEnableControlContainer() and check result.
Yours,
Igor.
      function
0
 

Expert Comment

by:t004024
ID: 1316127
If U have Ole initialized in the application, the the AfxOleInit() function in the DLL, will  give U an assertion, because, Ole is already initialized. If U have the AfxOleInitialize and AfxEnableControlContainer in U'r application, then U'r control creation in the DLL, should work fine, without any module specific initialization and so without AFX_MANAGE_STATE.

If U call AfxEnableControlContainer(), what this really does is initialize the afxOccManager of the current state(U can look at the occmgr.cpp file in the mfc src). So, if AfxEnableControlContainer is called in the InitInstance of the DLL, the afxOccManager in the DLL module is initialized and not that in the application instance.

AFX_MANAGE_STATE(AfxGetStaticModuleState()) is required when the state in which U want U'r dll exported function to run is different from that of the application, and that is how it is here. The DLL state should provide for a OccManager, where as the application does not provide for a OccManager.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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.
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…

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

11 Experts available now in Live!

Get 1:1 Help Now