如何使用ctypes调用' AU3_WinGetTitle'在AutoIt?

时间:2014-09-16 03:59:38

标签: python ctypes autoit

我正在尝试使用ctypes为AutoIt创建一个python包装器。

这是我的问题:

e.g。 AU3_WinGetText的原型是:

void AU3_WinGetTitle(LPCWSTR szTitle, LPCWSTR szText, LPWSTR szRetText, int nBufSize);

我正在使用流动代码来调用函数:

import ctypes
from ctypes.wintypes import *

AUTOIT = ctypes.windll.LoadLibrary("AutoItX3.dll")


def win_get_title(title, text="", buf_size=200):
    AUTOIT.AU3_WinGetTitle.argtypes = (LPCWSTR, LPCWSTR, LPWSTR, INT)
    AUTOIT.AU3_WinGetTitle.restypes = None
    rec_text = LPWSTR()
    AUTOIT.AU3_WinGetTitle(LPCWSTR(title), LPCWSTR(text),
                          ctypes.cast(ctypes.byref(rec_text), LPWSTR),
                          INT(buf_size))
    res = rec_text.value
    return res

print win_get_title("[CLASS:Notepad]")

我在运行这些代码后遇到异常:

    res = rec_text.value
ValueError: invalid string pointer 0x680765E0

1 个答案:

答案 0 :(得分:2)

szRetText用于接收输出文本缓冲区

import ctypes
from ctypes.wintypes import *

AUTOIT = ctypes.windll.LoadLibrary("AutoItX3.dll")


def win_get_title(title, text="", buf_size=200):
    # AUTOIT.AU3_WinGetTitle.argtypes = (LPCWSTR, LPCWSTR, LPWSTR, INT)
    # AUTOIT.AU3_WinGetTitle.restypes = None
    rec_text = ctypes.create_unicode_buffer(buf_size)
    AUTOIT.AU3_WinGetTitle(LPCWSTR(title), LPCWSTR(text),
                          rec_text, INT(buf_size))
    res = rec_text.value.rstrip()
    return res

print win_get_title("[CLASS:Notepad]")