Python在Windows和在Linux下调用动态链接库的教程


Posted in Python onAugust 18, 2015

Linux系统下调用动态库(.so)

1、linuxany.c代码如下:

#include "stdio.h"
  void display(char* msg){
    printf("%s\n",msg);
  }
   
  int add(int a,int b){
    return a+b;
  }

2、编译c代码,最后生成Python可执行的.so文件
(1)gcc -c linuxany.c,将生成一个linuxany.o文件
(2)gcc -shared linuxany.c -o linuxany.so,将生成一个linuxany.so文件

3、在Python中调用

#!/usr/bin/python
   
  from ctypes import *
  import os 
  //参数为生成的.so文件所在的绝对路径
  libtest = cdll.LoadLibrary(os.getcwd() + '/linuxany.so') 
  //直接用方法名进行调用
  print 
  libtest.display('Hello,I am linuxany.com') 
  print libtest.add(2,2010)

4、运行结果

Hello,I am linuxany.com
2012

Windows下Python调用dll

python中如果要调用dll,需要用到ctypes模块,在程序开头导入模块 import ctypes

由于调用约定的不同,python调用dll的方法也不同,主要有两种调用规则,即 cdecl和stdcal,还有其他的一些调用约定,关于他们的不同,可以查阅其他资料

先说 stdcal的调用方法:

方法一:

import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )

方法二:

import ctypes
dll = ctypes.WinDll( 'test.dll' )

cdecl的调用方法:

1.

import ctypes
dll = ctypes.cdll.LoadLibrary( 'test.dll' )
##注:一般在linux下为test.o文件,同样可以使用如下的方法:
## dll = ctypes.cdll.LoadLibrary('test.o')

2.

import ctypes
dll = ctypes.CDll( 'test.dll' )

看一个例子,首先编译一个dll

导出函数如下:

# define ADD_EXPORT Q_DECL_EXPORT
extern "C" ADD_EXPORT int addnum(int num1,int num2)
{
return num1+num2;
}


extern "C" ADD_EXPORT void get_path(char *path){
memcpy(path,"hello",sizeof("hello"));
}

这里使用的是cdecl

脚本如下:

dll=ctypes.CDLL("add.dll")
add=dll.addnum
add.argtypes=[ctypes.c_int,ctypes.c_int] #参数类型
add.restypes=ctypes.c_int            #返回值类型
print add(1,2)


get_path=dll.get_path
get_path.argtypes=[ctypes.c_char_p]
path=create_string_buffer(100)
get_path(path)
print path.value

结果如下:

Python在Windows和在Linux下调用动态链接库的教程

我们看到两个结果,第一个是进行计算,第二个是带回一个参数。

当然我们还可以很方便的使用windows的dll,提供了很多接口

GetSystemDirectory = windll.kernel32.GetSystemDirectoryA
buf = create_string_buffer(100)
GetSystemDirectory(buf,100)
print buf.value
MessageBox = windll.user32.MessageBoxW
MessageBox(None, u"Hello World", u"Hi", 0)

运行结果如下:

Python在Windows和在Linux下调用动态链接库的教程

Python 相关文章推荐
使用Python将数组的元素导出到变量中(unpacking)
Oct 27 Python
python微信跳一跳系列之棋子定位颜色识别
Feb 26 Python
python时间日期函数与利用pandas进行时间序列处理详解
Mar 13 Python
Python生成短uuid的方法实例详解
May 29 Python
python实现石头剪刀布程序
Jan 20 Python
Python处理时间日期坐标轴过程详解
Jun 25 Python
Django自定义用户登录认证示例代码
Jun 30 Python
详解numpy.meshgrid()方法使用
Aug 01 Python
tensorflow实现在函数中用tf.Print输出中间值
Jan 21 Python
python异常处理、自定义异常、断言原理与用法分析
Mar 23 Python
Python爬虫之Selenium下拉框处理的实现
Dec 04 Python
python中用ggplot绘制画图实例讲解
Jan 26 Python
Python中map,reduce,filter和sorted函数的使用方法
Aug 17 #Python
Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法
Aug 16 #Python
使用Python操作MySQL的一些基本方法
Aug 16 #Python
Python中list列表的一些进阶使用方法介绍
Aug 15 #Python
Python中的super()方法使用简介
Aug 14 #Python
在Python中使用正则表达式的方法
Aug 13 #Python
简单讲解Python中的闭包
Aug 11 #Python
You might like
Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)
2011/04/18 PHP
php中实现用数组妩媚地生成要执行的sql语句
2015/07/10 PHP
Android AsyncTack 异步任务实例详解
2016/11/02 PHP
PHP单例模式详解及实例代码
2016/12/21 PHP
javascript 单选框,多选框美化代码
2008/08/01 Javascript
15 个 JavaScript Web UI 库
2010/05/19 Javascript
javascript框架设计读书笔记之数组的扩展与修复
2014/12/02 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
原生js实现class的添加和删除简单代码
2016/07/12 Javascript
BootStrap fileinput.js文件上传组件实例代码
2017/02/20 Javascript
Bootstrap表单制作代码
2017/03/17 Javascript
JavaScript方法_动力节点Java学院整理
2017/06/28 Javascript
Vue.js实现按钮的动态绑定效果及实现代码
2017/08/21 Javascript
vue如何进行动画的封装
2018/09/26 Javascript
JavaScript变量作用域及内存问题实例分析
2019/06/10 Javascript
JavaScript实现京东放大镜效果
2019/12/03 Javascript
python定时器使用示例分享
2014/02/16 Python
Python数据结构之栈、队列及二叉树定义与用法浅析
2018/12/27 Python
Django中的静态文件管理过程解析
2019/08/01 Python
python Event事件、进程池与线程池、协程解析
2019/10/25 Python
python 类之间的参数传递方式
2019/12/20 Python
python对文件的操作方法汇总
2020/02/28 Python
如何利用Python识别图片中的文字
2020/05/31 Python
安装Anaconda3及使用Jupyter的方法
2020/10/27 Python
快速创建python 虚拟环境
2020/11/28 Python
实习自我评价怎么写
2013/12/02 职场文书
英文自荐信
2013/12/19 职场文书
高三高考决心书
2014/03/11 职场文书
毕业生求职自荐书范文
2014/03/27 职场文书
中国入世承诺
2014/04/01 职场文书
专家推荐信模板
2014/05/09 职场文书
推广活动策划方案
2014/08/23 职场文书
党员民主评议总结
2014/10/20 职场文书
销售员态度差检讨书
2014/10/26 职场文书
党员证明模板
2015/06/19 职场文书
PHP策略模式写法
2021/04/01 PHP