有没有办法将 Microsoft C/C++ UIAutomationCore.dll 导入 Python?
Is there a way to import the Microsoft C/C++ UIAutomationCore.dll into Python?
我正在使用 Anaconda3,只能使用默认库。有没有办法访问已经安装的 UIAutomationCore.dll 那是 Windows OS 现在 and/or 其他 'Standard' DLL 的标准并导入它们或包装它们以创建我的在 Python 中拥有自定义模块?我目前在 Windows 10.
上使用 Python 3.7
从某种意义上说。
首先,使用命令提示符安装 Python 模块:
pip install uiautomation
如果需要,您可以通过右键单击 Windows 图标并选择 'Command Prompt (Admin)' 为所有人安装它。
要在 Python 内使用此产品,最需要使用的语句是:
import uiautomation
这将使模块中的所有项目都可用于您的脚本。例如,您可以访问
uiautomation.CalendarControl
使用通常的 Python 语法。
我找到了这个问题的答案。您可以使用 'comtypes.client' 和 'ctypes' 模块导入整个 UIAutomationCore.dll 库和任何其他本机 DLL 或库。对于那些没有 Import/pip 安装限制和我的团队存在的安全问题的人来说,这是一个 link 到已经包装且功能齐全的导入。如果像我一样,你只需要使用 'Native' 默认情况下随 Anacoda3 安装一起提供的导入和库。然后,我建议您使用这段代码开始自己包装它,就像我针对自己的特定需求所做的那样。希望这可以帮助那里的其他人努力使用 UIAutomationCore。
代码:
import os
import sys
from typing import (Any, Callable, Iterable, Tuple, List, Dict) # need pip install typing for Python3.4 or lower
import ctypes
import ctypes.wintypes
import comtypes
import comtypes.client
TreeNode = Any
METRO_WINDOW_CLASS_NAME = 'Windows.UI.Core.CoreWindow' # for Windows 8 and 8.1
SEARCH_INTERVAL = 0.5 # search control interval seconds
MAX_MOVE_SECOND = 0.5 # simulate mouse move or drag max seconds
TIME_OUT_SECOND = 20
OPERATION_WAIT_TIME = 0.5
MAX_PATH = 260
DEBUG_SEARCH_TIME = False
DEBUG_EXIST_DISAPPEAR = False
S_OK = 0
IsNT6orHigher = os.sys.getwindowsversion().major >= 6
ProcessTime = time.perf_counter #this returns nearly 0 when first call it if python version <= 3.6
ProcessTime() # need to call it once if python version <= 3.6
class _AutomationClient:
_instance = None
@classmethod
def instance(cls) -> '_AutomationClient':
"""Singleton instance (this prevents com creation on import)."""
if cls._instance is None:
cls._instance = cls()
return cls._instance
def __init__(self):
try:
self.UIAutomationCore = comtypes.client.GetModule("UIAutomationCore.dll")
self.IUIAutomation = comtypes.client.CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=self.UIAutomationCore.IUIAutomation)
self.ViewWalker = self.IUIAutomation.RawViewWalker
#self.ViewWalker = self.IUIAutomation.ControlViewWalker
except OSError as ex:
Logger.WriteLine('Can not load UIAutomationCore.dll.\nYou may need to install Windows Update KB971513.\nhttps://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation', ConsoleColor.Yellow)
raise ex
#Windows dll
ctypes.windll.user32.GetClipboardData.restype = ctypes.c_void_p
ctypes.windll.user32.GetWindowDC.restype = ctypes.c_void_p
ctypes.windll.user32.OpenDesktopW.restype = ctypes.c_void_p
ctypes.windll.user32.WindowFromPoint.restype = ctypes.c_void_p
ctypes.windll.user32.SendMessageW.restype = ctypes.wintypes.LONG
ctypes.windll.user32.GetForegroundWindow.restype = ctypes.c_void_p
ctypes.windll.user32.GetWindowLongW.restype = ctypes.wintypes.LONG
ctypes.windll.kernel32.GlobalLock.restype = ctypes.c_void_p
ctypes.windll.kernel32.GlobalAlloc.restype = ctypes.c_void_p
ctypes.windll.kernel32.GetStdHandle.restype = ctypes.c_void_p
ctypes.windll.kernel32.OpenProcess.restype = ctypes.c_void_p
ctypes.windll.kernel32.CreateToolhelp32Snapshot.restype = ctypes.c_void_p
class _DllClient:
_instance = None
@classmethod
def instance(cls) -> '_DllClient':
"""Singleton instance (this prevents com creation on import)."""
if cls._instance is None:
cls._instance = cls()
return cls._instance
def __init__(self):
binPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "bin")
os.environ["PATH"] = binPath + os.pathsep + os.environ["PATH"]
load = False
if sys.maxsize > 0xFFFFFFFF:
try:
self.dll = ctypes.cdll.UIAutomationClient_VC140_X64
load = True
except OSError as ex:
pass
else:
try:
self.dll = ctypes.cdll.UIAutomationClient_VC140_X86
load = True
except OSError as ex:
pass
if load:
self.dll.BitmapCreate.restype = ctypes.c_size_t
self.dll.BitmapFromWindow.argtypes = (ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int)
self.dll.BitmapFromWindow.restype = ctypes.c_size_t
self.dll.BitmapFromFile.argtypes = (ctypes.c_wchar_p, )
self.dll.BitmapFromFile.restype = ctypes.c_size_t
self.dll.BitmapToFile.argtypes = (ctypes.c_size_t, ctypes.c_wchar_p, ctypes.c_wchar_p)
self.dll.BitmapRelease.argtypes = (ctypes.c_size_t, )
self.dll.BitmapGetWidthAndHeight.argtypes = (ctypes.c_size_t, )
self.dll.BitmapGetPixel.argtypes = (ctypes.c_size_t, ctypes.c_int, ctypes.c_int)
self.dll.BitmapSetPixel.argtypes = (ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_uint)
self.dll.Initialize()
else:
self.dll = None
Logger.WriteLine('Can not load dll.\nFunctionalities related to Bitmap are not available.\nYou may need to install Microsoft Visual C++ 2010/2015 Redistributable Package.', ConsoleColor.Yellow)
def __del__(self):
if self.dll:
self.dll.Uninitialize()
我正在使用 Anaconda3,只能使用默认库。有没有办法访问已经安装的 UIAutomationCore.dll 那是 Windows OS 现在 and/or 其他 'Standard' DLL 的标准并导入它们或包装它们以创建我的在 Python 中拥有自定义模块?我目前在 Windows 10.
上使用 Python 3.7从某种意义上说。
首先,使用命令提示符安装 Python 模块:
pip install uiautomation
如果需要,您可以通过右键单击 Windows 图标并选择 'Command Prompt (Admin)' 为所有人安装它。
要在 Python 内使用此产品,最需要使用的语句是:
import uiautomation
这将使模块中的所有项目都可用于您的脚本。例如,您可以访问
uiautomation.CalendarControl
使用通常的 Python 语法。
我找到了这个问题的答案。您可以使用 'comtypes.client' 和 'ctypes' 模块导入整个 UIAutomationCore.dll 库和任何其他本机 DLL 或库。对于那些没有 Import/pip 安装限制和我的团队存在的安全问题的人来说,这是一个 link 到已经包装且功能齐全的导入。如果像我一样,你只需要使用 'Native' 默认情况下随 Anacoda3 安装一起提供的导入和库。然后,我建议您使用这段代码开始自己包装它,就像我针对自己的特定需求所做的那样。希望这可以帮助那里的其他人努力使用 UIAutomationCore。
代码:
import os
import sys
from typing import (Any, Callable, Iterable, Tuple, List, Dict) # need pip install typing for Python3.4 or lower
import ctypes
import ctypes.wintypes
import comtypes
import comtypes.client
TreeNode = Any
METRO_WINDOW_CLASS_NAME = 'Windows.UI.Core.CoreWindow' # for Windows 8 and 8.1
SEARCH_INTERVAL = 0.5 # search control interval seconds
MAX_MOVE_SECOND = 0.5 # simulate mouse move or drag max seconds
TIME_OUT_SECOND = 20
OPERATION_WAIT_TIME = 0.5
MAX_PATH = 260
DEBUG_SEARCH_TIME = False
DEBUG_EXIST_DISAPPEAR = False
S_OK = 0
IsNT6orHigher = os.sys.getwindowsversion().major >= 6
ProcessTime = time.perf_counter #this returns nearly 0 when first call it if python version <= 3.6
ProcessTime() # need to call it once if python version <= 3.6
class _AutomationClient:
_instance = None
@classmethod
def instance(cls) -> '_AutomationClient':
"""Singleton instance (this prevents com creation on import)."""
if cls._instance is None:
cls._instance = cls()
return cls._instance
def __init__(self):
try:
self.UIAutomationCore = comtypes.client.GetModule("UIAutomationCore.dll")
self.IUIAutomation = comtypes.client.CreateObject("{ff48dba4-60ef-4201-aa87-54103eef594e}", interface=self.UIAutomationCore.IUIAutomation)
self.ViewWalker = self.IUIAutomation.RawViewWalker
#self.ViewWalker = self.IUIAutomation.ControlViewWalker
except OSError as ex:
Logger.WriteLine('Can not load UIAutomationCore.dll.\nYou may need to install Windows Update KB971513.\nhttps://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation', ConsoleColor.Yellow)
raise ex
#Windows dll
ctypes.windll.user32.GetClipboardData.restype = ctypes.c_void_p
ctypes.windll.user32.GetWindowDC.restype = ctypes.c_void_p
ctypes.windll.user32.OpenDesktopW.restype = ctypes.c_void_p
ctypes.windll.user32.WindowFromPoint.restype = ctypes.c_void_p
ctypes.windll.user32.SendMessageW.restype = ctypes.wintypes.LONG
ctypes.windll.user32.GetForegroundWindow.restype = ctypes.c_void_p
ctypes.windll.user32.GetWindowLongW.restype = ctypes.wintypes.LONG
ctypes.windll.kernel32.GlobalLock.restype = ctypes.c_void_p
ctypes.windll.kernel32.GlobalAlloc.restype = ctypes.c_void_p
ctypes.windll.kernel32.GetStdHandle.restype = ctypes.c_void_p
ctypes.windll.kernel32.OpenProcess.restype = ctypes.c_void_p
ctypes.windll.kernel32.CreateToolhelp32Snapshot.restype = ctypes.c_void_p
class _DllClient:
_instance = None
@classmethod
def instance(cls) -> '_DllClient':
"""Singleton instance (this prevents com creation on import)."""
if cls._instance is None:
cls._instance = cls()
return cls._instance
def __init__(self):
binPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "bin")
os.environ["PATH"] = binPath + os.pathsep + os.environ["PATH"]
load = False
if sys.maxsize > 0xFFFFFFFF:
try:
self.dll = ctypes.cdll.UIAutomationClient_VC140_X64
load = True
except OSError as ex:
pass
else:
try:
self.dll = ctypes.cdll.UIAutomationClient_VC140_X86
load = True
except OSError as ex:
pass
if load:
self.dll.BitmapCreate.restype = ctypes.c_size_t
self.dll.BitmapFromWindow.argtypes = (ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int)
self.dll.BitmapFromWindow.restype = ctypes.c_size_t
self.dll.BitmapFromFile.argtypes = (ctypes.c_wchar_p, )
self.dll.BitmapFromFile.restype = ctypes.c_size_t
self.dll.BitmapToFile.argtypes = (ctypes.c_size_t, ctypes.c_wchar_p, ctypes.c_wchar_p)
self.dll.BitmapRelease.argtypes = (ctypes.c_size_t, )
self.dll.BitmapGetWidthAndHeight.argtypes = (ctypes.c_size_t, )
self.dll.BitmapGetPixel.argtypes = (ctypes.c_size_t, ctypes.c_int, ctypes.c_int)
self.dll.BitmapSetPixel.argtypes = (ctypes.c_size_t, ctypes.c_int, ctypes.c_int, ctypes.c_uint)
self.dll.Initialize()
else:
self.dll = None
Logger.WriteLine('Can not load dll.\nFunctionalities related to Bitmap are not available.\nYou may need to install Microsoft Visual C++ 2010/2015 Redistributable Package.', ConsoleColor.Yellow)
def __del__(self):
if self.dll:
self.dll.Uninitialize()