4

我的 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,因为我已经插入print self._namectypes.py.

该应用程序运行我需要的大多数 Python 脚本,除了那些加载pyreadline.

这些相同的脚本从已安装的 Python 可执行文件运行没有问题。

这可能是什么原因?

更新 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 而我的应用程序不能。我很困惑。

4

2 回答 2

5

更新!
出现问题是因为系统尝试从不同的来源加载 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>

比我在项目属性/清单工具/输入和输出/附加清单文件中设置此文件
您在处理器架构 =“ amd64 ”中的清单中有错误。
项目重建后工作正常。

于 2013-11-30T21:49:22.500 回答
0

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

于 2018-11-08T03:45:43.297 回答