shco.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 1k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "proj.h"
  2. // Need to put into separate file because for some reason the /Gy compiler
  3. // option doesn't work.
  4. //
  5. //  COM Initialization is weird due to multithreaded apartments.
  6. //
  7. //  If this thread has not called CoInitialize yet, but some other thread
  8. //  in the process has called CoInitialize with the COINIT_MULTITHREADED,
  9. //  then that infects our thread with the multithreaded virus, and a
  10. //  COINIT_APARTMENTTHREADED will fail.
  11. //
  12. //  In this case, we must turn around and re-init ourselves as
  13. //  COINIT_MULTITHREADED to increment the COM refcount on our thread.
  14. //  If we didn't do that, and that other thread decided to do a
  15. //  CoUninitialize, that >secretly< uninitializes COM on our own thread
  16. //  and we fall over and die.
  17. //
  18. STDAPI SHCoInitialize(void)
  19. {
  20.     HRESULT hres;
  21.     hres = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  22.     if (FAILED(hres)) {
  23.         hres = CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
  24.     }
  25.     return hres;
  26. }