解决无法加载UIAutomationCore.dll的报错

阅读: 评论:0

解决无法加载UIAutomationCore.dll的报错

解决无法加载UIAutomationCore.dll的报错

前言: 此问题是在Python开发环境下关于UIAutomation的报错问题,问题来源于生产环境中的几台WIN10系统报错。

module &#UIAutomationClient' has no attribute 'IUIAutomation'
Can not load UIAutomationCore.dll.
1, You may need to install Windows Update KB971513 if your OS is Windows XP, see 
2, you need to use an UIAutomationInitializerInThread object if use uiautomation in a thread, see demos/uiautomation_in_thread.py
  • 因为在生产环境中出现,当时是怀疑是UIAutomationCore.dll在系统中没有生效,所以尝试过如下的方法(以管理员身份执行如下cmd命令),但始终没有效果,该报错还是报错;也怀疑过WIN10上缺少KB971513的补丁,为此还特地找过对应的补丁安装,但是该补丁只适用于Windows XP系统,在安装的时候存在类似“当前系统无法安装”的提示。
>cd C:WindowsSystem32
>regsvr32 UIAutomationCore.dll
  • 由于生产环境不联外网,不好远程调试。本地开发环境又复现不出该问题,
    所以当时,虽然无奈地将原因归结于未知的系统原因,但只能先弃用有报错的几台WIN10系统,换别的系统进行业务操作。
  • 直到现在,这个问题过去了有将近半年的时间,终于在开发环境上复现了!
  • 初步定位,是在执行control = uiautomation.ControlFromPoint()的语句时出错,于是在此处捕获异常后打印堆栈信息如下:
Traceback (most recent call last):File "test_hook.py", line 112, in get_mouseFile "uiautomationuiautomation.py", line 8164, in ControlFromPointFile "uiautomationuiautomation.py", line 52, in instanceFile "uiautomationuiautomation.py", line 71, in __init__File "uiautomationuiautomation.py", line 60, in __init__
AttributeError: module &#UIAutomationClient' has no attribute 'IUIAutomation'
  • 然后根据堆栈信息找到出错位置对应的uiautomation.py源码
class _AutomationClient:_instance = None@classmethoddef instance(cls) -> '_AutomationClient':"""Singleton instance (this prevents com creation on import)."""if cls._instance is None:cls._instance = cls()return cls._instancedef __init__(self):tryCount = 3for retry in range(tryCount):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.ControlViewWalkerbreakexcept Exception as ex:if retry + 1 == tryCount:Logger.WriteLine('''
{}
Can not load UIAutomationCore.dll.
1, You may need to install Windows Update KB971513 if your OS is Windows XP, see 
2, you need to use an UIAutomationInitializerInThread object if use uiautomation in a thread, see demos/uiautomation_in_thread.py'''.format(ex), ConsoleColor.Yellow)raise ex
  • 显然,最初的报错如上,是通过日志的形式输出的。但是,上述错误是在打包后的软件中出现,在PyCharm的运行过程中并没有出现。
  • 然后觉得奇怪,会不会开发模式和打包模式下所加载的UIAutomationCore.dll不是同一个?
  • 于是乎,带着疑问进行对比验证,直接在命令行中试了下如下python语句
>>> import comtypes.client
>>> core = comtypes.client.GetModule("UIAutomationCore.dll")
>>> core
<module &#_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'D:\DevelopTools\Python\Python37\lib\site-packages\comtypes\gen\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
  • 接着,又在uiautomation.py源码_AutomationClient类__init__方法中,添加一句输出语句,将self.UIAutomationCore控制台输出,然后打包软件,重新运行,得到如下结果:
<module &#_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
<module &#_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
<module &#_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0' from 'C:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'>
  • 符合源码逻辑,循环了三次。

  • 重点是,对比验证有了结果!两者加载的UIAutomationCore.dll确实不是同一处,然后找到本地目录C:UsersOwAppDataLocalTempcomtypes_cachemain-37后发现,其中的_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py文件为空,对比python3库中安装的comtypes源码,不难发现,正是因为该文件中缺少IUIAutomation类,才报错AttributeError: module ‘UIAutomationClient’ has no attribute ‘IUIAutomation’

  • 既然知道出错的原因,那么就容易对症下药,根治问题。要么将comtypes_cache目录整个删掉,要么将UIAutomationClient import IUIAutomation手动添加到项目源码中去。

  • 至此,项目问题已解决。但是还有一系列值得思考的问题,就是为何会产生comtypes_cache目录?又为啥项目软件加载的UIAutomationCore.dll会优先找到本地临时目录中?看了comtypes_cache目录的生成日期就是昨天,是因何事件,临时目录中会写入comtypes_cache?

  • 暂时未知,有时间可以研究一下。

未完,待续。。。

本文发布于:2024-02-01 20:04:01,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170678903939114.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:报错   加载   UIAutomationCore   dll
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23