Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

COM objects returning other COM objects

Posted on 1998-06-16
4
Medium Priority
?
295 Views
Last Modified: 2013-11-25
Topic: COM objects returning other COM objects

I'm writing a little OCX control using MFC OCX-Control wizard.
The DLL contains two controls - ObjX and ObjY, where the first
one is simple frame control and the second one is "invisible-at-runtime".
To ObjX I’ve added method, which returns instance of ObjY :

afx_msg LPDISPATCH GetNextDisp();

implemented as follows:

{
      CQocxYCtrl* pCtl = new CQocxYCtrl();
      
      pCtl->EnableAutomation();

      LPDISPATCH pdisp = pCtl->GetIDispatch(1); // 1-incrament refcount

      return pdisp;
}

In VB, I’ve placed the ObjX control onto my form and wrote
script which calls GetNextDisp() of ObjX, stores IDispatch object
in variable newObj, and then calls arbitrary method of the newObj.
The VB script was:

Private Sub myButton_Click()
        Dim newObj As Object
        Set newObj = Qocx11.GetNextDisp
        newObj.MyShowMsg "hello"
End Sub

When running this VB app, exception arrives:

"Run-time error '-2147418113': OLE Automation error Catastrophic Failure"

in line newObj.MyShowMsg "hello". The method MyShowMsg is really present
in ObjY, I’ve tested it separately with VB and OLE-control test container.

What I actually wanted to do -> to return pointer to object from another object,
i.e. ObjX returns instances of ObjY, like array object returns it's row objects
sequentially or something similar.
The problem I guess is in the line:

CQocxYCtrl* pCtl = new CQocxYCtrl();

I suspect the CQocxYCtrl should be instantiated using it's class
factory, but I simply can't find it - it is buried under bunch
of MFC helper macros... may be I should use the "hard core" approach -
without MFC wizards, or any other way?

Thanks in Advance,

Aleksandras


0
Comment
Question by:alexas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:dvazquez
ID: 1318022
You don't need to get to the object's class factory.  Just use CoCreateInstance().

Also, you might want to try declaring the function as:

HRESULT CObjX::GetNextDisp(LPDISPATCH *ppDisp);

Do whatever is necessary to get an IID_IDispatch interface pointer into ppDisp and return NOERROR if all goes well.
0
 

Expert Comment

by:dvazquez
ID: 1318023
Rejected?  Explain please...
0
 

Expert Comment

by:t004024
ID: 1318024
A control needs to have a Control site which can handle the events and callbacks of the control. When U create the control as an automation object, it does not have a site associated with it which can handle the callbacks of the control and hence it gives a catastropic error.

I do not know how VB sets the ControlSite, but if U check the code in CreateControl for CWnd, U'll see that it makes the parent Window a container and creates a control site and associates it with the control.

Maybe what U could try is call CreateControl with the parent same as the ObjX, then call GetControlIUnknown, query the IID_Dispatch interface and return it.
0
 

Accepted Solution

by:
unmeshv earned 200 total points
ID: 1318025
Some explaination for dvazquez
1. Instead of using
CQocxYCtrl* pCtl = new CQocxYCtrl()

Try implementing
HRESULT CObjX::GetNextDisp(LPDISPATCH *ppDisp)
{
    CoCreateInstance(CLISID_ObjY, NULL, CLSCTX_LOCAL_SERVER,                                         IID_OBJYINTERFACE, ppDisp);

   return S_OK;
}

In your implementation by returning pdisp u are creating another pointer  to a block which is referred by locally created new object. Now after the funtion returns this block becomes invalid and hence u get an exception.
In the method described above, u are passing pdisp by reference and it directly refers to the object created. So the new object will remain valid even after your call returns in client code.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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…

610 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