c++ - 如何找到指向结构的指针的地址并将其转换为 CFFI 中的 void**

标签 c++ python-2.7 python-cffi

我的C++代码是

StructureEx* obj; // structure
functionEx((void**)&obj);

我的职能是

int functionEx(void** obj); //calling function

我是 CFFI 的新手。所以我的问题是

  1. 如何在 CFFI 中实现同样的目标?

  2. 如何在CFFI中找到一个指针的地址,指向结构体的指针?

我知道转换为 void** 可以通过

ffi.cast("void*",address)

但是我怎样才能得到那个地址并传递给函数呢?

最佳答案

可以声明可用的 arg = ffi.new("void **")

打印以下代码

<cdata 'void *' NULL>

<cdata 'void *' 0xc173c0>

7

即首先指针的值为零,调用后对应于functionEx中设置的值。

from cffi import FFI

ffi = FFI()
ffi.cdef("""int functionEx(void** obj);""")

C = ffi.dlopen("./foo.so")
print(C)
arg = ffi.new("void **")
print(arg[0])
C.functionEx(arg)
print(arg[0])
ints = ffi.cast("int *", arg[0])
print(ints[7])
#include <stdio.h>
#include <stdlib.h>

int functionEx(void ** obj)
{
    int * arr;
    int i;

    *obj = malloc(sizeof(int) * 8);

    arr = *obj;
    for (i=0; i<8; i++) {
        arr[i] = i;
    }

    return 0;
}

关于c++ - 如何找到指向结构的指针的地址并将其转换为 CFFI 中的 void**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38530702/

相关文章:

python - CFFI : TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, 不是 str

python - 在 Python CFFI 中声明包含 time_t 字段的结构

c++ - 如何将输入存储到数组中? C++

python - 在 python 中展开列表

python - 从 Javascript 到 Python - 了解类、方法和属性的工作原理

Python 分组两个列表

python - 如何使用 CFFI 将包含其 header 的 C 库包装到 python 程序中?

c++ - main.obj : fatal error LNK1143: invalid or corrupt file: no symbol for COMDAT section 0x6

c++ - 字节冲突

c++ - 在 std 算法中否定 boost is_directory