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 @   叶常落  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示