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

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

module 'comtypes.gen.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 https://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation
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:\Windows\System32
>regsvr32 UIAutomationCore.dll
  • 由于生产环境不联外网,不好远程调试。本地开发环境又复现不出该问题,
    所以当时,虽然无奈地将原因归结于未知的系统原因,但只能先弃用有报错的几台WIN10系统,换别的系统进行业务操作。
  • 直到现在,这个问题过去了有将近半年的时间,终于在开发环境上复现了!
  • 初步定位,是在执行control = uiautomation.ControlFromPoint()的语句时出错,于是在此处捕获异常后打印堆栈信息如下:
Traceback (most recent call last):
  File "test_hook.py", line 112, in get_mouse
  File "uiautomation\uiautomation.py", line 8164, in ControlFromPoint
  File "uiautomation\uiautomation.py", line 52, in instance
  File "uiautomation\uiautomation.py", line 71, in __init__
  File "uiautomation\uiautomation.py", line 60, in __init__
AttributeError: module 'comtypes.gen.UIAutomationClient' has no attribute 'IUIAutomation'
  • 然后根据堆栈信息找到出错位置对应的uiautomation.py源码
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):
        tryCount = 3
        for 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.ControlViewWalker
                break
            except 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 https://github.com/yinkaisheng/WindowsUpdateKB971513ForIUIAutomation
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 'comtypes.gen._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 'comtypes.gen._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 'comtypes.gen._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 'comtypes.gen._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:\Users\Ow\AppData\Local\Temp\comtypes_cache\main-37后发现,其中的_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py文件为空,对比python3库中安装的comtypes源码,不难发现,正是因为该文件中缺少IUIAutomation类,才报错AttributeError: module ‘comtypes.gen.UIAutomationClient’ has no attribute ‘IUIAutomation’

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

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

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

未完,待续。。。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
当我们遇到"c ntdll.dll 报错"时,通常是指在Windows操作系统中发生了一个名为"ntdll.dll"的系统动态链接库的错误。 ntdll.dll(NT Layer DLL)是一个非常重要的系统文件,它包含了Windows操作系统的核心函数和服务。该文件的主要作用是为应用程序和系统的交互提供支持和功能。当出现"c ntdll.dll 报错"时,可能意味着该动态链接库文件已被损坏,丢失或与其他程序发生冲突等问题。 出现这种错误的原因可能有很多种,以下是一些常见的原因和解决方法: 1. 病毒或恶意软件感染:某些恶意软件可能会修改或破坏ntdll.dll文件。我们可以运行一个杀毒软件扫描系统,检查并清除系统中的恶意软件。 2. 操作系统损坏:操作系统本身的问题可能导致ntdll.dll报错。我们可以尝试使用Windows系统自带的系统文件检查工具修复系统文件。 3. 应用程序冲突:某些应用程序可能与ntdll.dll文件发生冲突导致错误。在这种情况下,我们可以卸载或更新相关的应用程序,或者尝试通过更改应用程序的兼容性设置来解决问题。 4. 硬件问题:某些硬件故障,如内存损坏或磁盘错误,也可能导致c ntdll.dll 报错。我们可以通过运行硬件诊断工具来检查并修复硬件问题。 综上所述,当我们遇到"c ntdll.dll 报错"时,我们可以首先运行杀毒软件检查恶意软件,然后使用系统文件检查工具修复系统文件,卸载或更新可能引起冲突的应用程序,并检查和修复可能的硬件故障。如果问题仍然存在,可以考虑联系专业技术支持以获取更进一步的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值