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. Confused about queryinterface and cocreateinstance

Confused about queryinterface and cocreateinstance

Scheduled Pinned Locked Moved COM
databasecomhelpquestion
4 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.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    Hi I am trying to understand relation between queryinterface and cocretaeinstance. I read so many things still dont understand. (For cocretaeinstance) In MSDN it says: "This function creates on the local system a single uninitialized object of the class associated with a specified class identifier. " I understand that We use cocreateinstance to take instance for a class and we query for a supported interface with queryinterface. But if we use COM for using Interface instead of class object. So why do we take instance of a class? Isn't concept of COM using Interface instead of class object. So hiding implemantion details and avoiding name mangling etc... So why do we take a class object? I dont understand here. In what conditions do we need/use cocretaeinstance and queryinterface together? And in what conditions one of them is enough? I hope i could explain the problem. I am looking for your answers Thanks.

    C S 2 Replies Last reply
    0
    • S sawerr

      Hi I am trying to understand relation between queryinterface and cocretaeinstance. I read so many things still dont understand. (For cocretaeinstance) In MSDN it says: "This function creates on the local system a single uninitialized object of the class associated with a specified class identifier. " I understand that We use cocreateinstance to take instance for a class and we query for a supported interface with queryinterface. But if we use COM for using Interface instead of class object. So why do we take instance of a class? Isn't concept of COM using Interface instead of class object. So hiding implemantion details and avoiding name mangling etc... So why do we take a class object? I dont understand here. In what conditions do we need/use cocretaeinstance and queryinterface together? And in what conditions one of them is enough? I hope i could explain the problem. I am looking for your answers Thanks.

      C Offline
      C Offline
      cool_raja
      wrote on last edited by
      #2

      I guess, one cannot take an instace of a COM class...As it is still abstract... It is passed as a parameter to one of the CComObject<> varaiants to instantiate it.. If you want to access other interfaces supported by the coclass, then it can be done by queryinterface-ing the already obtained interface... instead of re-creating the Object instance... Hope this helps,...:)

      raja

      1 Reply Last reply
      0
      • S sawerr

        Hi I am trying to understand relation between queryinterface and cocretaeinstance. I read so many things still dont understand. (For cocretaeinstance) In MSDN it says: "This function creates on the local system a single uninitialized object of the class associated with a specified class identifier. " I understand that We use cocreateinstance to take instance for a class and we query for a supported interface with queryinterface. But if we use COM for using Interface instead of class object. So why do we take instance of a class? Isn't concept of COM using Interface instead of class object. So hiding implemantion details and avoiding name mangling etc... So why do we take a class object? I dont understand here. In what conditions do we need/use cocretaeinstance and queryinterface together? And in what conditions one of them is enough? I hope i could explain the problem. I am looking for your answers Thanks.

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        CoCreateInstance will give you an instance of a class of object (defined by the CLSID), and allow you to reference it via an interface (defined by the IID). You can then ask the object for a different interface via QueryInterface. As an example, imagine an object which displays data onto a device context. This uses an 'IRender' interface, which allows you to specify stuff like where to render to, and stuff like a zoom ratio, etc. But the object also supports the IPersistFile interface, to allow the saving/loading of the object to/from file. The only way to get to IPersistFile from IRender is via IUnknown's QueryInterface method. If you only use simple objects, they may only implement one (custom) interface, although in practice IUnknown must always be supported as well. Does that help?

        Steve S Developer for hire

        S 1 Reply Last reply
        0
        • S Steve S

          CoCreateInstance will give you an instance of a class of object (defined by the CLSID), and allow you to reference it via an interface (defined by the IID). You can then ask the object for a different interface via QueryInterface. As an example, imagine an object which displays data onto a device context. This uses an 'IRender' interface, which allows you to specify stuff like where to render to, and stuff like a zoom ratio, etc. But the object also supports the IPersistFile interface, to allow the saving/loading of the object to/from file. The only way to get to IPersistFile from IRender is via IUnknown's QueryInterface method. If you only use simple objects, they may only implement one (custom) interface, although in practice IUnknown must always be supported as well. Does that help?

          Steve S Developer for hire

          S Offline
          S Offline
          sawerr
          wrote on last edited by
          #4

          OK. Thank you...

          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