• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 934
  • Last Modified:

OLE Control fails in a dialog in a DLL.

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
chacko
Asked:
chacko
  • 2
  • 2
1 Solution
 
t004024Commented:
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
 
IgorGrebnevCommented:
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
 
chackoAuthor Commented:
Dear Igor,

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

thanks,
chacko
0
 
IgorGrebnevCommented:
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
 
t004024Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now