Aaron
Aaron

Reputation: 2873

Why do I get E_NOINTERFACE when creating an object that supports that interface?

Note:

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

Answers (2)

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

Also, take a look at CoCreateInstance function.

Upvotes: 1

Franci Penov
Franci Penov

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

Related Questions