Python ctypes : Prototype with LPCSTR [out] parameter

标签 python winapi prototype ctypes user32

我目前正在进入 ctypes 模块,并尝试调用 user32 函数 GetWindowText带有我已经通过使用 FindWindow 收到的 HWND 句柄。这次我想进一步处理并使用函数原型(prototype),而不是使用 ctypes.windll.user32.GetWindowText 调用函数。尽管我在将 lpString 争论声明为输出参数时遇到问题。

我的第一次尝试是这样的:

GetWindowText = cfunc("GetWindowTextA",windll.user32,c_int,
                  ("hWnd",HWND,1),
                  ("lpString",LPCSTR,2),
                  ("nMaxCount",c_int,1)
                  )

(cfunc 是我发现的一个小包装 here )

这个原型(prototype)一旦被调用就会产生以下异常:

    chars,name = user32.GetWindowText(handle,255)
TypeError: c_char_p 'out' parameter must be passed as default value

我认为任何输出变量都必须是 POINTER(...) 类型,因此我将定义更改为:

GetWindowText = cfunc("GetWindowTextA",windll.user32,c_int,
                      ("hWnd",HWND,1),
                      ("lpString",POINTER(c_char),2),
                      ("nMaxCount",c_int,1)
                      )

但这也会产生一个异常:

    chars,name = user32.GetWindowText(handle,255)
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong type

我希望有人知道如何使用 ctypes 原型(prototype)正确调用 GetWindowText 函数。

编辑:

通过进一步的研究,我可以让它发挥作用,至少以某种方式。我修复的第一个问题是使用 cfunc() 时调用说明符错误。我定义了该函数的精确副本,并将其命名为 winfunc() 并替换了 return CFUNCTYPE(result, *atypes)((name, dll), tuple(aflags)) 返回 WINFUNCTYPE(结果, *atypes)((name, dll), tuple(aflags))

然后我进一步检查了原型(prototype)设计。看起来,如果您将 ("someParameter",POINTER(aType),2) 传递给 WINFUNCTYPE ,它将创建一个 aType 对象调用并将指向该对象的指针传递给该函数。然后,您可以在返回的元组中访问 aType 对象。这又带来了另一个问题。 cstring 是一个字符数组;因此需要告诉 ctypes 创建一个 c_char 数组。这意味着类似:

GetWindowText = winfunc("GetWindowTextA",windll.user32,c_int,
                  ("hWnd",HWND,1),
                  ("lpString",POINTER(c_char*255),2),
                  ("nMaxCount",c_int,1)
                  )

工作得很好。但不幸的是,ctypes 现在将传递一个指向 cstring 的指针,该指针始终为 255 个字符长,忽略 nMaxCount 指定的大小。

在我看来,我认为没有办法让该函数与定义为输出参数的动态大小的 cstring 一起使用。唯一的可能性似乎就是简单地不使用输出参数功能并将 LPCSTR 定义为输入参数。然后,被调用者需要使用 ctypes.create_string_buffer() 创建自己的缓冲区并将其传递给函数(就像在 C 中一样)。

最佳答案

您必须为输出参数创建一个字符串缓冲区。您可以包装函数以使其透明:

# python3
from ctypes import *

_GetWindowText = WinDLL('user32').GetWindowTextW
_GetWindowText.argtypes = [c_void_p,c_wchar_p,c_int]
_GetWindowText.restype = c_int

def GetWindowText(h):
    b = create_unicode_buffer(255)
    _GetWindowText(h,b,255)
    return b.value

FindWindow = WinDLL('user32').FindWindowW
FindWindow.argtypes = [c_wchar_p,c_wchar_p]
FindWindow.restype = c_void_p

h = FindWindow(None,'Untitled - Notepad')
print(GetWindowText(h))

或者在这种情况下您可以只使用 pywin32 :

import win32gui
h = win32gui.FindWindow(None,'Untitled - Notepad')
print(win32gui.GetWindowText(h))

关于Python ctypes : Prototype with LPCSTR [out] parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10896633/

相关文章:

python - 什么样的不可见参数从内置函数传递到你的函数,如何找出

c# - 观看另一个应用程序,如果它关闭关闭我的应用程序(不轮询)c#

windows - 如何执行Windows身份验证?

c++ - CreateProcess 不遵守命令行

JavaScript 嵌套对象

javascript:从字符串调用对象中的方法

python - 保存一系列命令并将其重新加载到 Python shell 中

python - 运行消息队列的 python 消费者的最佳实践?

python - 如何从唯一列表和 Counter 对象创建计数列表?

c - 错误 C2143 : syntax error : missing ')' before '*'