我希望能够使用Python从位于 C:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll 中的 AutoIt 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 必须猜测应该转换成哪种类型,如果猜错了,程序就会崩溃。 - abarnert