devxlogo

Use Cogetclassobject Instead Of Cocreateinstance While Creating COM Components

Use Cogetclassobject Instead Of Cocreateinstance While Creating COM Components

In most cases, you can use CoCreateInstance and forget about CoGetClassObject.Function CoGetClassObject returns the pointer to the desired component’s classfactory and not the component itself. The desired component is created using thethe pointer returned from the function CoGetClassObject while theCoCreateInstance returns the requested interface pointer to that component (itcreates the component using IClassFactory internally).

However, there are two cases where you should use CoGetClassObject instead ofCoCreateInstance. First, you must use CoGetClassObject if you want to createobject using a creation interface other than IClassFactory (say if you want touse IClassFactory2). Second, if you want to create a bunch of components at the same time, it is more efficient to create the class factory for all of thecomponents instead of creating and releasing the class factory for everyinstance of a component.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist