gpt4 book ai didi

python - 在python中使用SendMessage在前台窗口中设置文本

转载 作者:行者123 更新时间:2023-11-30 16:13:31 25 4
gpt4 key购买 nike

我们有一个旧的遗留数据库,需要来自另一个系统的输入。将数据输入数据库表单的SendInput方法缓慢且不可靠,设置剪贴板然后^v也不可靠(我不知道为什么,但数据库接口(interface)很旧,2000年代初)。经过大量摆弄后,我发现使用 SendMessage 设置文本然后发送 VK_RETURN 速度很快(比 SendInput/keybd_event 快得多)并且对我们的数据库可靠。现在这段纯 C 代码可以工作:

    HWND fghwnd = GetForegroundWindow();
DWORD threadId = GetWindowThreadProcessId(fghwnd, NULL);
DWORD myId = GetCurrentThreadId();
if (AttachThreadInput(myId, threadId, true)) {
HWND ctrl = GetFocus();
SendMessage(ctrl, WM_SETTEXT, 0, (LPARAM) sendbuf); // TESTING
PostMessage(ctrl, WM_KEYDOWN, VK_RETURN, 0);
PostMessage(ctrl, WM_KEYUP, VK_RETURN, 0);
AttachThreadInput(myId, threadId, false);
} else {
printf("\nError: AttachThreadInput failure!\n");
}

但是 python 中的这个却没有:

    foregroundHwnd = win32gui.GetForegroundWindow()
foregroundThreadID = win32process.GetWindowThreadProcessId(foregroundHwnd)[0]
ourThreadID = win32api.GetCurrentThreadId()
if foregroundThreadID != ourThreadID:
win32process.AttachThreadInput(foregroundThreadID, ourThreadID, True)
focus_whd = win32gui.GetFocus()
win32gui.SendMessage(focus_whd, win32con.WM_SETTEXT, None, "test text")
win32gui.PostMessage(focus_whd, win32con.WM_KEYDOWN, win32con.VK_RETURN, None)
win32gui.PostMessage(focus_whd, win32con.WM_KEYUP, win32con.VK_RETURN, None)
win32process.AttachThreadInput(foregroundThreadID, ourThreadID, False)

问题是,我们的大部分新逻辑都是用 python 编写的。我把 C 代码变成了一个小的 python 模块,它可以工作,但结果现在我依赖于微软庞大的编译器,并且对模块构建进行了大量的摆弄。我想要一个仅使用 python 的解决方案。

有什么想法为什么这个Python代码不起作用吗?这些系统调用看起来相同...

最佳答案

是的,AttachThreadInput 失败。根据这里的评论https://toster.ru/q/79336 win32process.GetWindowThreadProcessId 返回错误值,必须使用 ctypes。此代码有效:

"""
Fast "paste" implemented via calls to Windows internals, sends parameter
string and RETURN after that

Usage:

from paste import paste
paste("test")

"""

import time
import random
import string
from ctypes import windll
import ctypes
import win32con

def random_string(string_length=10):
"""Generate a random string of fixed length """
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(string_length))

ERROR_INVALID_PARAMETER = 87

def paste(text_to_paste):
"""Fast "paste" using WM_SETTEXT method + Enter key"""
current_hwnd = windll.user32.GetForegroundWindow()
current_thread_id = windll.kernel32.GetCurrentThreadId()
thread_process_id = windll.user32.GetWindowThreadProcessId(current_hwnd, None)
if thread_process_id != current_thread_id:
res = windll.user32.AttachThreadInput(thread_process_id, current_thread_id, True)
# ERROR_INVALID_PARAMETER means that the two threads are already attached.
if res == 0 and ctypes.GetLastError() != ERROR_INVALID_PARAMETER:
print("WARN: could not attach thread input to thread {0} ({1})"
.format(thread_process_id, ctypes.GetLastError()))
return
focus_whd = windll.user32.GetFocus()
windll.user32.SendMessageW(focus_whd, win32con.WM_SETTEXT, None, text_to_paste)
windll.user32.PostMessageW(focus_whd, win32con.WM_KEYDOWN, win32con.VK_RETURN, None)
windll.user32.PostMessageW(focus_whd, win32con.WM_KEYUP, win32con.VK_RETURN, None)
res = windll.user32.AttachThreadInput(thread_process_id, current_thread_id, True)


if __name__ == '__main__':
time.sleep(5) # time to switch to the target
# paste random 150 char string
paste(random_string(150))

关于python - 在python中使用SendMessage在前台窗口中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58031135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com