如何用 C++ 为 Python 写 dll

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值