
Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 windows 下的 .dll 文件,或者 linux 下的 .so 文件。先来看一下 ctypes 怎么使用 C 标准库。
windows 系统下的 C 标准库动态链接文件为 msvcrt.dll (一般在目录 C:\windows\System32 和 C:\windows\SysWOW64 下分别对应 32-bit 和 64-bit,使用时不用刻意区分,Python 会选择合适的)
linux 系统下的 C 标准库动态链接文件为 libc.so.6 (以 64-bit Ubuntu 系统为例, 在目录 /lib/x86_64-linux-gnu 下)
代码:#例如,以下代码片段导入 C 标准库,并使用 printf 函数打印一条消息import platformfrom ctypes import *if platform.system() == ‘windows‘: libc = cdll.Loadlibrary(‘msvcrt.dll‘) # libc = windll.Loadlibrary(‘msvcrt.dll‘)elif platform.system() == ‘linux‘: libc = cdll.Loadlibrary(‘libc.so.6‘)string=‘Hello ctypes!\n‘libc.printf(string.encode("utf-8"))二、platform模块
该模块用来访问平台相关属性。
常见属性和方法
# 返回平台架构print(platform.machine())# AMD64# 获取网络名称print(platform.node())# DESKtop-NMIUQ2D# 获取系统版本print(platform.platform())# windows-10-10.0.17763-SP0# 获取处理器名称print(platform.processor())# Intel64 Family 6 Model 142 StepPing 10,GenuineIntel# 获取系统名称print(platform.system())# windows总结
以上是内存溢出为你收集整理的platform模块和ctypes模块全部内容,希望文章能够帮你解决platform模块和ctypes模块所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)