如何在pywin32程序中使用DllCall函数访问user32.dll?
我正在尝试编写一段代码,以便通过Windows图形界面中的选项卡面板进行访问。为此,我想在我的Python代码中调用user32.dll这个文件。我使用Pywin32来访问AutoIt的功能,我认为我可以调用DllCall,下面是我这样做的方式。
TabID = autoit.DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", hTab)
TabID = autoit.TabID[0]
但是每次运行这段代码时,它都会给我以下错误 -:
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: AutoItX3.Control.DllCall
整个代码看起来是这样的:
import time
import LogIt
import datetime
import win32con
import win32gui
import win32com.client
# this makes use of the AutoITX.dll that we registered with the Windows OS
autoit = win32com.client.Dispatch("AutoItX3.Control")
autoit.DllCall("user32.dll", "int", "GetDlgCtrlID")
有没有人能帮我找出这个错误的原因?另外,我在网上找到的信息也不多。
1 个回答
0
DllCall
在 AutoItX 的函数列表中找不到,所以会出现它不可用的错误。这通常是因为在动态链接库(dll)中实现这个功能比较复杂,就像 HotkeySet
这样的函数一样。AutoItX 是 AutoIt 的一个小项目,如果移植这个功能需要花费很多精力,那通常就不会去做。
另一种选择是实现和 DllCall
类似的机制:
通常在 Windows 系统中,你会使用 GetModuleHandle
来获取 user32.dll 的句柄,然后再用 GetProcAddress
来获取函数的地址。在 Python 中,大多数代码片段都是通过 ctypes 或 win32api 来做到这一点,使用的是 kernel32。