Hi,
I'm trying to use ImmGetContext to get the IME context of another process's window, but getting error "himc = 0". The documentation doesn't mention any process boundary restrictions.
Code:
import time
import win32api
import win32gui
import ctypes
from ctypes import windll, byref, c_uint, c_wchar
def switch_to_english():
# 获取输入法上下文
imm32 = ctypes.windll.imm32
# 获取前台窗口句柄
hwnd1 = win32gui.GetForegroundWindow()
hwnd = ctypes.windll.imm32.ImmGetDefaultIMEWnd(hwnd1)
if not hwnd:
print("无法获取活动窗口")
return False
# 尝试获取当前具有键盘输入焦点的窗口句柄
focus_hwnd = win32gui.GetFocus()
# 优先尝试对 focus_hwnd 调用 ImmGetContext
if focus_hwnd:
print("1")
himc = imm32.ImmGetContext(focus_hwnd)
else:
print("2")
himc = imm32.ImmGetContext(hwnd)
if not himc:
print("无法获取 IME 上下文")
return False
from ctypes import wintypes
HWND = wintypes.HWND
BOOL = wintypes.BOOL
DWORD = wintypes.DWORD
if himc:
# 设置为英文模式
conversion = DWORD(0)
sentence = DWORD(0)
imm32.ImmGetConversionStatus(himc, byref(conversion), byref(sentence))
# conversion.value &= ~0x0001
imm32.ImmSetConversionStatus(himc, conversion.value, sentence.value)
imm32.ImmSetOpenStatus(himc, BOOL(0))
# time.sleep(2)
if __name__ == '__main__':
switch_to_english()
Output:
2
Failed to get IME context
Process finished with exit code 0
Environment:
- Windows Version: Windows 10 22H2
- Python Version: Python 3.13.0
- Running with administrator privileges
Questions:
- Is ImmGetContext designed to work across process boundaries?
- If yes, what additional permissions or settings are needed?
- If not, what's the recommended way to control IME status of windows from other processes?
I've checked the documentation at https://learn.microsoft.com/en-us/windows/win32/api/imm/nf-imm-immgetcontext but found no explicit mention of process boundary restrictions.