【发布时间】: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