我们有一个旧的,遗留的数据库,需要从另一个系统输入。将数据输入到数据库表单的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中的这个没有:
^{pr2}$问题是,我们的大多数新逻辑都是用python编写的。我把C代码转换成了一个小的python模块,它可以工作,但结果是现在我依赖于微软的巨大编译器,并在模块构建上做了很多手脚。我想要一个只有python的解决方案。在
你知道为什么这个python代码不能工作吗?这些系统调用看起来一样。。。在
是,AttachThreadInput失败。根据这里的注释https://toster.ru/q/79336win32进程.GetWindowThreadProcessId返回错误值,必须使用ctypes。此代码起作用:
相关问题 更多 >
编程相关推荐