python ctypes

windows API
from ctypes import windll
kernel32 = windll.kernel32

https://docs.python.org/2/library/ctypes.html
http://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/
官方说法:允许python去调用DLLs和共享库中的c函数。
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python

linux
python中如何使用c的动态链接库
from ctypes import *
libc = CDLL(“libc.so.6”)
libc

windows
libc = cdll.msvcrt

Structures and unions must derive from the Structure and Union base classes which are defined in the ctypes module. Each subclass must define a fields attribute. fields must be a list of 2-tuples, containing a field name and a field type

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

from ctypes import pointer

i = c_int(42)
pi = pointer(i)
print pi.contents

Note that ctypes does not have OOR (original object return), it constructs a new, equivalent object each time you retrieve an attribute:

这里写图片描述

posted @ 2022-03-06 10:39  叶常落  阅读(66)  评论(0编辑  收藏  举报