【问题标题】:Python cannot open msvcr90.dllPython 无法打开 msvcr90.dll
【发布时间】:2013-11-24 13:37:39
【问题描述】:

我的 Windows 应用程序嵌入了 Python 2.6(我知道这是旧的,但这是我们必须使用的)。它可以运行基本的 Python 命令,但尝试执行失败

import ctypes
ctypes.WinDLL("msvcr90.dll")

我收到错误 126“找不到 DLL”。如果我将 DLL 植入应用程序可以找到的位置,则会收到错误 1114“DLL 初始化例程失败”。

更新这可以用这个最简单的程序重现:

#include <math.h>
#include <iostream>
#undef _DEBUG
#include <Python.h>

int main(int argc, char* argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PyRun_SimpleString("import pyreadline\n");
    Py_Finalize();
    std::cout << "Press enter: " << std::endl;
    char c;
    std::cin.read(&c, 1);
    return 0;
}

在 x86 和 amd64 架构中使用 V9 或 v10 工具链编译时,此操作会失败。

回溯如下:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python26-x86\lib\site-packages\pyreadline\__init__.py", line 9, in <m
odule>
    import unicode_helper, logger, clipboard, lineeditor, modes, console
  File "C:\Python26-x86\lib\site-packages\pyreadline\console\__init__.py", line
14, in <module>
    from console import *
  File "C:\Python26-x86\lib\site-packages\pyreadline\console\console.py", line 6
05, in <module>
    msvcrt = cdll.LoadLibrary(ctypes.util.find_msvcrt())
  File "C:\Python26-x86\Lib\ctypes\__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "C:\Python26-x86\Lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found    
  -- or alternatively --
WindowsError: [Error 1114] A dynamic link library (DLL) initialization routine f
ailed

我知道正在加载的 DLL 是 msvcr90.dll,因为我在 ctypes.py 中插入了 print self._name

该应用程序运行我需要的大多数 Python 脚本,但加载 pyreadline 的脚本除外。

这些相同的脚本可以毫无问题地从已安装的 Python 可执行文件中运行。

这可能是什么原因?

UPDATED 2 简单的LoadLibrary("msvcr90.dll") 也失败了。我已将 DLL 添加到应用程序清单中,如 'net. 这没有帮助。这是嵌入在可执行文件中的清单:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="amd64" publicKeyToken="1fc8b3b9a1e18e3b" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>

这个清单和嵌入在 python.exe 中的清单确实匹配,但是 python.exe 可以打开 DLL 而我的应用程序不能。我很困惑。

【问题讨论】:

  • 您的 Windows 应用程序是用什么版本的 C/C++ 构建的?
  • @martineau vs2010(工具链 v10.0,msvcr100.dll)
  • 我认为这可能是您的问题的原因——您实际上是在尝试创建一个同时使用两个不同 C/C++ 运行时库的程序。顺便说一句,对于那个旧版本的 Python,我能想到的唯一解决方案是尝试使用 VS2010 自己重建它。
  • @martineau 使用两个运行时绝对没有问题,只要它们不混合(即,你不为一个运行时分配内存而另一个运行时空闲)。事实上,我的应用程序几乎可以运行我需要的所有 Python 命令。它唯一不能做的是加载 pyreadline,它会尝试加载 msvcr90.dll。
  • @martineau 即使使用 v90 工具链编译也会失败,请参阅更新。

标签: python dll python-2.6 msvcr90.dll


【解决方案1】:

更新了!
出现问题是因为系统尝试从不同的来源加载 msvcr90.dll。首先是应用程序启动时,然后是 python 脚本启动。
要解决这个问题,您确实应该在应用程序中放置一个正确的清单文件。
我在项目的根目录中创建了一个 added.manifest 文件,内容如下:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b" ></assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>

比我在项目属性/清单工具/输入和输出/附加清单文件中设置此文件
您在 processorArchitecture="amd64" 的清单中有错误。
重建后项目运行良好。

【讨论】:

  • Python.exe 可以运行 pyreadline 而我的程序不能。该错误绝对不在 pyreadline 中。
  • 我同时构建了 x86 和 amd64 版本。我刚刚查了一下,x86 版本引用processorArchitecture="x86",amd64 版本引用processorArchitecture="amd64"。仍然两个版本都不起作用。
  • 您可以尝试将清单嵌入到应用程序中吗?就像我写的那样。
  • 你也可以尝试在这里下载我的测试项目:kurilo.su/stackoverflow/20175129-pytest.7z
  • 谢谢。通过嵌入您的确切清单,我设法使 x86 版本正常工作。调试版本需要调试 CRT 作为附加依赖,否则将无法启动。但是 amd64(我真正感兴趣的)仍然不起作用(我已将清单中的处理器架构更改为 amd64)。
【解决方案2】:

我通过重新安装 python 2.7 为另一个 Python 应用程序解决了 Windows 10 上的相同错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 2020-09-02
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多