Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用ctypes调用';AU3_Wingtitle和#x27;在汽车里?_Python_Ctypes_Autoit - Fatal编程技术网

Python 如何使用ctypes调用';AU3_Wingtitle和#x27;在汽车里?

Python 如何使用ctypes调用';AU3_Wingtitle和#x27;在汽车里?,python,ctypes,autoit,Python,Ctypes,Autoit,我正在尝试使用ctypes为AutoIt制作python包装器 我的问题是: e、 g.AU3_WinGetText的原型为: void AU3_wingtitle(LPCWSTR szTitle、LPCWSTR szText、LPWSTR szRetText、int nBufSize) 我使用流动代码调用函数: import ctypes from ctypes.wintypes import * AUTOIT = ctypes.windll.LoadLibrary("AutoItX3.dl

我正在尝试使用ctypes为AutoIt制作python包装器

我的问题是:

e、 g.AU3_WinGetText的原型为:

void AU3_wingtitle(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

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]")

您是否需要
演员阵容
?如果第三个参数只是
rec_text
(或
ctypes.byref(rec_text)
),会发生什么情况?我已经解决了问题:)走吧!现在,为了帮助其他人做同样的事情,你应该在一个答案中发布你的解决方案,并将其标记为自己回答:)更简单的DLL加载:
AUTOIT=ctypes.windell('AutoItX3')
。Windows在库名称后附加“.dll”。为了提高效率,只定义一次
argtypes
restype
(不是复数
restypes
),即不在函数中:
AUTOIT.AU3_wingtitle.restype=None;AUTOIT.AU3_wingtitle.argtypes=(wintypes.LPCWSTR、wintypes.LPCWSTR、wintypes.LPWSTR、wintypes.INT)
。创建输出缓冲区的另一种方法:
rec_text=(ctypes.c_wchar*buf_size)(
)。使用
argtypes
调用只需
AUTOIT.AU3_wingtitle(title、text、rec_text、buf_size)
。嘿,这是我的答案;)