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