Hello @iamoon-6427 ,
It appears that your hypothesis might be on the right track. The Text Services Framework (TSF) context is heavily thread-local. When calling GetActiveProfile from a standalone command-line .exe, it queries the profile for its own isolated thread, and AttachThreadInput may not be sufficient to bridge the TSF context between two different processes.
Since your goal is to build an editor extension, I suggest considering a transition from a standalone executable to an in-process native module (such as a Node.js Addon .node for VS Code, or a standard .dll plugin). By running the C++ code within the editor's actual process/thread, the TSF APIs should seamlessly apply to the target window.
Below is a simplified C++ snippet using ITfInputProcessorProfileMgr that you can wrap inside a native addon to both get and set the profile in-process:
#include <msctf.h>
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
// 1. Initialize COM safely for the thread (e.g., CoInitialize)
// ...
ComPtr<ITfInputProcessorProfileMgr> pTsfProfileMgr;
HRESULT hr = CoCreateInstance(CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_INPROC_SERVER, IID_ITfInputProcessorProfileMgr, (void**)&pTsfProfileMgr);
if (SUCCEEDED(hr) && pTsfProfileMgr) {
// GET the active profile
TF_INPUTPROCESSORPROFILE stActiveProfile;
hr = pTsfProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD, &stActiveProfile);
// SET a new profile (using the CLSID, GUID, and LangID you retrieved)
/*
pTsfProfileMgr->ActivateProfile(
TF_PROFILETYPE_INPUTPROCESSOR,
targetLangId,
targetClsid,
targetGuidProfile,
NULL,
TF_IPPMF_FORPROCESS // Crucial: sets it for the current process
);
*/
}
(Alternatively, if you must use a standalone .exe, it might be easier to assign Windows OS hotkeys to your languages in Settings and use the SendInput API to simulate those keystrokes based on the editor context).
Reference Documents:
- ITfInputProcessorProfileMgr::GetActiveProfile
- ITfInputProcessorProfileMgr::ActivateProfile
- C++ Addons for Node.js
Disclaimer: Some links are non-Microsoft website. The pages appear to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classifies as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.
I hope this information is helpful. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation.
Thank you.