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

使用python中的SendMessage在前台窗口中设置文本,python,c,windows,winapi,Python,C,Windows,Winapi,我们有一个旧的遗留数据库,需要从另一个系统输入。SendInput将数据输入数据库表单的方法速度慢且不可靠,设置剪贴板和^v也不可靠(我不知道为什么,但数据库界面非常陈旧,在2000年代早期)。经过多次修改,我发现使用SendMessage设置文本,然后发送VK_RETURN速度很快(比SendInput/keybd_事件快得多),而且对于我们的数据库来说是可靠的。现在,此代码在普通C中工作: HWND fghwnd = GetForegroundWindow(); DWORD

我们有一个旧的遗留数据库,需要从另一个系统输入。SendInput将数据输入数据库表单的方法速度慢且不可靠,设置剪贴板和^v也不可靠(我不知道为什么,但数据库界面非常陈旧,在2000年代早期)。经过多次修改,我发现使用SendMessage设置文本,然后发送VK_RETURN速度很快(比SendInput/keybd_事件快得多),而且对于我们的数据库来说是可靠的。现在,此代码在普通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失败。根据此处的注释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))

你没有错误检查。可能有一个API调用失败。建议检查
focus\u whd
的返回值,它在我的Python编译器中返回0。最后一个
AttachThreadInput
不是应该用
False
分离线程吗?