Skip to content

Commit 8bb831f

Browse files
authored
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
1 parent 224ac9d commit 8bb831f

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/tsf/Handle.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ Handle Handle::Create()
1212
{
1313
Handle handle;
1414
handle._impl = new Implementation();
15-
handle._impl->Initialize();
15+
if (!handle._impl->Initialize())
16+
{
17+
delete handle._impl;
18+
handle._impl = nullptr;
19+
}
1620
return handle;
1721
}
1822

src/tsf/Implementation.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,14 @@ void Implementation::SetDefaultScopeAlphanumericHalfWidth(bool enable) noexcept
6868
s_wantsAnsiInputScope.store(enable, std::memory_order_relaxed);
6969
}
7070

71-
void Implementation::Initialize()
71+
bool Implementation::Initialize()
7272
{
73-
_categoryMgr = wil::CoCreateInstance<ITfCategoryMgr>(CLSID_TF_CategoryMgr, CLSCTX_INPROC_SERVER);
73+
_categoryMgr = wil::CoCreateInstanceNoThrow<ITfCategoryMgr>(CLSID_TF_CategoryMgr);
74+
if (!_categoryMgr)
75+
{
76+
return false;
77+
}
78+
7479
_displayAttributeMgr = wil::CoCreateInstance<ITfDisplayAttributeMgr>(CLSID_TF_DisplayAttributeMgr);
7580

7681
// There's no point in calling TF_GetThreadMgr. ITfThreadMgr is a per-thread singleton.
@@ -89,6 +94,7 @@ void Implementation::Initialize()
8994
THROW_IF_FAILED(_contextSource->AdviseSink(IID_ITfTextEditSink, static_cast<ITfTextEditSink*>(this), &_cookieTextEditSink));
9095

9196
THROW_IF_FAILED(_documentMgr->Push(_context.get()));
97+
return true;
9298
}
9399

94100
void Implementation::Uninitialize() noexcept

src/tsf/Implementation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Microsoft::Console::TSF
2121

2222
virtual ~Implementation() = default;
2323

24-
void Initialize();
24+
bool Initialize();
2525
void Uninitialize() noexcept;
2626
HWND FindWindowOfActiveTSF() noexcept;
2727
void AssociateFocus(IDataProvider* provider);

0 commit comments

Comments
 (0)