Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Probably dumb question: passing IUnknown** to VB/MCPP/C#

Probably dumb question: passing IUnknown** to VB/MCPP/C#

Scheduled Pinned Locked Moved COM
c++comquestioncsharphelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Florian Storck
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • F Florian Storck

      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

      H Offline
      H Offline
      hanofee
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • H hanofee

        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

        F Offline
        F Offline
        Florian Storck
        wrote on last edited by
        #3

        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

        F 1 Reply Last reply
        0
        • F Florian Storck

          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

          F Offline
          F Offline
          Florian Storck
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups