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. C / C++ / MFC
  4. How to return COM object?

How to return COM object?

Scheduled Pinned Locked Moved C / C++ / MFC
comhelptutorialquestion
2 Posts 1 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.
  • R Offline
    R Offline
    RYU
    wrote on last edited by
    #1

    Hi, I tried to return an interface but it gave me this error:

    Unable to cast COM object of type 'MyTestLib.MainTestClass' to interface type 'MyTestLib.IMainTest '. This operation failed because the QueryInterface call on the COM component for the interface with IID '{519A413A-2792-4021-847F-B7C205FFE057}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    Any idea why? Here is my IDL:

    import "oaidl.idl";
    import "ocidl.idl";
    
    [ object, uuid(EC23249E-2891-4f06-9D6E-77895E74448F) ]
    interface IAdd : IUnknown
    {
        HRESULT     SetFirstNumber(long nX1);
        HRESULT     SetSecondNumber(long nX2);
        HRESULT     DoTheAddition([out,retval] long *pBuffer);
    };
    
    [ object, uuid(519A413A-2792-4021-847F-B7C205FFE057) ]
    interface IMainTest : IUnknown
    {
        HRESULT     GetAddObject([out,retval] IAdd **ppObj);
    };
    
    [ uuid(AA484B10-6099-4e37-AEE3-8B4ADFBE2815) ]
    library MyTestLib
    {
        importlib("stdole32.tlb");
    
        [ uuid(82B76786-E89B-4e42-8C50-B7916FEC1ADC) ]
        coclass AddClass
        {
            [default] interface IAdd;
        }
    
        [ uuid(E76E5E58-F9D9-4570-AEC5-79A72E8E8743) ]
        coclass MainTestClass
        {
            [default] interface IMainTest;
        }
    };
    

    Thanks for any help :)

    R 1 Reply Last reply
    0
    • R RYU

      Hi, I tried to return an interface but it gave me this error:

      Unable to cast COM object of type 'MyTestLib.MainTestClass' to interface type 'MyTestLib.IMainTest '. This operation failed because the QueryInterface call on the COM component for the interface with IID '{519A413A-2792-4021-847F-B7C205FFE057}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

      Any idea why? Here is my IDL:

      import "oaidl.idl";
      import "ocidl.idl";
      
      [ object, uuid(EC23249E-2891-4f06-9D6E-77895E74448F) ]
      interface IAdd : IUnknown
      {
          HRESULT     SetFirstNumber(long nX1);
          HRESULT     SetSecondNumber(long nX2);
          HRESULT     DoTheAddition([out,retval] long *pBuffer);
      };
      
      [ object, uuid(519A413A-2792-4021-847F-B7C205FFE057) ]
      interface IMainTest : IUnknown
      {
          HRESULT     GetAddObject([out,retval] IAdd **ppObj);
      };
      
      [ uuid(AA484B10-6099-4e37-AEE3-8B4ADFBE2815) ]
      library MyTestLib
      {
          importlib("stdole32.tlb");
      
          [ uuid(82B76786-E89B-4e42-8C50-B7916FEC1ADC) ]
          coclass AddClass
          {
              [default] interface IAdd;
          }
      
          [ uuid(E76E5E58-F9D9-4570-AEC5-79A72E8E8743) ]
          coclass MainTestClass
          {
              [default] interface IMainTest;
          }
      };
      

      Thanks for any help :)

      R Offline
      R Offline
      RYU
      wrote on last edited by
      #2

      I found the problem. Apparently I am trying to be really smart about this by creating only 1 Factory and put a check in CreateInstance() to determine whether it is AddClass or MainTestClass. I was wrong because the IID check should be in DllGetClassObject(). I have created 2 different factories for these classes and it works fine. I can now return the AddClass by using GetAddObject() function. However, when I tried to use the function in AddClass (eg. SetFirstNumber()), it gave me this error:

      The runtime has encountered a fatal error. The address of the error was at 0x7f628678, on thread 0x105c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

      Here is my code:

      HRESULT __stdcall CMainTestClass::GetSamTest(IAdd **ppRetVal)
      {
          CAddClass *pAddClass = new CAddClass();
          *ppRetVal = pAddClass;
          return S_OK;
      }
      

      Thanks again for any help :)

      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