python ctypes
发布日期:2021-05-07 18:06:10 浏览次数:21 分类:技术文章

本文共 1032 字,大约阅读时间需要 3 分钟。

windows API

from ctypes import windll
kernel32 = windll.kernel32

官方说法:允许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:

这里写图片描述

上一篇:linux下各种小命令
下一篇:imp库,python进入import内部

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年03月24日 20时08分17秒