python调dll时报FileNotFoundError: Could not find module ‘xxx.dll‘(or one of its dependencies)的解决方案

如题所述, 在windows下,python调用dll文件时,无论是使用ctypes.cdll.LoadLibrary(`xxx.dll`),还是使用ctypes.CDLL('xxx.dll'),均报如下错误:

FileNotFoundError: Could not find module ‘xxx.dll’ (or one of its dependencies). Try using the full path with constructor syntax.

有文章说是因为加载的dll缺少依赖库,使用VS的dumpbin.exe查看以后,发现依赖的dll都存在,所以博主遇到的应该不是此类问题。

dumpbin的使用方式如下:

dumpbin.exe /dependents xxx.dll

还有文章说是dll路径不正确,先使用os.add_dll_directory将路径加载进来,结果发现也不生效。。。

后来发现,使用ctypes.CDLL('xxx.dll',winmode=0)可以完美解决该问题。

原因是因为python3.8及以上版本的dll搜索机制变更导致的,目前暂时的解决方法:使用CDLL方法加载dll并设置参数winmode=0


参考链接:

  • https://github.com/ynyyn/Miniblink-Python-SimpleDemo/issues/4
  • https://blog.csdn.net/weixin_43980331/article/details/132556194
  • https://blog.csdn.net/echoszf/article/details/131944250
  • 作者:罗伯特祥

    物联沃分享整理
    物联沃-IOTWORD物联网 » python调dll时报FileNotFoundError: Could not find module ‘xxx.dll‘(or one of its dependencies)的解决方案

    发表回复