Answer the question
In order to leave comments, you need to log in
Why is the Cyrillic string in create_string_buffer distorted?
Good day.
Tell me, what could be the problem, there is such a code in Python:
import time
from ctypes import *
from win32con import *
from ctypes.wintypes import HWND, DWORD
from win32gui import GetForegroundWindow, SendMessage
from win32process import GetWindowThreadProcessId
GetGUIThreadInfo = windll.user32.GetGUIThreadInfo
class RECT(Structure):
_fields_ = [("left", c_ulong),
("top", c_ulong),
("right", c_ulong),
("bottom", c_ulong)]
class GUITHREADINFO(Structure):
_fields_ = [("cbSize", DWORD),
("flags", DWORD),
("hwndActive", HWND),
("hwndFocus", HWND),
("hwndCapture", HWND),
("hwndMenuOwner", HWND),
("hwndMoveSize", HWND),
("hwndCaret", HWND),
("rcCaret", RECT)]
time.sleep(5)
PID = GetWindowThreadProcessId(GetForegroundWindow())
lpgui = GUITHREADINFO()
lpgui.cbSize = sizeof(GUITHREADINFO)
GetGUIThreadInfo(PID[0], pointer(lpgui))
textMaxSize = 32
text = create_string_buffer(textMaxSize)
SendMessage(lpgui.hwndFocus, WM_GETTEXT, textMaxSize, text)
print(text.value)
Answer the question
In order to leave comments, you need to log in
I figured out what the problem is, text.value encodes a string in utf-8 by default, and SendMessage writes a character in two bytes to the buffer, i.e. if you recode the buffer yourself like this:
then everything is displayed correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question