diff --git a/src/msw/app.cpp b/src/msw/app.cpp index 027a075fd0..be4bc5e9dc 100644 --- a/src/msw/app.cpp +++ b/src/msw/app.cpp @@ -145,6 +145,33 @@ wxVector gs_regClassesInfo; LRESULT WXDLLEXPORT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM); +// ---------------------------------------------------------------------------- +// Module for OLE initialization and cleanup +// ---------------------------------------------------------------------------- + +class wxOleInitModule : public wxModule +{ +public: + wxOleInitModule() + { + } + + virtual bool OnInit() wxOVERRIDE + { + return wxOleInitialize(); + } + + virtual void OnExit() wxOVERRIDE + { + wxOleUninitialize(); + } + +private: + wxDECLARE_DYNAMIC_CLASS(wxOleInitModule); +}; + +wxIMPLEMENT_DYNAMIC_CLASS(wxOleInitModule, wxModule); + // =========================================================================== // wxGUIAppTraits implementation // =========================================================================== @@ -622,8 +649,6 @@ bool wxApp::Initialize(int& argc_, wxChar **argv_) InitCommonControls(); - wxOleInitialize(); - wxSetKeyboardHook(true); callBaseCleanup.Dismiss(); @@ -739,8 +764,6 @@ void wxApp::CleanUp() wxSetKeyboardHook(false); - wxOleUninitialize(); - // for an EXE the classes are unregistered when it terminates but DLL may // be loaded several times (load/unload/load) into the same process in // which case the registration will fail after the first time if we don't diff --git a/src/msw/clipbrd.cpp b/src/msw/clipbrd.cpp index 8d4ad6ae9f..ea77bd4182 100644 --- a/src/msw/clipbrd.cpp +++ b/src/msw/clipbrd.cpp @@ -458,10 +458,6 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject); wxClipboard::wxClipboard() { -#if wxUSE_OLE_CLIPBOARD - wxOleInitialize(); -#endif - m_lastDataObject = NULL; m_isOpened = false; } @@ -472,10 +468,6 @@ wxClipboard::~wxClipboard() { Clear(); } - -#if wxUSE_OLE_CLIPBOARD - wxOleUninitialize(); -#endif } void wxClipboard::Clear() diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index 7ad74fc4e2..9231d36d6c 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -5247,14 +5247,14 @@ class wxDirect2DModule : public wxModule public: wxDirect2DModule() { + // Using Direct2D requires OLE and, importantly, we must ensure our + // OnExit() runs before it is uninitialized. + AddDependency("wxOleInitModule"); } virtual bool OnInit() wxOVERRIDE { - HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); - // RPC_E_CHANGED_MODE is not considered as an error - // - see remarks for wxOleInitialize(). - return SUCCEEDED(hr) || hr == RPC_E_CHANGED_MODE; + return true; } virtual void OnExit() wxOVERRIDE @@ -5289,8 +5289,6 @@ public: gs_ID2D1Factory->Release(); gs_ID2D1Factory = NULL; } - - ::CoUninitialize(); } private: diff --git a/src/msw/rt/notifmsgrt.cpp b/src/msw/rt/notifmsgrt.cpp index 3b9ba571ce..d91e64454b 100644 --- a/src/msw/rt/notifmsgrt.cpp +++ b/src/msw/rt/notifmsgrt.cpp @@ -463,6 +463,9 @@ class wxToastNotifMsgModule : public wxModule public: wxToastNotifMsgModule() { + // Using RT API requires OLE and, importantly, we must ensure our + // OnExit() runs before it is uninitialized. + AddDependency("wxOleInitModule"); } virtual bool OnInit() wxOVERRIDE