Using COM

  • Thread starter Thread starter its_me_here
  • Start date Start date
I

its_me_here

Guest
I am creating an instance of a COM object as below:

CComPtr<IMyInterface> pMyInterface; //class member

MySampleClass::Iitialize()
{

HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr)
{

return;
}


hr = CoCreateInstance(CLSID_MyInterface,
NULL,
CLSCTX_ALL,
IID_IMyInterface,
(void**)&pMyInterface);

.....

::CoUnInitilze();
}


As I said pMyInterface is a COMPtr declared as the member of the class. So I am using that pointer in other methods of the class. But I am aware that according to COM spec, CoInitilalise() and CoUninitilaize() are to be called in the same function where the COM object is created. I have some questions in this regard:

1. Is pMyInterace pointer supposed to be used by other methods of the class since CoInitliase/Unitilize is in the same location? Is it a good design?

2. I understand that after use, pMyInterface should be set to null before COM uninitlisation, I am not able to set it to null in the same location since I need it in the rest of the class after Com Unitilisation.

3. Since I need pMyInterface in the rest of the class, is it OK to call CoInitilaze in one function and CoUnIntialize() in another function, say constructror and destructor respectively, and call CoCreateInstace to get pMyInterface pointer in another>

4. Should pMyInterface be released by explicit call pMyInterface .relase() after use?

Continue reading...
 
Back
Top