Probably dumb question: passing IUnknown** to VB/MCPP/C#
-
Hi there, while taking some first steps in programming COM interfaces, I'm stuck at some point. Here's my question: I'm trying to add COM functionality to an existing application. Tnis application contains already a MFC Dispatch interface. My new COM interface tried to prevent in the first step using IDispatch, to have some more possibilities defining interfaces and datatypes. What I did is following: 1.) Extended the existing Dispatch interface with a function, which returns an IUnknown**, which exposes the interface to an internally created and maintained object. 2.) Created a custom interface, derived from IUnknown, which can' t be directly created and is basically used as proxy object. Extended dispatch interface, returns pointer to existing instace of my object:
[id(100))] SCODE GetObjRef_ppUnk([out] IUnknown**);
Interface definition to be returned:interface IMyInterface : IUnknown { import "unknwn.idl"; HRESULT my_proc([out] myparam* p); }
Working CPP code calling the OCX Control:#import "libid: ... my GUID ... " LPUNKNOWN pUnk = NULL; BOOL bOK = FALSE; MyLib::IMyInterfacePtr pMyIF; int iRet = 0; MyLib::myparam myParam = {0}; bOK = m_myCTL.GetObjRef_ppUnk(&pUnk); pUnk->QueryInterface(IID_IFMYIF, (LPVOID*)&pMyIF); if(pMyIF) { pMyIF->my_proc(&myParam); }
The object instance to be exposed via the interface is completely handled internally in the OCX control. Therefore, my custom interface is declared as noncreatable. While this is working as expected in CPP , I don't get it running with VB, MCPP, C# , which I'm also not very familiar with at the moment. I tried somthing like this (Managed C++):System::Object *pUnk = NULL; Interop::MyLib::IMyIF * pMyIF = NULL; Interop::MyLib::MYPARAM myParam = {0}; this->axMyCTL->GetObjRef_ppUnk(&pUnk); if(pMyIF) { pMyIF->my_proc(&my_param); }
Similar code also doesn't works in VB/C#. I suppose, it has to do with the managed thing. When calling GetObjRef I get an "An unhandled exception of type 'System.ExecutionEngineException' occurred in mscorlib.dll" . I there a legal way to accomplish this, or do I have to use the IDispatch interaces ? I searched a lot -
Hi there, while taking some first steps in programming COM interfaces, I'm stuck at some point. Here's my question: I'm trying to add COM functionality to an existing application. Tnis application contains already a MFC Dispatch interface. My new COM interface tried to prevent in the first step using IDispatch, to have some more possibilities defining interfaces and datatypes. What I did is following: 1.) Extended the existing Dispatch interface with a function, which returns an IUnknown**, which exposes the interface to an internally created and maintained object. 2.) Created a custom interface, derived from IUnknown, which can' t be directly created and is basically used as proxy object. Extended dispatch interface, returns pointer to existing instace of my object:
[id(100))] SCODE GetObjRef_ppUnk([out] IUnknown**);
Interface definition to be returned:interface IMyInterface : IUnknown { import "unknwn.idl"; HRESULT my_proc([out] myparam* p); }
Working CPP code calling the OCX Control:#import "libid: ... my GUID ... " LPUNKNOWN pUnk = NULL; BOOL bOK = FALSE; MyLib::IMyInterfacePtr pMyIF; int iRet = 0; MyLib::myparam myParam = {0}; bOK = m_myCTL.GetObjRef_ppUnk(&pUnk); pUnk->QueryInterface(IID_IFMYIF, (LPVOID*)&pMyIF); if(pMyIF) { pMyIF->my_proc(&myParam); }
The object instance to be exposed via the interface is completely handled internally in the OCX control. Therefore, my custom interface is declared as noncreatable. While this is working as expected in CPP , I don't get it running with VB, MCPP, C# , which I'm also not very familiar with at the moment. I tried somthing like this (Managed C++):System::Object *pUnk = NULL; Interop::MyLib::IMyIF * pMyIF = NULL; Interop::MyLib::MYPARAM myParam = {0}; this->axMyCTL->GetObjRef_ppUnk(&pUnk); if(pMyIF) { pMyIF->my_proc(&my_param); }
Similar code also doesn't works in VB/C#. I suppose, it has to do with the managed thing. When calling GetObjRef I get an "An unhandled exception of type 'System.ExecutionEngineException' occurred in mscorlib.dll" . I there a legal way to accomplish this, or do I have to use the IDispatch interaces ? I searched a lotProbably a dumb answer as well, since I'm not an expert in COM... But all COM made in VB that I've seen has IDispatch interface, so I guess that VB might not know how to deal with the IUnknown. What I don't understand, if you have IDispatch already, why do you want to use IUnknown ? Found IDispatch is much easier to use, and though I'm not sure how VB deal with IUnknown, it's certainly can deal with IDispatch.:-D Good luck... :-D
-
Probably a dumb answer as well, since I'm not an expert in COM... But all COM made in VB that I've seen has IDispatch interface, so I guess that VB might not know how to deal with the IUnknown. What I don't understand, if you have IDispatch already, why do you want to use IUnknown ? Found IDispatch is much easier to use, and though I'm not sure how VB deal with IUnknown, it's certainly can deal with IDispatch.:-D Good luck... :-D
Hi hanofee, hanofee wrote: What I don't understand, if you have IDispatch already, why do you want to use IUnknown ? Found IDispatch is much easier to use, and though I'm not sure how VB deal with IUnknown, it's certainly can deal with IDispatch. basically it's because the object I want to expose is allocated and maintained in the main ActiveX Control itself. So I need a way to expose the interface of this instance, and at the current point, it's the only working way. Maybe someone has some example code showing how to cast an IMyInterface** of an object inside an ActiveX control exposed via IDispatch to an object in VB/C# or whatever... I still didn't got it working. Maybe also something is wrong on the server side... Florian
-
Hi hanofee, hanofee wrote: What I don't understand, if you have IDispatch already, why do you want to use IUnknown ? Found IDispatch is much easier to use, and though I'm not sure how VB deal with IUnknown, it's certainly can deal with IDispatch. basically it's because the object I want to expose is allocated and maintained in the main ActiveX Control itself. So I need a way to expose the interface of this instance, and at the current point, it's the only working way. Maybe someone has some example code showing how to cast an IMyInterface** of an object inside an ActiveX control exposed via IDispatch to an object in VB/C# or whatever... I still didn't got it working. Maybe also something is wrong on the server side... Florian
Meanwhile I switched to ATL, which seems to solve some of the problems. Main problem was obviously in the server, that I casted the reference to be returned via a static_cast to the IUnkown/IDispatch Base interface. This seemed to cause some hassle, when using other languages than C++. I now return a reference, which is initialized via the QueryInterface process for the IDispatch interface. No everythings working fine.