ctypes:从任意整数构造指针
出于低级目的,我需要从任意地址(以整数给出)构造一个 ctypes 指针。例如:
INTP = ctypes.POINTER(ctypes.c_int)
p = INTP(0x12345678) # i *know* this is the address
但是所有这些尝试都会导致
TypeError: expected c_long instead of int
我能做些什么来克服这个问题吗?如果有人想知道我为什么需要这个,这样做是为了从 win32file.PyOVERLAPPED
中提取 OVERLAPPED
结构,用于将 ctypes 公开的函数与 win32file 包装的 API 集成。
谢谢,
-托默
For low-level purposes, I need to construct a ctypes pointer from an arbitrary address, given as an integer. For instance:
INTP = ctypes.POINTER(ctypes.c_int)
p = INTP(0x12345678) # i *know* this is the address
But all such attempts result in
TypeError: expected c_long instead of int
Is there anything I can do to overcome this? In case someone wonders why I need this, it's done so as to extract the OVERLAPPED
struct from a win32file.PyOVERLAPPED
, for integrating ctypes-exposed functions with win32file wrapped APIs.
Thanks,
-Tomer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ctypes.cast(addr, type)
。我将扩展您的示例以通过已知对象获取地址,以演示:输出:
You can use
ctypes.cast(addr, type)
. I'll extend your example to acquire an address via a known object, to demonstrate:Output: