Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.0 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. fusioncoinitialize.h
  5. Abstract:
  6. exception safe contructor/destructor local for CoInitialize(Ex)/CoUninitialize
  7. Author:
  8. Jay Krell (JayKrell) August 2001
  9. Revision History:
  10. --*/
  11. #if !defined(FUSION_INC_FUSION_COINITIALIZE_H_INCLUDED_)
  12. #define FUSION_INC_FUSION_COINITIALIZE_H_INCLUDED_
  13. #pragma once
  14. namespace F
  15. {
  16. class CWin32CoInitialize
  17. {
  18. private:
  19. HRESULT m_hresult;
  20. static HRESULT STDMETHODCALLTYPE CoInitializeEx_DownlevelFallback(void * Reserved, DWORD dwCoInit)
  21. {
  22. return ::CoInitialize(NULL);
  23. }
  24. public:
  25. CWin32CoInitialize();
  26. BOOL Win32Initialize(DWORD dwCoInit = COINIT_APARTMENTTHREADED);
  27. ~CWin32CoInitialize();
  28. };
  29. class CThrCoInitialize : public CWin32CoInitialize
  30. {
  31. protected:
  32. void ThrInit();
  33. public:
  34. CThrCoInitialize();
  35. ~CThrCoInitialize() { }
  36. };
  37. }
  38. inline void F::CThrCoInitialize::ThrInit()
  39. {
  40. FN_PROLOG_VOID_THROW
  41. IFW32FALSE_EXIT(this->Win32Initialize());
  42. FN_EPILOG_THROW
  43. }
  44. inline F::CThrCoInitialize::CThrCoInitialize()
  45. {
  46. this->ThrInit();
  47. }
  48. inline F::CWin32CoInitialize::CWin32CoInitialize() : m_hresult(E_FAIL) { }
  49. inline F::CWin32CoInitialize::~CWin32CoInitialize() { if (SUCCEEDED(m_hresult)) { m_hresult = E_FAIL; CoUninitialize(); } }
  50. inline BOOL F::CWin32CoInitialize::Win32Initialize(DWORD dwCoInit)
  51. {
  52. typedef HRESULT (STDMETHODCALLTYPE * PFN)(void * Reserved, DWORD dwCoInit);
  53. static PFN s_pfn;
  54. if (s_pfn == NULL)
  55. {
  56. PFN pfn = NULL;
  57. //
  58. // GetModuleHandle would be sufficient because we have static references to
  59. // CoInitialize and CoUninitialize, but in case delayload is used..
  60. //
  61. HMODULE Ole32 = ::LoadLibraryW(L"Ole32.dll");
  62. if (Ole32 != NULL)
  63. pfn = reinterpret_cast<PFN>(::GetProcAddress(Ole32, "CoInitializeEx"));
  64. if (pfn == NULL)
  65. pfn = &CoInitializeEx_DownlevelFallback;
  66. s_pfn = pfn;
  67. }
  68. return SUCCEEDED(m_hresult = (*s_pfn)(NULL, dwCoInit));
  69. }
  70. #endif // !defined(FUSION_INC_FUSION_COINITIALIZE_H_INCLUDED_)