python中的ctypes库初探
来源:互联网 发布:微软笔记软件 编辑:程序博客网 时间:2024/05/01 22:30
python ctypes
Table of Contents
- 1 Python中调用C语言库
1 Python中调用C语言库
Python中提供了ctypes标准库,用来调用C语言库中的函数。
- 加载dll C语言函数存在不同的调用约定,常见的有stdcall和cdecl,这两种调用约定在Python中的调用函数需要区分:
- stdcall的加载方式: dll = windll.LoadLibrary("dllpath") dll = WinDLL("dllpath")
- cdecl的加载方式: dll = cdll.LoadLibrary("dllpath") dll = CDLL("dllpath")
- 调用函数,传入参,返回值 C函数通过参数读取值,做处理后通过返回值传回。
#include <stdlib.h>// file add.c#include <stdio.h>int add(const int a, const int b){ return a+b;}
把C函数编译为.so库,执行一下命令: gcc -shared -fPIC -o libadd.so add.c
from ctypes import *dll = CDLL("./libadd.so")print("add(1,2) = ", dll.add(1, 2))
执行Python命令,查看执行情况: python3 ./add.py add(1,2) = 3
- C函数通过参数传出值,即参数是指针的情况 C语言指针概念在ctypes中用pointer对应,如下示例:
#include <stdlib.h>// file add.c#include <stdio.h>int add(const int a, const int b){ return a+b;}int add2(const int a, const int b, int *c){ if (NULL != c) *c = a+b; return a+b;}
add2函数传入指针,并可通过指针输出值。 python调用函数如下:
from ctypes import *dll = CDLL("./libadd.so")print("add(1,2) = ", dll.add(1, 2))out = c_int(0)ptr = pointer(out)out.value = 100print("out.value = ", out.value)dll.add2(2,3,ptr)print("add2(2, 3) = ", out.value)
python3 ./add.py add(1,2) = 3 out.value = 100 add2(2, 3) = 5
- 参数是结构体指针 结构体通过继承Structure类实现,各字段通过field定义,示例如下:
typedef struct{ int x; int y;} Point;int add3(const Point a, const Point b, Point *c){ if (NULL == c) return 255; c->x = a.x + b.x; c->y = a.y + b.y; return 0;}
Python中的定义如下:
class Point(Structure): _fields_ = ("x", c_int), ("y", c_int) def __repr__(self): return "(%d, %d)" % (self.x, self.y)p1 = Point(1,2)p2 = Point(5, 8)p3 = Point(0,0)ptr_p3 = pointer(p3)dll.add3(p1,p2,ptr_p3)print("add3(p1,p2) = ", p3)
其中_repr_是python的内置函数,这个是类的展示函数,相当于java语言中的toString()函数。
- 入参char*指针
int pr(const char *name, char *out){ printf("Hello, %s\n", name); sprintf(out, "Hello, %s\n", name); return 0;}
- 方法1
sbuf = create_string_buffer(b'robin')out = create_string_buffer(100)dll.pr(sbuf, out)print("1--", out.value)
- 方法2
sbuf2 = b'\0'*10pStr = c_char_p()pStr.value = sbuf2dll.pr(b'John', pStr)print("2--", sbuf2)
- 方法3
sbuf3 = b'\0'*20dll.pr(b'Rich', sbuf3)print("3--", sbuf3)
- 方法1
- 字节序处理 fields定义的每个参数的类型后面可以定义bit位,示例如下:
int swap32(const int x){ return htonl(x);}
Python中测试字节序如下:
class Int(Structure): _fields_ = [("uint8_0", c_int, 8), ("uint8_1", c_int, 8), ("uint8_2", c_int, 8), ("uint8_3", c_int, 8)] def __init__(self, i): i0 = i & 0xFF i1 = (i >> 8) & 0xFF i2 = (i >> 16) & 0xFF i3 = (i >> 24) & 0xFF super().__init__(i0,i1,i2,i3) def __repr__(self): return "0x%08X" % ((self.uint8_3<<24) + \ (self.uint8_2<<16) + \ (self.uint8_1<<8) + \ (self.uint8_0))i = Int(0x12345678)i_big = dll.swap32(i)print( i, "0x%08X" % i_big)
这里使用Int主要是为了测试bit的定义,这个用例用Python内置的int类型ibig=dll.swap32(0x12345678)就可以。
Date: 2013-07-28 17:56:36 中国标准时间
Org version 7.8.11 with Emacs version 24
Validate XHTML 1.0- python中的ctypes库初探
- Python--ctypes
- Python Ctypes
- python ctypes
- python ctypes - python调用C语言库
- 用python ctypes调用动态链接库
- Python:使用ctypes库调用外部DLL
- Python:使用ctypes库调用外部DLL
- python ctypes 调用 dll动态库小结
- Python:使用ctypes库调用外部DLL
- Python 外部函数调用库ctypes简介
- Python中ctypes库的学习
- ctypes库
- Python:使用ctypes库调用外部DLL(附带ctypes c 类型转换图)
- 【Python】Python通过ctypes调用C动态链接库
- ctypes-Python外部函数库
- python 实践之 ctypes
- [python相关]ctypes模块
- Unity 绘制人物头顶的血条
- 浅析人脸检测之Haar分类器方法
- HDU 1159 Common Subsequence
- 等概率随机函数的实现 .
- Android如何防止apk程序被反编译(尊重劳动成果)
- python中的ctypes库初探
- Flip Game
- ASCII,UTF-8和Unicode字符编码规范
- EL表达式详解
- 捕捉 程序关闭或者 进入后台事件
- android 开发环境搭建及hello world
- 杭电 1308 What Day Is It?
- 程序员有哪些发展方向
- Lustre的安装与配置