From 5c543bb2bec439383e127d22bbd0d69c7997f9fc Mon Sep 17 00:00:00 2001 From: SEt <70444390+SEt-t@users.noreply.github.com> Date: Wed, 10 Dec 2025 01:46:27 +0300 Subject: [PATCH] Gracefully handle unavailable TSF from SYSTEM account (#19635) When run from SYSTEM account TSF seems to be unavailable. The only missing step to handle that is check during initialization. Not sure if fail after partial success in `Implementation::Initialize` should also be gracefully handled. Closes #19634 (cherry picked from commit 8bb831f628d3b8ddaf614c0a4d6f9b0b0533b5f0) Service-Card-Id: PVTI_lADOAF3p4s4BBcTlzgiXm14 Service-Version: 1.24 --- src/tsf/Handle.cpp | 6 +++++- src/tsf/Implementation.cpp | 10 ++++++++-- src/tsf/Implementation.h | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/tsf/Handle.cpp b/src/tsf/Handle.cpp index b7cae1d1be..ea1041c11a 100644 --- a/src/tsf/Handle.cpp +++ b/src/tsf/Handle.cpp @@ -12,7 +12,11 @@ Handle Handle::Create() { Handle handle; handle._impl = new Implementation(); - handle._impl->Initialize(); + if (!handle._impl->Initialize()) + { + delete handle._impl; + handle._impl = nullptr; + } return handle; } diff --git a/src/tsf/Implementation.cpp b/src/tsf/Implementation.cpp index 99ea34c01c..2b9e24ab73 100644 --- a/src/tsf/Implementation.cpp +++ b/src/tsf/Implementation.cpp @@ -68,9 +68,14 @@ void Implementation::SetDefaultScopeAlphanumericHalfWidth(bool enable) noexcept s_wantsAnsiInputScope.store(enable, std::memory_order_relaxed); } -void Implementation::Initialize() +bool Implementation::Initialize() { - _categoryMgr = wil::CoCreateInstance(CLSID_TF_CategoryMgr, CLSCTX_INPROC_SERVER); + _categoryMgr = wil::CoCreateInstanceNoThrow(CLSID_TF_CategoryMgr); + if (!_categoryMgr) + { + return false; + } + _displayAttributeMgr = wil::CoCreateInstance(CLSID_TF_DisplayAttributeMgr); // There's no point in calling TF_GetThreadMgr. ITfThreadMgr is a per-thread singleton. @@ -89,6 +94,7 @@ void Implementation::Initialize() THROW_IF_FAILED(_contextSource->AdviseSink(IID_ITfTextEditSink, static_cast(this), &_cookieTextEditSink)); THROW_IF_FAILED(_documentMgr->Push(_context.get())); + return true; } void Implementation::Uninitialize() noexcept diff --git a/src/tsf/Implementation.h b/src/tsf/Implementation.h index 82e84ab888..9270b510d5 100644 --- a/src/tsf/Implementation.h +++ b/src/tsf/Implementation.h @@ -21,7 +21,7 @@ namespace Microsoft::Console::TSF virtual ~Implementation() = default; - void Initialize(); + bool Initialize(); void Uninitialize() noexcept; HWND FindWindowOfActiveTSF() noexcept; void AssociateFocus(IDataProvider* provider);