Reputation: 2873
Note:
Using CoGetClassObject, to create multiple objects through a class object for which there is a CLSID in the system registry
Single threaded apartment
For instance:
hresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
IClassFactory *pIClassFactory;
hresult = CoGetClassObject (clsid, CLSCTX_LOCAL_SERVER, NULL, IID_IClassFactory, (LPVOID *)&pIClassFactory);
hresult = pIClassFactory->QueryInterface (IID_IUnknown, (LPVOID *)&pUnk);
hresult = pUnk->QueryInterface (__uuidof(IExample), (LPVOID *)&pISimClass);
Note:
Question:
Upvotes: 0
Views: 4510
Reputation: 76001
The problem here is that you are confusing the class object and the object itself. CoGetClassObject
will give you a pointer to an object that implements IClassFactory
and intended to create an instance of the object you are interested in. It is not an actual instance of that object.
In your example, you are getting an IUnknown
pointer by calling QueryInterface
on the IClassFactory
pointer. This pointer still points to the instance of the class object, hence doing QueryInterface
for the interface you are interested in results in an error. Instead you need to call IClassFactory::Createinstance
to get the IUnknown
pointer to the actual object and do the QueryInterface
on that pointer.
Upvotes: 3