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. Return Interface Pointer

Return Interface Pointer

Scheduled Pinned Locked Moved COM
questionc++comgraphicsgame-dev
8 Posts 4 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.
  • D Offline
    D Offline
    Daniel Strigl
    wrote on last edited by
    #1

    Hi! I am very new to COM and ATL and try to implement something like this: I have a simple COM server with the following interface:

    [
    object, ...
    ]
    interface ITest : IUnknown
    {
    [id(1), helpstring("Methode GetInterface")] HRESULT GetInterface([in] int n, [out] ITest2** pITest2);
    };

    The interface method GetInterface should return a new pointer to the interface ITest2. This function should not work like QueryInterface, the interface ITest2 is not in the same coclass as the ITest, it's in another coclass. This function should work like the following DirectX (DirectDraw) function CreateSurface:

    LPDIRECTDRAW lpDD = NULL;
    LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;
    ...
    HRESULT hError = lpDD->CreateSurface(&DDSurfaceDesc, &lpDDSPrimary, NULL);
    ...

    How can I implement the GetInterface function? The ITest2 interface (and the coclass of the interface) should not be creatable with the CoCreateInstance, it should be only created by the GetInterface method! Daniel ;) --------------------------- Never change a running system!

    V S 2 Replies Last reply
    0
    • D Daniel Strigl

      Hi! I am very new to COM and ATL and try to implement something like this: I have a simple COM server with the following interface:

      [
      object, ...
      ]
      interface ITest : IUnknown
      {
      [id(1), helpstring("Methode GetInterface")] HRESULT GetInterface([in] int n, [out] ITest2** pITest2);
      };

      The interface method GetInterface should return a new pointer to the interface ITest2. This function should not work like QueryInterface, the interface ITest2 is not in the same coclass as the ITest, it's in another coclass. This function should work like the following DirectX (DirectDraw) function CreateSurface:

      LPDIRECTDRAW lpDD = NULL;
      LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;
      ...
      HRESULT hError = lpDD->CreateSurface(&DDSurfaceDesc, &lpDDSPrimary, NULL);
      ...

      How can I implement the GetInterface function? The ITest2 interface (and the coclass of the interface) should not be creatable with the CoCreateInstance, it should be only created by the GetInterface method! Daniel ;) --------------------------- Never change a running system!

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      One solution is containment. Another solution is aggregation given that you want the client to gain direct access to the inner COM object. Kuphryn

      D 1 Reply Last reply
      0
      • D Daniel Strigl

        Hi! I am very new to COM and ATL and try to implement something like this: I have a simple COM server with the following interface:

        [
        object, ...
        ]
        interface ITest : IUnknown
        {
        [id(1), helpstring("Methode GetInterface")] HRESULT GetInterface([in] int n, [out] ITest2** pITest2);
        };

        The interface method GetInterface should return a new pointer to the interface ITest2. This function should not work like QueryInterface, the interface ITest2 is not in the same coclass as the ITest, it's in another coclass. This function should work like the following DirectX (DirectDraw) function CreateSurface:

        LPDIRECTDRAW lpDD = NULL;
        LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;
        ...
        HRESULT hError = lpDD->CreateSurface(&DDSurfaceDesc, &lpDDSPrimary, NULL);
        ...

        How can I implement the GetInterface function? The ITest2 interface (and the coclass of the interface) should not be creatable with the CoCreateInstance, it should be only created by the GetInterface method! Daniel ;) --------------------------- Never change a running system!

        S Offline
        S Offline
        Stefan Pedersen
        wrote on last edited by
        #3

        If I didn't misunderstand you static HRESULT CComObject::CreateInstance(CComObject** pp) should be the solution to your problem.

        D 1 Reply Last reply
        0
        • V valikac

          One solution is containment. Another solution is aggregation given that you want the client to gain direct access to the inner COM object. Kuphryn

          D Offline
          D Offline
          Daniel Strigl
          wrote on last edited by
          #4

          Can you give my a sample application or a link to a tutorial? Thanks! Daniel ;) --------------------------- Never change a running system!

          1 Reply Last reply
          0
          • S Stefan Pedersen

            If I didn't misunderstand you static HRESULT CComObject::CreateInstance(CComObject** pp) should be the solution to your problem.

            D Offline
            D Offline
            Daniel Strigl
            wrote on last edited by
            #5

            Any sample or link to a tutorial? Thanks! Daniel ;) --------------------------- Never change a running system!

            L 2 Replies Last reply
            0
            • D Daniel Strigl

              Any sample or link to a tutorial? Thanks! Daniel ;) --------------------------- Never change a running system!

              L Offline
              L Offline
              Lim Bio Liong
              wrote on last edited by
              #6

              Hello Daniel, I created a sample ATL project with two simple coclass's (Test and Test2). One of them, Test2 is specifically noncreatable. This noncreatble feature can be set easily by including the "noncreatable" attribute for the coclass statement in the IDL file : [ uuid(A6C64958-F337-4981-8F31-F35AEA02DD98), helpstring("Test2 Class"), noncreatable ] coclass Test2 { [default] interface ITest2; [default, source] dispinterface _ITest2Events; }; This attribute is very useful when your client apps are Visual Basic based. In VB, if a coclass is noncreatable, then when you attempt to create a new instance of it via the "New" keyword, e.g. : Dim Test2Obj As Test2 ... ... ... Set Test2Obj = New Test2 You will get an error message that reads : "Invalid use of New keyword". However, the "noncreatable" attribute is not sufficient for VC++ clients. In a VC++ client app, if you have the following statement : ITest2Ptr spTest2Ptr = NULL; spTest2Ptr.CreateInstance(__uuidof(Test2)); Surprise, surprise, you will still succeed and obtain an ITest2 smart pointer. In order to ensure that the above CreateInstance() function call will fail, you will need to comment out the Test2 Object Entry in the ATL Object Map of your ATL project : BEGIN_OBJECT_MAP(ObjectMap) OBJECT_ENTRY(CLSID_Test, CTest) //OBJECT_ENTRY(CLSID_Test2, CTest2) <-- comment out this line. END_OBJECT_MAP() This will ensure that when your ATL DLL is asked to create an instance of an object of coclass Test2 (this happens in the global DllGetClassObject() function), a failure code will be returned. Note that the good thing about the "noncreatable" attribute and the commenting out of the OBJECT_ENTRY statement will not prevent you from internally creating the CTest2 object. This is, of course, required when the GetInterface() method is called. My implementation of GetInterface() is as follows : STDMETHODIMP CTest::GetInterface(int n, ITest2 **pITest2) { // TODO: Add your implementation code here CComObject *pTest2New = NULL; CComObject::CreateInstance(&pTest2New); if (pTest2New) { pTest2New -> QueryInterface (IID_ITest2, (void**)pITest2); } return S_OK; } I will email you the sample source codes plus VB and a VC++ client apps. Hope the aboe info will help. Best Regards, Bio.

              1 Reply Last reply
              0
              • D Daniel Strigl

                Any sample or link to a tutorial? Thanks! Daniel ;) --------------------------- Never change a running system!

                L Offline
                L Offline
                Lim Bio Liong
                wrote on last edited by
                #7

                Hello Daniel, Please email me your email address so that I can send to you some sample codes. Best Regards, Bio.

                D 1 Reply Last reply
                0
                • L Lim Bio Liong

                  Hello Daniel, Please email me your email address so that I can send to you some sample codes. Best Regards, Bio.

                  D Offline
                  D Offline
                  Daniel Strigl
                  wrote on last edited by
                  #8

                  Thanks! It works fine! Cheers, Daniel. -- FIND A JOB YOU LOVE, AND YOU'LL NEVER HAVE TO WORK A DAY OF YOUR LIFE. ;)

                  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