我的 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._name
了ctypes.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 而我的应用程序不能。我很困惑。