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()

        //Call the COM object

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

     CTestMacroInterface* pObject =( CTestMacroInterface*)p;


       return 0;


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
    STDMETHODIMP CMTSDriverDisp::ClearChannelAssignments()
     if (m_pCurrentdrv!=NULL)
     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;en-us;Q150777&SD=MSKB&

   INFO: OLE Threads Must Dispatch Messages;en-us;Q136885

   Inproc Object Custom Interface in Apartmetn Model Client;en-us;Q137629

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

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