- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
奇怪的是,即使在设置了 restype
之后,python 仍会返回 long
而不是 c_void_p
。
例如;
# python code
from ctypes import *
dll = windll.LoadLibrary("my.dll")
dll.my_object_create.restype = c_void_p
x = dll.my_object_create()
print type(x) # prints <type 'long'>
//c++ code
my_object *my_object_create() { return new my_object(); }
void my_object_destroy(my_object *obj) { delete obj; }
我最近不得不修复一个错误,在该错误中,将 x
反馈给另一个 ctypes 函数时,指针被践踏了。这是通过将初始 dll 调用更改为
x = c_void_p(dll.my_object_create())
...我猜 ctypes 将 x
视为 4 个字节长而不是 8 个字节(64 位架构)。
所以我想知道现有行为是否有原因将您引入这个陷阱?
最佳答案
P_get
对于'P' pointer type使用 PyLong_FromVoidPtr
.如果地址适合平台 long
,它返回一个 Python int
;否则它返回一个 Python long
,它具有可变的精度。很好,但是当将此整数值作为参数传递时,默认行为是 convert to a C int
,在所有支持的平台上都是 32 位的。
我认为最好的解决方案是设置 argtypes
以正确地将参数转换为指针类型。另一种选择是将 restype
设置为 c_void_p
的子类。使用子类会禁用到 Python 整数的转换。 GetResult
通过调用 _ctypes_simple_instance
检查这个,它实际上返回与其名称和源评论建议相反的内容。 (在 2.5 中,这个函数被命名为 IsSimpleSubType
,并且当时的源评论也是错误的。有问题的“简单”从来不是元类 PyCSimpleType
,而是基类型 _SimpleCData
。)
POSIX:
# Configure the interpreter to load visible extension-
# module symbols, such as _ctypes_simple_instance,
# into the global symbol table.
import sys, DLFCN
sys.setdlopenflags((sys.getdlopenflags() & ~DLFCN.RTLD_LOCAL) |
DLFCN.RTLD_GLOBAL)
from ctypes import *
_ctypes_simple_instance = PyDLL(None)._ctypes_simple_instance
_ctypes_simple_instance.argtypes = py_object,
malloc = CDLL(None).malloc
class my_void_p(c_void_p):
pass
>>> _ctypes_simple_instance(c_void_p)
0
>>> _ctypes_simple_instance(my_void_p)
1
>>> malloc.restype = c_void_p
>>> type(malloc(100))
<type 'int'>
>>> malloc.restype = my_void_p
>>> type(malloc(100))
<class '__main__.my_void_p'>
window :
_ctypes_simple_instance
不由 _ctypes.pyd 导出。
from ctypes import *
malloc = cdll.msvcrt.malloc
class my_void_p(c_void_p):
pass
>>> malloc.restype = c_void_p
>>> type(malloc(100))
<class 'int'>
>>> malloc.restype = my_void_p
>>> type(malloc(100))
<class '__main__.my_void_p'>
关于python - 为什么设置 ctypes dll.function.restype=c_void_p 返回 long?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17840144/
我已经定义了 ctypes 结构,像这样: class MyStruct(Structure): _fields_ = [('x', ctypes.c_ulonglong), ('y', ct
给定一个字符串: msg="hello world" 如何将其定义为 ctypes.c_void_p() 数据类型? 以下代码会产生“无法转换为指针”异常: data=ctypes.c_void_p(
我有一个关于 Python 的 ctypes 和调用 C 函数的问题困扰了我几天。我正在使用 Python 3.5 和 ctypes 来包装 C .dll。 我有一个接受 void ** 作为输出参数
我有一个使用 ctypes 的 C 库包装函数,看起来像这样 rrpLib.getParameterValueAsPointer.restype = c_void_p def getParameter
我目前正面临 ctypes 的问题。我有一个 C 函数 foo 这样: void** foo(int); 我必须为 foo 函数定义一个回调函数。所以: FOO_FUNC = CFUNCTYPE(PO
长话短说,我试图将字典列表传递到容器类中,目的是每个字典都将用于实例化另一个类。问题是每个字典都包含一个要分配给子类的函数对象引用,并且由于某种原因,就在最里面的子类被实例化之前,它从 python
我正在与一个返回不透明指针的库交互。子类化 c_void_p 以在 ctypes 中表示它并为 c_void_p 的这种特殊风格提供类型检查是否可以接受? 最佳答案 进行这种类型检查的一种简单方法可能
我正在尝试将 Python 对象转换为 C void 指针,以便可以从 C API 调用它,但是以下代码似乎不起作用从 ctypes 导入 * class myclass: def __ini
我正在与 ctypes 合作并且似乎无法弄清楚如何使用自定义数据类型。希望有一个 C++ 公共(public)方法的 Python 接口(interface) cell类和 C++ cellCompl
在 Python 应用程序中,我有一个包含一些数据的字节数组。我的python代码由一些外部 native 库(DLL/so/dylib)调用(使用ctypes及其 callback function
首先我知道Python ctypes, pass c_void_p as an out parameter to c function以及他们建议的解决方案,但我无法通过他们的回答使其发挥作用。反正我
我正在使用第 3 方 DLL 加载一些原始图像数据,并且我想将此原始图像数据用作 openGL 中的纹理。然而,c 函数返回一个 void*,我需要以某种方式转换它,以便它将作为 glTexImage
我正在编写一个包含一些 C++ 代码的 Python 模块。我曾经使用这个函数将一个指针从 C++ 传递给 Python(该指针将被另一个函数释放 DLLEXPORT void* run(double
我似乎找不到任何将 ctypes.c_void_p() 转换为字符串或字节数组的简单示例。有没有简单的衬里可以做到这一点? 最佳答案 给你: import ctypes as ct # set up
我经常使用 ctypes,但我遇到了一些我无法解释的事情。 首先,当我使用 c_void_p 定义回调时,回调将接收一个固定整数。 我看到一些其他帖子提到在使用 c_void_p 作为返回值时将 c_
我需要转换 multiprocessing.Array到 c_void_p 中。目前我所拥有的是: shared_mem_bfr = multiprocessing.Array(c_uint8, im
奇怪的是,即使在设置了 restype 之后,python 仍会返回 long 而不是 c_void_p。 例如; # python code from ctypes import * dll = w
我需要将用 C++ 创建的 ArrayFire 数组共享给 Python。没问题: PyObject* arrayToPyObject(const af::array& arr) { // C
我是一名优秀的程序员,十分优秀!