`
Zhongwei_leg
  • 浏览: 552088 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

如何用 C++ 为 Python 写 dll

    博客分类:
  • C++
阅读更多

1. 先新建一个名为 hello.cpp 的 C++ 源文件:

#include <stdio.h>

#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT int __stdcall hello()
{
    printf("Hello world!\n");
    return 0;
}

 


2. 编译成 dll 文件:

cl /LD hello.cpp
 


注意, 这里的参数是 /LD, 而不是 /DL。


3. 编写一个名为 hello.py 的 python 文件:

# coding: utf-8

import os
import ctypes

CUR_PATH = os.path.dirname(__file__)

if __name__ == '__main__':
    print 'starting...'
    dll = ctypes.WinDLL(os.path.join(CUR_PATH, 'hello.dll'))
    dll.hello()

 


4. 输出为:

 

starting...
Hello world!

 


需要注意的地方:
1. C++ 的 dll 中的接口函数必须以 extern "C" __declspec(dllexport) 为前缀, C 的以 __declspec(dllexport) 为前缀。
否则会报错:

 

Traceback (most recent call last):
  File "hello.py", line 12, in <module>
    dll.hello()
  File "C:\Python25\lib\ctypes\__init__.py", line 361, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python25\lib\ctypes\__init__.py", line 366, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'hello' not found

 


2. 是使用 ctypes.CDLL 还是 ctypes.WinDLL 呢?
反正我是分不清,所以在 dll 中的接口函数中显式地加上了 __stdcall. 似乎一个是 caller 清栈,另外一个是 callee 清。

MSDN 中关于 __stdcall, __cdecl 的介绍:
__cdecl:
http://msdn.microsoft.com/en-us/library/zkwh89ks(VS.80).aspx
This is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code. The following list shows the implementation of this calling convention.

__stdcall:
http://msdn.microsoft.com/en-us/library/zxk0tw93(VS.71).aspx
The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl.

参考文档:
1. 《How to write a DLL/SO in C/C++ for Python》
http://wolfprojects.altervista.org/dllforpyinc.php

2. 《Python调用windows下DLL详解 - ctypes库的使用》
http://blog.csdn.net/magictong/archive/2008/10/14/3075478.aspx


1
1
分享到:
评论

相关推荐

    Python中调用C++dll例子

    使用C++编译器(如Visual Studio或g++)将源代码编译为DLL。例如,使用Visual Studio,你需要创建一个Win32 DLL项目,并将`addNumbers`函数添加到项目中。然后编译项目生成`CreateDll.dll`。 3. 使用Python调用DLL...

    c++生成DLL,分别用c++和python调用demo

    对于Python调用C++ DLL,我们需要使用`ctypes`库。在Python脚本中: ```python import ctypes dll = ctypes.cdll.LoadLibrary('ConsoleApplication1.dll') add = dll.Add print("The sum is:", add(3, 5)) ``` ...

    python引用DLL文件的方法

    `ctypes`是Python的标准库之一,它提供了与C兼容的数据类型,允许调用用C语言编写的函数,无需编写任何C或C++代码。要使用`ctypes`来加载DLL文件,首先需要导入`ctypes`模块。 ##### 示例DLL文件定义 假设我们有一...

    c++嵌入python

    为了使这个函数能在Python环境中调用,我们需要使用Python C API的`Py_InitModule()`或`PyModule_Create()`函数创建一个新的Python模块,然后使用`PyModule_AddFunction()`将我们的C++函数注册到该模块中。...

    python 调用 C++ dll 32位 64位 问题 ctypes.cdll.LoadLibrary

    t532.rar 测试代码 https://blog.csdn.net/wowocpp/article/details/105382257 python 调用 C++ dll 32位 64位 问题 ctypes.cdll.LoadLibrary

    Python调用DLL实例

    DLLs在Windows操作系统中尤为常见,C++是创建DLL的主要编程语言。本主题将深入探讨非MFC规则DLL、MFC规则DLL、MFC扩展DLL,并提供Python调用DLL的实例。 首先,我们来看非MFC规则DLL。非MFC(Microsoft Foundation ...

    Vs_Python.rar用python调用c++写的dll

    下面是如何使用Python调用上述C++ DLL的示例: ```python import ctypes # 加载DLL my_dll = ctypes.cdll.LoadLibrary("path_to_your_dll.dll") # 定义函数原型 sum_func = my_dll.sum sum_func.argtypes = ...

    ctypes库的使用 python调用Windows DLL

    ### ctypes库的使用:Python调用Windows DLL #### 一、ctypes库简介与功能 ctypes 是 Python 的一个标准库模块,它提供了与 C 兼容的数据类型,并且能够轻松地调用 C 库(DLL)中的函数。这对于那些需要与 C 语言...

    C++/ C#生成dll 用C/C#/Python/Unity 调用

    VS2015工程里面通过c++生成静态库lib,C++生成dll用C++工程,python, unity调用对应的dll。C#生成 dll,通过C#和Unity工程调用对应的dll。Python使用的是VS中集成Python. Python调用C#dll时需要通过pip安装python...

    C++ 扩展python

    首先,你需要为你的C++库创建一个SWIG接口文件(.i文件),定义哪些部分是暴露给Python的。然后,SWIG会生成Python模块的源码,编译后即可在Python中使用。 **2. Boost.Python** Boost库中的Boost.Python模块提供...

    C/C++与Python混编的详细文件

    9. **Python调用C/C++代码**:在Python脚本中,使用ctypes导入.so文件,通过`cdll`或`cdll.LoadLibrary`加载库,然后定义函数原型并调用。 10. **性能考虑**:使用C/C++编写的动态库通常比纯Python代码运行得更快,...

    python调用C++DLL.rar

    标题中的"python调用C++DLL"指的是在Python编程环境中,通过特定的接口技术来调用C++编译生成的动态链接库(DLL)文件。这通常涉及到跨语言交互,以便利用C++的高性能和Python的易用性。C++ DLL是Windows操作系统中...

    C++/Python混编代码

    文件包含tensorflow1.14模型文件(VGG16),pb格式的,C++调用python脚本程序代码,代码中包含向python文件中传值,并从python文件中取值。系统:ubuntu系统,编译器:qt5。

    python调用DLL带指针函数读ID卡

    在IT领域,有时候我们需要在Python中使用非Python编写的库,比如C或C++编译的动态链接库(DLL)。这种需求通常出现在处理硬件交互、底层系统操作或使用特定库时。本文将深入探讨如何在Python中调用DLL,特别是那些带...

    c++生成dll使用python调用dll的方法

    第一步,建立一个CPP的DLL工程,然后写如下代码,生成DLL 复制代码 代码如下:#include  #define DLLEXPORT extern “C” __declspec(dllexport) DLLEXPORT int __stdcall hello() { printf(“Hello world!\n”);...

    不安装Python的情况下用C++调用Python

    想在不安装python的情况下调用它,网上说的方法都不太详细,就连官方给的chm文件也没提及怎么配置,摸索了下,找到了简单调用的办法,文章在这里 https://blog.csdn.net/sunnyloves/article/details/45602423

    linux 下 python调用c或者c++编写的代码使用案例

    例如,你可能需要定义一个`void func(int)`的C函数,然后在Python中使用`ctypes.CDLL("libname.so").func`来调用。 2. **使用SWIG(Simplified Wrapper and Interface Generator)**: SWIG是一个工具,它可以自动...

    python36.dll

    上传的文件是32位python36.dll文件,用于在vs2013中的c++程序调用python程序时确实dll文件,可以将其放入系统文件夹System32文件下,适用32bit软件

    C++调用python文件

    C++代码需要包含`Python.h`并链接到Python的动态链接库(通常为`python37.dll`)。在VS2012中,你需要在项目的属性设置里指定包含目录和库目录,以便编译器能找到这些依赖。 当遇到“无法打开包括文件:“inttypes.h...

Global site tag (gtag.js) - Google Analytics