我正在尝试使用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
答案 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]")