首页 > 编程问答 >Python ctypes OSError:[WinError 1114]动态链接库(DLL)初始化例程失败

Python ctypes OSError:[WinError 1114]动态链接库(DLL)初始化例程失败

时间:2024-07-24 16:50:07浏览次数:12  
标签:python c++ ctypes

我试图使用Python中的 ctypes 库调用C++函数:

test.py

from ctypes import *
from random import randint

tester = cdll.LoadLibrary('./test.dll')
print(tester.test(randint(1, 100)))

test.cpp

#include <vector>

int cppTest(int num) {
    std::vector<int> v;
    v.push_back(num);
    return v.back();
}

extern "C" __declspec(dllexport) int test(int num) {
    return cppTest(num);
}

然后执行以下命令:

> g++ -o test.dll -shared -fPIC test.cpp
> python test.py

并且我收到以下错误:|| |我认为使用任何 C++ STL 库都会导致问题,所以我尝试使用

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    tester = cdll.LoadLibrary('./test.dll')
  File "D:\Programs\Python\Python38\lib\ctypes\__init__.py", line 451, in LoadLibrary
    return self._dlltype(name)
  File "D:\Programs\Python\Python38\lib\ctypes\__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 1114] Dynamic-link library (DLL) initialization routine failed.

并且再次出现相同的错误。 new int 我的环境:

Windows 10 x64

  • Python 3.8.5 x64(与Python 3.6同样的问题)
  • TDM-GCC 9.2.0
  • 有人能帮我解决这个问题吗?

编辑:

编译的 文件是64位的,通过运行检查VS 2017 上的以下命令: dll 编辑 2:

E:\...\test>dumpbin /headers test.dll
Microsoft (R) COFF/PE Dumper Version 14.16.27044.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file test.dll

PE signature found

File Type: DLL

FILE HEADER VALUES
            8664 machine (x64)
              13 number of sections
        612A415D time date stamp Sat Aug 28 21:59:57 2021
          10B000 file pointer to symbol table
            6FA9 number of symbols
              F0 size of optional header
            2026 characteristics
                   Executable
                   Line numbers stripped
                   Application can handle large (>2GB) addresses
                   DLL

当我尝试使用 MinGW 作为编译器时,这里再次出现错误: When I tried to use MinGW as the compiler, here again was an error:

E:\...\test>python test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    tester = cdll.LoadLibrary('./test.dll')
  File "D:\Programs\Python\Python38\lib\ctypes\__init__.py", line 451, in LoadLibrary
    return self._dlltype(name)
  File "D:\Programs\Python\Python38\lib\ctypes\__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'E:\...\test\test.dll' (or one of its dependencies). Try using the full path with constructor syntax.

错误“OSError: [WinError 1114] 动态链接库 (DLL) 初始化例程失败”通常在尝试加载与 Python 解释器不兼容的 DLL(例如,32 位 DLL 与 64 位 Python)时发生,或者 DLL 依赖于无法找到的其他 DLL。

但是,已经检查了 DLL 的体系结构,它看起来是正确的。让我们专注于确保正确配置 MinGW 并找到所有依赖项:

1. 使用 MinGW 进行编译

  • 下载并安装 MinGW-w64: 如果还没有,请从 https://www.mingw-w64.org/ 下载 MinGW-w64 的最新安装程序并安装。 确保选择与的 Python 安装(64 位)匹配的正确架构。
  • 将 MinGW-w64 bin 文件夹添加到的 PATH: 将 MinGW-w64 bin 文件夹的路径(例如, C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin )添加到的系统环境变量 PATH 中。这将允许 Python 找到 g++ 编译器。
  • 使用 MinGW-w64 编译 DLL: 打开命令提示符或 PowerShell 并使用以下命令编译的 C++ 代码:
g++ -c -fPIC test.cpp -o test.o
g++ -shared -o test.dll test.o -Wl,--add-stdcall-alias

2. 依赖项

  • 依赖项行者: 使用 Dependency Walker ( http://www.dependencywalker.com/ ) 等工具检查 test.dll 的任何依赖项。这将显示 DLL 依赖的任何其他 DLL,并且需要确保这些 DLL 可用并且位于系统可以找到它们的位置(例如,与 test.dll 在同一目录中,或在的系统 PATH 中)。
  • C++ 运行时: 的 DLL 可能依赖于 Microsoft C++ 运行时库。可能需要在用于运行 Python 脚本的同一环境中安装相应的运行时可再发行组件包。

3. 测试 Python 代码

test.dll 和任何必要的依赖项放在与的 Python 脚本相同的目录中,或位于系统 PATH 中的目录中,然后再次运行的 Python 脚本。

如果在遵循这些步骤后问题仍然存在,请提供有关遇到的特定错误消息的更多信息,包括来自 Dependency Walker 的任何输出,以便我进一步帮助解决问题。

标签:python,c++,ctypes
From: 68965141

相关文章

  • C++ opencv putText
    C++opencv putText  #include<opencv2/opencv.hpp>intmain(){//创建一个空白图像cv::Matimg(400,400,CV_8UC3,cv::Scalar(255,255,255));//设置文本内容std::stringtext="Hello,OpenCV!";//设置文本起始坐标(左下角坐标)......
  • Python 的分布式锁管理器
    我有一堆具有多个实例的服务器,这些实例访问的资源对每秒的请求有硬性限制。我需要一种机制来锁定所有正在运行的服务器和实例对此资源的访问。有我在github上找到的一个restful分布式锁管理器:https://github.com/thefab/restful-distributed-lock-manager不幸......
  • 如何在Python中提示self变量的类型
    通常不需要关心Python类成员函数中的self变量,但我正在实现一个装饰器,看起来像defextractor(depends:List[Text]=None,provides:List[Text]=None)->Callable[[ExtractorFunction],Extracto......
  • 将 Python 类型提示限制为元组中的值
    我的方法之一采用status中使用的filter()参数。此参数与定义如下的模型字段相关:STATUS_CHOICES=((1,_("draft")),(2,_("private")),(3,_("published")),)classMyModel(Model):status=models.PositiveSmallIntegerFi......
  • 在python中查找区间数据的中位数
    我正在探索不同的python库,我想知道如何找到分组数据集的近似中值。这里有一个表格供参考。年龄频率1-1012310-203502......
  • 比较Python中的字符串统一特殊字符
    也许我可以使用更好的英语,但我想要的是忽略单词中的重音(和类似的),所以:renè、rené、rene'和rene应该是相同的,所以应该mañana和manana或even-distribuited和evendistribuited,可能还有sho......
  • 如何使用 Python 脚本从客户账单电子邮件中获取订单 ID - WooCommerce API
    我想创建一个python脚本,返回只知道客户的账单电子邮件的订单。我尝试这样做,但返回所有最近的订单:fromwoocommerceimportAPIwcapi=API(url="https://siteexample.com",consumer_key="ck_xxx",consumer_secret="cs_xxx",version="wc/v3")......
  • python基础理论小总结
    1.python语言的特性Python是一门解释型语言,简单清晰,开源免费,跨平台,有大量第三方库辅助开发,支持面向对象与自动垃圾回收,方便与其他编程语言相互调用。Python在数据采集、人工智能、WEB后台开发、自动化运维、测试等方向应用广泛。2.解释型语言和编译型语言的区别执行方式不......
  • python编码规范
    本篇讲的是代码格式化的问题,解决格式化的方法在最下方,不想看内容的,滑到最下方就好了。一、变量的命名规则1.组成:字母、数字、下划线2.不可以以数字开头3.不建议使用下划线开头4.命名需见名知意5.不要与关键字重名。如何查找所有关键字?importkeywordprint(keyword.k......
  • Python爬虫开发中的常用库与框架安装指南
    在Python爬虫开发中,选择合适的库和框架可以大大提高开发效率和爬虫的性能。本文将介绍一些常用的解析库、请求库、储存库、Web库、App爬取库以及爬虫框架,并展示如何使用pip命令进行安装。一、解析库1.BeautifulSoupBeautifulSoup是一个用于从HTML或XML文件中提取数据的Pyth......