CoInitializeSecurity with multiple threads and different COM models RRS feed

  • Question

  • My main thread calls AfxOleInit() before creating a window.  I understand that AfxOleInit() internally calls CoInitializeEx() with COINIT_APARTMENTTHREADED as required by MFC.  I did not wish to call CoInitializeSecurity() in my main thread.

    One subthread has to call CoInitializeEx() with COINIT_MULTITHREADED in order to invoke some WMI interfaces.  Actually I did not experiment with COINIT_APARTMENTTHREADED so I'm not sure if some MSDN pages are correct in saying that COINIT_MULTITHREADED is required, but COINIT_MULTITHREADED has been working as documented.  I wished to call CoInitializeSecurity() in my subthread.

    But CoInitializeSecurity() returns an error in my subthread because my main thread has already marshalled some interfaces.

    Today's experiments indicate that putting the call to CoInitializeSecurity() in my main thread lets my subthread work correctly.  My main thread works correctly too, but I worry.  My main thread has UI; it creates windows.  Is my UI now exposed to security threats?

    Where CoInitializeEx() operates perfectly fine with different threading models in different threads, WHY does CoInitializeSecurity() require a single setting across the entire process?

    I don't really understand CoInitializeSecurity() and I only use it because, as MSDN pages document, some WMI interfaces require that the thread call CoInitializeSecurity() before invoking the WMI interfaces.

    Monday, March 17, 2014 2:36 AM