Help on CoInitialize,CoInitializeEx and CoUninitialize(); 
Author Message
 Help on CoInitialize,CoInitializeEx and CoUninitialize();

Hi every one
I had a confusion with where and when to using CoInitialize,CoInitializeEx
and CoUninitialize();

I have a Atl based com ServerA(outprocess exe.) I build another atl ServerB
(a dll) which uses COM objects of ServerA. Finally VB uses ServerB objects.
ServerA has one function which is running in second thread too. Server B
aslo has second thread too. So there is a situation where three threads can
run pararelly.

My questionS are
1. Do I have to add  CoInitialize(NULL) and CoUninitialize() to each every
function which uses COM object.
    eg FunctionX()
    {
        CoInitialize(NULL)

        //Call the COM object

        CoUninitialize()
    }
2.only Add CoInitializeEX(....) and CoUninitialize() only in the thread
creation ??
eg: DWORD WINAPI CTestMacroInterface::ThreadProcInitiateTest(void *p)
    {

     ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
     CTestMacroInterface* pObject =( CTestMacroInterface*)p;

     pObject->ExecuteTest();

     ::CoUninitialize();
       return 0;

Quote:
}

3. Do I have to CoInitialize(NULL) only for Dll server and
CoInitializeEX(...) for EXE server ??

4.All the dispatch functions need to add   CoInitialize(NULL) and
CoUninitialize()  internally
eg
    STDMETHODIMP CMTSDriverDisp::ClearChannelAssignments()
    {
     ::CoInitialize(NULL);
     if (m_pCurrentdrv!=NULL)
     {
      m_pCurrentdrv->ClearChannelAssignments();
     }
     ::CoUninitialize();
     return S_OK;
    }
5. If there is any harm if I add all function in my class with
::CoInitialize(NULL); and ::CoUninitialize();
 in pairs
6. Can I share static variable in two thread. eg Class A has a static
variable. object A which is created in main thread access in the static
variable.and object B aslo acess the static variable in a second thread.

I got and access violation error in my program. Please Help me out the rules
how to I use these initialisation.



Wed, 08 Sep 2004 00:23:06 GMT  
 Help on CoInitialize,CoInitializeEx and CoUninitialize();
Hi Ravi,

You only need to call CoInitialize(Ex) once per thread. Which one you want
to call depends upon the threading model you want to support.

You'll want to read up on COM threading models so you have a better idea of
what's going on here. Check out the following and feel free to post any
follow up questions you might have.

    INFO: Description and Workings of OLE Threading Models
   http://support.microsoft.com/default.aspx?scid=kb;en-us;Q150777&SD=MSKB&

   INFO: OLE Threads Must Dispatch Messages
   http://support.microsoft.com/default.aspx?scid=kb;en-us;Q136885

   Inproc Object Custom Interface in Apartmetn Model Client
   http://support.microsoft.com/default.aspx?scid=kb;en-us;Q137629

Also, I like to recommend Don Box's 'Essential COM' book, which is a great
resource for begining COM programmers.

Sincerely,
Ed Dore [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.



Wed, 08 Sep 2004 08:31:17 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help ...When to Call ::CoInitialize and ::CoUninitialize();-

2. Problem with multiple CoInitialize/CoUninitialize

3. Reg. CoInitialize and CoUninitialize from DLLs

4. ExitThread hangs due to CoInitialize / CoUninitialize

5. Change CoInitialize to CoInitializeEx

6. CoInitializeEx undeclared..but CoInitialize works fine

7. CoUninitialize takes time.

8. InitCommonControlsEx - CoUninitialize

9. CoUninitialize inside each thread?

10. CoUninitialize!

11. Using CoUninitialize and a smart pointer in the same procedure

 

 
Powered by phpBB® Forum Software