From: Kazuhiro Fujieda Date: Sun, 8 Oct 2017 12:58:46 +0000 (+0900) Subject: Reduce invocations of CoInitializeEx X-Git-Tag: snap-180407~3 X-Git-Url: http://git.osdn.net/view?p=xkeymacs%2Fxkeymacs.git;a=commitdiff_plain;h=98c0a1605ea8514bb51fe6d478b443c686dd2553 Reduce invocations of CoInitializeEx --- diff --git a/xkeymacsdll/TSFHandler.cpp b/xkeymacsdll/TSFHandler.cpp index 2b54fa9..5392c4e 100644 --- a/xkeymacsdll/TSFHandler.cpp +++ b/xkeymacsdll/TSFHandler.cpp @@ -32,15 +32,19 @@ void TSFHandler::InitSink() { if (TLS::GetTSFHandler()) return; - HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - if (FAILED(hr)) { - DebugLog("CoInitializeEx failed."); - return; - } - if (hr == S_FALSE) - CoUninitialize(); ITfThreadMgr *thread; - if (FAILED(CoCreateInstance(CLSID_TF_ThreadMgr, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&thread)))) { + HRESULT hr = CoCreateInstance(CLSID_TF_ThreadMgr, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&thread)); + if (hr == CO_E_NOTINITIALIZED) { + hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); + if (FAILED(hr)) { + DebugLog("CoInitializeEx failed."); + return; + } + if (hr == S_FALSE) + CoUninitialize(); + hr = CoCreateInstance(CLSID_TF_ThreadMgr, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&thread)); + } + if (FAILED(hr)) { DebugLog("CoCreateInstance for ThreadMgr failed."); return; } diff --git a/xkeymacsdll/xkeymacsdll.cpp b/xkeymacsdll/xkeymacsdll.cpp index 45ddeab..df2d078 100644 --- a/xkeymacsdll/xkeymacsdll.cpp +++ b/xkeymacsdll/xkeymacsdll.cpp @@ -243,7 +243,6 @@ void CXkeymacsDll::ShowHookState() LRESULT CALLBACK CXkeymacsDll::CallWndProc(int nCode, WPARAM wParam, LPARAM lParam) { SetKeyboardHook(); - TSFHandler::InitSink(); if (nCode >= 0) { const CWPSTRUCT *cwps = reinterpret_cast(lParam); switch (cwps->message) { @@ -341,6 +340,7 @@ LRESULT CALLBACK CXkeymacsDll::ShellProc(int nCode, WPARAM wParam, LPARAM lParam void CXkeymacsDll::InitKeyboardProc() { AppName::Init(); + TSFHandler::InitSink(); if (m_CurrentConfig == NULL || _tcsnicmp(m_CurrentConfig->AppName, AppName::GetAppName(), 0xF) || // PROCESSENTRY32 has only 0xF bytes of Name !CUtils::IsMatchWindowText(m_CurrentConfig->WindowText)) {