python的ctype调用_Python 使用ctypes调用 C 函数
这些数据都可以用一个默认值进行创建
>>> 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 "", 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
>>> 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
python的ctype调用_Python 使用ctypes调用 C 函数相关推荐
- python def return 文件_python基础-文件处理与函数
1. 文件处理 1.1 文件处理流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 1.2 文件读取模式r r文本模式的读,在文件不存在,不会创建新文件 f = ...
- python 运行时间 装饰器_python 装饰器统计某个函数的运行时间
import datetime def count_time(func): def int_time(*args, **kwargs): start_time = datetime.datetime. ...
- python子类初始化父类_Python实现子类调用父类的初始化实例
前言 python中进行面向对象编程,当在子类的实例中调用父类的属性时,由于子类的__init__方法重写了父类的__init__方法,如果在子类中这些属性未经过初始化,使用时就会出错. 例如以下的代 ...
- python测试rpc接口_python使用grpc调用rpc接口
#Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! importgrpcimportcoupon_pb2 as c ...
- python执行sqlserver存储过程_python – 从SqlAlchemy调用MSSQL存储过程
看起来SqlAlchemy似乎不支持调用存储过程.有没有人找到适用于SQL Server的解决方法? 样品程序: CREATE PROCEDURE list_lock_set @name varcha ...
- python 控制手机摄像头_python+open cv调用手机摄像头,保存文件
import cv2 cv2.namedWindow("camera", 1) # 开启ip摄像头 video = "http://admin:admin@192.168 ...
- python做接口外部调用_python使用suds调用外部接口
查看webservice接口函数@classmethod def get_methods(cls, client): """ 查看服务接口 :param client: ...
- python什么是调用_python open需要调用什么
withopen(r'c:\Users\zz9154\Desktop\12.txt','w')asf:print(f.read())Traceback(mostrecentcalllast):Fil ...
- python3库查看调用_Python 3 中调用 COM 的库文件 | 学步园
在 Python 2.x 时代貌似有支持 COM的. http://svn.python.org/projects/ctypes/tags/release_0_6_2/ctypes/win32/com ...
- python函数调用的例子_Python案例|混用C函数
今天,我们来解决python的速度问题.我们平时所说的Python是指在PC机上的CPython,换句话说,Python的解释器是用C语言编写的.为Python编写C扩展模块能够显著提高Python的 ...
最新文章
- C语言------运算符和表达式
- 基于ANN的6种调制信号自动调制识别(2ASK、4ASK、2FSK、4FSK、2PSK、4PSK)
- Spark函数详解系列--RDD基本转换
- Windows7配置GPU和Theano编程环境
- boost asio resolver
- 2019-03-13-算法-进化(验证回文串)
- 信息学奥赛一本通(1318:【例5.3】自然数的拆分)
- 【数据结构----笔记4】插入排序算法之【折半插入排序算法】
- Struts2 + Hibernate + Spring 以及javaweb模块问题解决(1)
- oracle简单建库基本流程
- java收集碎片_Thinking in Java 笔记碎片
- 玩转混合云+边缘计算,且看ZStack Mini!
- 中国计算机软件行业分析4---外企的商业贿赂
- 百宝云Post与Get事件教程
- LSD-SLAM论文总结
- html页面下边显示不全,网页显示不全怎么处理(网页显示排版不正常)
- 福州大学866信号与系统初试经验分享
- 力扣(415.482)补8.28
- 考驾照--驾驶证考试
- C# - Entity Framework 对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性