如何在CFFI中找到指向结构的指针的地址并将其强制转换为void**
How to find address of a pointer to structure and cast it to void** in CFFI
我在C++中的代码是
StructureEx* obj; // structure
functionEx((void**)&obj);
我的功能是
int functionEx(void** obj); //calling function
我是CFFI的新手。所以我的问题是
我如何在CFFI中实现同样的目标?
如何在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;
}
相关文章:
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将尾部调用void(i32,..)位转换为llvm::函数以获取FnAttribute
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- Boost buffer_cast 无法从 void* 转换为 PointerToPodType
- 将通用引用强制转换为可调用的 void 指针,反之亦然
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 在不同的编译器上转换为 void**
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- 不能在 if 语句 - c++ 中使用 void func 直接将字符串转换为大写
- 是否可以将已经初始化的变量转换为 void*?
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- 将可变参数模板类型转换为 void,预期')'之前
- 如何在运行时将指向派生类的 void* 正确转换为指向基类的 void*
- 是否强制转换void**并将第一个字节设置为nullptr
- 如何转换 void* 以访问结构的静态数据成员?
- 警告传递"double"以转换"void moveto(int, int)"的 2
- 是否有一种方法可以自动转换void指针的类型
- 在构造函数中强制转换void*并访问值
- 重新解释不带类型标识符的强制转换void
- 强制转换void指针