ctypes 使用方法与说明


1, 首先确定你的python支持不支持ctypes 

python2.7以后ctypes已经是标配了,2.4以后的版本得自己装下ctypes

2,加载动态库

两种加载方式

>>> from ctypes import *
     >>> libc = cdll . LoadLibrary ( "libc.so.6" )
     >>> libc.printf("%d",2)
     >>> from ctypes import *
     >>> libc = CDLL ( "libc.so.6" )
     >>> libc.printf("%d",2)

3, 调用系统函数

上面的例子已经调用了系统函数printf,这里再给几个其他例子

>>> from ctypes import *
     >>> libc = CDLL ( "libc.so.6" )
     >>> print libc . time ( None )
     1308019893
     >>> print libc.atoi("234")
     234

4,ctypes 数据类型和 C数据类型 对照表 

ctypes type C type Python type
c_bool _Bool bool (1)
c_char char 1-character string
c_wchar wchar_t 1-character unicode string
c_byte char int/long
c_ubyte unsigned char int/long
c_short short int/long
c_ushort unsigned short int/long
c_int int int/long
c_uint unsigned int int/long
c_long long int/long
c_ulong unsigned long int/long
c_longlong __int64or long long int/long
c_ulonglong unsigned __int64or unsigned long long int/long
c_float float float
c_double double float
c_longdouble long double float
c_char_p char *(NUL terminated) string or None
c_wchar_p wchar_t *(NUL terminated) unicode or None
c_void_p void * int/long or None

这些数据都可以用一个默认值进行创建

>>> c_int()
c_long(0)
>>> c_char_p("Hello, World")
c_char_p('Hello, World')
>>> c_ushort(-3)
c_ushort(65533)
>>>

这些数据也可以被改变:

>>> i = c_int(42)
>>> print i
c_long(42)
>>> print i.value
42
>>> i.value = -99
>>> print i.value
-99
>>>

赋值给 c_char_p,c_wchar_p,c_void_p

只改变他们指向的内存地址,而不是改变内存的内容

>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> c_s.value = "Hi, there"
>>> print c_s
c_char_p('Hi, there')
>>> print s                 # first string is unchanged
Hello, World
>>>

如果需要可改变内容的字符串,需要使用 create_string_buffer()

>>> from ctypes import *
>>> p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes
>>> print sizeof(p), repr(p.raw)
3 '/x00/x00/x00'
>>> p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string
>>> print sizeof(p), repr(p.raw)
6 'Hello/x00'
>>> print repr(p.value)
'Hello'
>>> p = create_string_buffer("Hello", 10)  # create a 10 byte buffer
>>> print sizeof(p), repr(p.raw)
10 'Hello/x00/x00/x00/x00/x00'
>>> p.value = "Hi"
>>> print sizeof(p), repr(p.raw)
10 'Hi/x00lo/x00/x00/x00/x00/x00'
>>>

5,函数返回类型

函数默认返回 C int 类型,如果需要返回其他类型,需要设置函数的 restype 属性

>>> strchr = libc.strchr
>>> strchr("abcdef", ord("d")) # doctest: +SKIP
8059983
>>> strchr.restype = c_char_p # c_char_p is a pointer to a string
>>> strchr("abcdef", ord("d"))
'def'
>>> print strchr("abcdef", ord("x"))
None
>>>

6,传递指针或者引用

很多情况下 C 函数需要传递指针或者引用,ctypes也完美的支持这一点
byref() 用来传递引用参数,pointer() 函数也可以完成同样的工作,但pointer()会创建一个实际的指针对象,如果你不需要一个指针对象,
用byref()会快很多

>>> i = c_int()
>>> f = c_float()
>>> s = create_string_buffer('/000' * 32)
>>> print i.value, f.value, repr(s.value)
0 0.0 ''
>>> libc.sscanf("1 3.14 Hello", "%d %f %s",... byref(i), byref(f), s)
3
>>> print i.value, f.value, repr(s.value)
1 3.1400001049 'Hello'
>>>

7,结构体和联合 
结构体和联合必须从 Structure 和 Union 继承,子类必须定义 
_fields_ 属性,_fields_ 属性必须是一个2元组的列表,
包括一个field名字和field的类型
field类型 必须是一个ctypes的类型例如 c_int, 或者其他继承自ctypes的类型,结构体,联合,数组,指针。

下面的例子演示一个 POINT结构体,包括 field  X,Y

>>> from ctypes import *
>>> class POINT(Structure):. 
    _fields_ = [("x", c_int),
                ("y", c_int)]

>>> point = POINT(10, 20)
>>> print point.x, point.y
10 20
>>> point = POINT(y=5)
>>> print point.x, point.y
0 5
>>> POINT(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1
, in ?
ValueError
: too many initializers
>>>

一个复杂点的例子,field类型也是一个结构体

>>> class RECT(Structure):
...     _fields_ = [("upperleft", POINT),
...                 ("lowerright", POINT)]
...
>>> rc = RECT(point)
>>> print rc.upperleft.x, rc.upperleft.y
0 5
>>> print rc.lowerright.x, rc.lowerright.y
0 0
>>>

多种方式进行初始化
>>> r = RECT(POINT(1, 2), POINT(3, 4))
>>> r = RECT((1, 2), (3, 4))

8,数组

数组定义很简单

定义一个有10个POINT元素的数组

TenPointsArrayType = POINT * 10

初始化和使用数组:

>>> from ctypes import *
>>> TenIntegers = c_int * 10
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> print ii
<c_long_Array_10 object at 0x...>
>>> for i in ii: print i,
...
1 2 3 4 5 6 7 8 9 10
>>>

9,指针 
pointer() 函数可以创建一个指针

Pointer实例有一个 contents属性 返回指针指向的对象

>>> from ctypes import *
>>> i = c_int(42)
>>> pi = pointer(i)
>>> pi.contents
c_long(42)

可以改变指针指向的内容

>>> i = c_int(99)
>>> pi.contents = i
>>> pi.contents
c_long(99)
>>>

可以按数组方式访问:

>>> pi[0]
99
>>>

按数组方式改变值
>>> print i
c_long(99)
>>> pi[0] = 22
>>> print i
c_long(22)
>>>

以上都是ctypes的基本用法,对普通的开发人员来说,基本够用了

更详细的说明请参考:http://docs.python.org/library/ctypes.html

ctypes 使用方法与说明相关推荐

  1. python关联通达信pywin32_Python ctypes.wintypes方法代码示例

    本文整理汇总了Python中ctypes.wintypes方法的典型用法代码示例.如果您正苦于以下问题:Python ctypes.wintypes方法的具体用法?Python ctypes.wint ...

  2. c调用python第三方库_用 Python ctypes 来调用 C/C++ 编写的第三方库

    看到一篇简洁的文章--如何用Python ctypes调用C++(ctypes调用C没这么多麻烦事),不敢独享... 如果需要用 Python 调用 C/C++ 编写的第三方库(这些第三方库很可能就是 ...

  3. Python --- ctypes库的使用

    ctypes 的官方文档 英文文档:https://docs.python.org/3/library/ctypes.html 中文文档:https://docs.python.org/zh-cn/3 ...

  4. multiprocessing python_Python多线程/进程(threading、multiprocessing)知识覆盖详解

    你好,我是goldsunC 让我们一起进步吧! 基本知识 在Python中有一个全局解释器锁GIL(Global Interpreter Lock).GIL源于Python设计之初的考虑,目的是使数据 ...

  5. python基于海康设备SDK的二次开发(三)

    (最近重新修改了下SDK,因为在两个车间各安装了一台设备) 再次使用下这张图 今天是海康设备SDK二次开发的第三天,也是最后一次,因为这次将进入功能开发程序阶段,前面两次已经做好了全部的前期工作 . ...

  6. 在 Oracle Enterprise Linux 和 iSCSI 上构建您自己的 Oracle RAC 11g 集群

    作者:Jeffrey Hunter 了解如何以低于 2,700 美元的费用在 Oracle Enterprise Linux 上安装并配置 Oracle RAC 11g 第 2 版开发集群. 本指南中 ...

  7. Java面试题大全2021版

    一.Java 基础 JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java Run ...

  8. python调用so库输出传入指针_python中使用ctypes调用so传参设置遇到的问题及解决方法...

    问题 近日在做一组声纹聚类时,使用了另一团队同学开发的声纹距离算法.该算法对外提供的是一组so包,需要使用方自己去使用.在python中调用纯so包一般使用ctypes类库,用起来看起来简单但也有不少 ...

  9. c语言指针生成numpy数组,利用ctypes获取numpy数组的指针方法

    利用ctypes获取numpy数组的指针方法 如下所示: import numpy as np from ctypes import * a = np.asarray(range(16), dtype ...

最新文章

  1. 近距离接触RAC DRM
  2. fon在计算机网络中代表什么,计算机网络自顶向下方法第一章学习
  3. 使用ilmerge实现.net程序静态链接
  4. ACM学习历程—Hihocoder [Offer收割]编程练习赛1
  5. [HTML5]使用Box2dWeb模拟飞行箭矢
  6. 自己编译vim,解决Ubuntu 12.04 terminal里执行gvim会挂起的问题
  7. win7上安装mysql数据库_mysql-windows系统安装mysql数据库
  8. 编译静态expect现undefined reference to `openpty' 错误解决方法
  9. 全国计算机二级c语言题库,计算机二级c语言题库及答案
  10. java 气象数据_科学网-下载某地历史逐小时环境气象数据的方法-风速风向温湿度-洪晓强的博文...
  11. VTK:图像阈值用法实战
  12. Windows API (DAY07) ShowWindow 函数
  13. win10开启显示:你的电脑/设备需要修复,错误代码:0xc0000225
  14. 《1024伐木累》-小白篇之开发网站,三天!(结束篇)-总章节十三
  15. Image Processing Algorithms
  16. PWM转4~20mA电路
  17. 电脑计算机桌面窄,大神讲解电脑屏幕变窄且两边是黑的鼠标点不到?
  18. 高精度减法(C语言实现)
  19. CSDN-markdown编辑器帮助文档
  20. 「SCOI 2018 D2T1」Pipi 酱的日常

热门文章

  1. 使用UInput模拟系统键盘鼠标动作 UInput driver分析
  2. vmware不能resume问题,Collect Support Data,vmware.log
  3. 学习笔记 ---- 设计模式之观察者模式
  4. hadoop和spark的区别
  5. BZOJ3437:小P的牧场(斜率优化DP)
  6. 硬盘的分区误删除的恢复
  7. ubuntu上virsh+kvm安装虚拟机
  8. 对自己的python项目配置PYTHONPATH
  9. Unity GUI屏幕自适应
  10. ubuntu mysql lessons