tags:

views:

79

answers:

2

Hi all,

currently I'm struggling trying to use a COM dll on a simple system that I've made. Everything compiles successfully, but in runtime the CoCreateInstace is returning S_OK, but somehow my object pointer is returning NULL.

This interface pointer is created on my class header. The weirdest thing is that instantiating this same pointer type on the method stack results in a correct object, but subsequent calls to __hook turn on an acccessviolation when trying to create a BASE com class.

Some other aspects that might be useful:

  • Tried to run the program with CoInitalizeEx started as COINIT_MULTITHREADED and COINIT_APARTMENTTHREADED

  • The project is a dll which uses the COM dll in it

  • I've tried the same method without starting a new thread and the error persists

  • I've made a test program ( no threads, executable ) and the object is created normally, and hooked correctly. So my guess it is something related to it being a DLL itself or threaded related.

PS: As bonus question, why google doesn't return anything favorable related to COM? :)

+2  A: 

It sounds like a bug in the COM object's implementation of IUnknown::QueryInterface - not setting the output pointer but returning S_OK.

CoCreateInstance for an in-proc server is basically:

  • Load the DLL into memory
  • Call DllGetClassObject to get the class factory
  • Call IClassFactory::CreateInstance from the class factory which allocates a new object
  • Call IUnknown::QueryInterface on the new object to get the desired interface.

Returning NULL but S_OK at any step should result in a crash, except for the QI call at the end.

Michael
I don't think so, because this way would make the sample app I've made fail too.
A: 

Found the problem: The module attribute was defined on a static library, and that made the COM object go crazy; Moving it to the DLL source resolved the problem.