How to make a function like GetInputMethod(HWND hwnd)?

iamoon-6427 85 Reputation points
2026-07-31T18:18:00.3233333+00:00

TLDR

How to get the active input method for foreground window, like function signature below:

TF_INPUTPROCESSORPROFILE GetInputMethod(HWND hwnd)

Not to get HKL, but TF_INPUTPROCESSORPROFILE, which contains the CLSID and GUIDProfile of the activate input method for forground window.

Detail

I would like to retrieve current foreground window's input method by HWND or something else, by using a command line tool.

input-method

However, calling below code sample just always return the system-wide default input profile

// Use [ITfInputProcessorProfileMgr] to get active profile
CComPtr<ITfInputProcessorProfileMgr> pTsfProfileMgr;
pTsfProfileMgr.CoCreateInstance(CLSID_TF_InputProcessorProfiles);
TF_INPUTPROCESSORPROFILE stActiveProfile;
pTsfProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD, &stActiveProfile);

For test, I tried to change the input method of Windows Terminal

Windows Terminal -> Start Powershell -> call the executable -> return default input profile

I guess the reason is GetActiveProfile is for the executable thread, not linked to Windows terminal's thread.

I have already figured that there is a way to get HKL for any HWND:

HWND hwnd = GetForegroundWindow();
DWORD dwProcessId = 0;
DWORD dwThreadId = GetWindowThreadProcessId(hwnd, &dwProcessId);
HKL hkl = GetKeyboardLayout(dwThreadId);

But I do need a TF_INPUTPROCESSORPROFILE to retrieve these things of current active input method:

  • TF_INPUTPROCESSORPROFILE.clsid
  • TF_INPUTPROCESSORPROFILE.guidprofile

because:

  • Some of my input method has the same HKL, but has different clsid and guidprofile
  • TF_INPUTPROCESSORPROFILE is more detailed, compared to HKL, which gives convience for further operations

I have tried AttachThreadInput as well, to bind the foreground HWND with the thread of command line tool, but not work (maybe I used it in the wrong way, not sure...).

Any help would be greatly appreciated.

Windows development | Windows API - Win32

Answer accepted by question author
Taki Ly (WICLOUD CORPORATION) 4,615 Reputation points Microsoft External Staff Moderator
2026-08-03T06:23:42.8766667+00:00

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:

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.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jay Pham (WICLOUD CORPORATION) 4,515 Reputation points Microsoft External Staff Moderator
    2026-08-03T00:19:41.2166667+00:00

    Hi @iamoon-6427 ,

    We are currently investigating the issue and will provide an update soon. Thank you for your patience.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.