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. ATL problem

ATL problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
7 Posts 3 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.
  • N Offline
    N Offline
    novachen
    wrote on last edited by
    #1

    Hi! I get a ATL object by other object's method. like below STDMETHODIMP CBanana::GetApple(IApple** apple) { CComObject* p =NULL; CComObject::CreateInstance(&p); p->QueryInterface(apple); return S_OK; } But i dont know how to turn it back( from interface to real class). the code list below seems doesn't work. STDMETHODIMP CBanana::DoSomethingOnApple(IApple* apple) { CApple* p = dynamic_cast(apple); p->num = 3; } I think dynamic_cast can't be used in this situation. But i really don't know the proper way. Would you please lend me a hand? I have blocked in this problem for almost one week.

    J P 2 Replies Last reply
    0
    • N novachen

      Hi! I get a ATL object by other object's method. like below STDMETHODIMP CBanana::GetApple(IApple** apple) { CComObject* p =NULL; CComObject::CreateInstance(&p); p->QueryInterface(apple); return S_OK; } But i dont know how to turn it back( from interface to real class). the code list below seems doesn't work. STDMETHODIMP CBanana::DoSomethingOnApple(IApple* apple) { CApple* p = dynamic_cast(apple); p->num = 3; } I think dynamic_cast can't be used in this situation. But i really don't know the proper way. Would you please lend me a hand? I have blocked in this problem for almost one week.

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      You don't have to convert it back. If you look at the definition of CComObject<> you'll see:

      template
      class CComObject : public Base

      which means that CComObject<Base> inherits from Base. Everything Base has to offer, so will CComObject<Base>. -- Serial killers don't kill their boyfriends.

      N 1 Reply Last reply
      0
      • N novachen

        Hi! I get a ATL object by other object's method. like below STDMETHODIMP CBanana::GetApple(IApple** apple) { CComObject* p =NULL; CComObject::CreateInstance(&p); p->QueryInterface(apple); return S_OK; } But i dont know how to turn it back( from interface to real class). the code list below seems doesn't work. STDMETHODIMP CBanana::DoSomethingOnApple(IApple* apple) { CApple* p = dynamic_cast(apple); p->num = 3; } I think dynamic_cast can't be used in this situation. But i really don't know the proper way. Would you please lend me a hand? I have blocked in this problem for almost one week.

        P Offline
        P Offline
        peterchen
        wrote on last edited by
        #3

        novachen wrote: But i dont know how to turn it back( from interface to real class Rule of thumb: You must not, because your apple might sit on a server in Siberia. Long answer: Unless you know 100% that the class implementing IApple was created on the same machine, in the same process (a few registry entries could easily break that). One of the fundamentals of COM is that you can never assume anything about the Implementing class, only abotu the interfaces. The bad thing is, it works most of the time, for inproc servers, so people try it.


        "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
        sighist | Agile Programming | doxygen

        N 1 Reply Last reply
        0
        • P peterchen

          novachen wrote: But i dont know how to turn it back( from interface to real class Rule of thumb: You must not, because your apple might sit on a server in Siberia. Long answer: Unless you know 100% that the class implementing IApple was created on the same machine, in the same process (a few registry entries could easily break that). One of the fundamentals of COM is that you can never assume anything about the Implementing class, only abotu the interfaces. The bad thing is, it works most of the time, for inproc servers, so people try it.


          "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
          sighist | Agile Programming | doxygen

          N Offline
          N Offline
          novachen
          wrote on last edited by
          #4

          Thanks! But in fact, I try to hide a complex structure inside COM interface. On the interface of COM, Apple is a very simple object, but in the eyes of the other objects in the same COM dll, Apple has more content than the interface. So i hope IApple can turn back into CApple, that i can access the hid part. I can't confirm the objects sit with client at same computer, but i can confirm those objects access each other on the same server. How can i achieve this design?

          P 1 Reply Last reply
          0
          • J Jorgen Sigvardsson

            You don't have to convert it back. If you look at the definition of CComObject<> you'll see:

            template
            class CComObject : public Base

            which means that CComObject<Base> inherits from Base. Everything Base has to offer, so will CComObject<Base>. -- Serial killers don't kill their boyfriends.

            N Offline
            N Offline
            novachen
            wrote on last edited by
            #5

            I dont understand. I try to convert IApple into CApple or CComObject. Any idea?

            J 1 Reply Last reply
            0
            • N novachen

              I dont understand. I try to convert IApple into CApple or CComObject. Any idea?

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              Why do the conversion to IApple to begin with? It is possible, but very unsafe, see peterchens answer down below. A rule of thumb is to convert to IApple's just before they leave a method defined in a COM interface. -- You know me. I sure know you.. Everyone of you!

              1 Reply Last reply
              0
              • N novachen

                Thanks! But in fact, I try to hide a complex structure inside COM interface. On the interface of COM, Apple is a very simple object, but in the eyes of the other objects in the same COM dll, Apple has more content than the interface. So i hope IApple can turn back into CApple, that i can access the hid part. I can't confirm the objects sit with client at same computer, but i can confirm those objects access each other on the same server. How can i achieve this design?

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                You cam 1. Make Apple only creatable by Banana 2. Tag each Apple instance with a GUID, that can be queried through the IApple interface 3. Keep a map<GUID, CApple *> to retrieve the CApple * belonging to an IApple * 4. Reject each IApple you get that you don't know. I have a slightly simpler case, that uses a slightly simpler implementation: Noone passes an IApple back to me (I just hand it out), and the apple doesn't have any state that can't be dereived from Banana. So I let the Banana implement the methods of Apple and make the IApple implementation forward the calls to it's Banana. Some care with avoiding circular ref's is all to take care of.


                "Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
                sighist | Agile Programming | doxygen

                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