python - 如何在 ctypes 中取消引用 void*?

标签 python linux ctypes

考虑以下代码:

import ctypes

IPC_PRIVATE, MAP_SIZE, IPC_CREAT, IPC_EXCL = 0, 65536, 512, 1024

shmget = ctypes.cdll.LoadLibrary("libc.so.6").shmget
shmat = ctypes.cdll.LoadLibrary("libc.so.6").shmat

shm_id = shmget(IPC_PRIVATE, MAP_SIZE, IPC_CREAT | IPC_EXCL | 0600)
trace_bits = shmat(shm_id, 0, 0)
s = ctypes.string_at(ctypes.c_void_p(trace_bits), 1)
print(s[0])

当我尝试运行它时,它在成功运行 shmat 后给我一个“段错误”。我做错了什么?

最佳答案

默认情况下,所有像这样包装的ctypes 函数都有restype == c_int。所以你需要在调用它之前正确设置它。 argtypes 也是如此。

关于python - 如何在 ctypes 中取消引用 void*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32854815/

相关文章:

c - argv 的编码是什么?

Python int 在调用 ctypes 函数时溢出

python - 在 3D 图中剪辑 contourf() 图

python - 无限循环 - 魔方扰乱器

python - 用户在 Django 表单中输入无效正则表达式导致 500 服务器错误

python - 为什么python中没有do while循环

python - 为什么我的 Python 脚本在 Linux 中作为后台时不写入文件?

javascript - 我可以从 javascript 调用 Linux 终端吗?

python - 在 Python 中通过 ctypes 调用 GetExtendedTcpTable 时,谁负责释放 MIB_TCPROW_OWNER_PID 结构?

python - 如何使用 boost.python 将预填充的 "unsigned char*"缓冲区传递给 C++ 方法?