【发布时间】:2013-11-25 11:20:20
【问题描述】:
我想从一个 AutoIt dll 调用函数,我使用 Python 在 C:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll 中找到了该函数。我知道我可以使用win32com.client.Dispatch("AutoItX3.Control"),但我无法安装应用程序或在系统中注册任何内容。
到目前为止,这是我所在的位置:
from ctypes import *
path = r"C:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll"
autoit = windll.LoadLibrary(path)
以下是有效的方法:
autoit.AU3_WinMinimizeAll() # windows were successfully minimized.
autoit.AU3_Sleep(1000) # sleeps 1 sec.
这是我的问题,当我调用像这样的其他方法时,python 崩溃了。我从 Windows 中得到 python.exe 已停止工作...
autoit.AU3_WinGetHandle('Untitled - Notepad', '')
还有一些其他方法不会使 python 崩溃,但只是不起作用。这个不关闭窗口返回0:
autoit.AU3_WinClose('Untitled - Notepad', '')
这个另一个返回 1 但窗口仍然最小化:
autoit.AU3_WinActivate('Untitled - Notepad', '')
我已经使用Dispatch("AutoItX3.Control") 测试了这些示例,并且一切正常。
似乎应该返回字符串以外的东西的方法正在崩溃 python。但是,像WinClose 这样的其他人甚至都没有工作......
提前感谢您的帮助!
编辑:
这些方法现在在使用 unicode 字符串时有效:
autoit.AU3_WinClose(u'Untitled - Notepad', u'')
autoit.AU3_WinActivate(u'Untitled - Notepad', u'')
我找到了AU3_WinGetHandle的原型:
AU3_API void WINAPI AU3_WinGetHandle(const char szTitle, /[in,defaultvalue("")]*/const char *szText, char *szRetText, int nBufSize);
现在我可以使用以下代码检索返回值!
from ctypes.wintypes import LPCWSTR
s = LPCWSTR(u'')
print AU3_WinGetHandle(u'Untitled - Notepad', u'', s, 100) # prints 1
print s.value # prints '000705E0'!
感谢帮助过我的人!
【问题讨论】:
-
你知道这些函数的正确原型吗?如果是这样,您应该为每个函数设置
argtypes和restype,而不仅仅是调用它。这样,如果 Python 可以将你的参数转换为正确的类型,它就会,否则它会给你一个错误。你这样做的方式,Python 必须猜测它应该转换成哪种类型,如果它猜错了,你就会崩溃。