在C ++中,我接受一个int类型的变量,并将其地址传递给python。地址为十六进制,并作为字符串传递给python。在python中,我将其转换为int类型,并使用ctypes.cast()。但是它崩溃了。
在python中,
class testClass:
def testMethod(actorArg, stringArg):
a = int(stringArg,16) #stringArg is the hex address in string type
print(type(a)) #gives int type
print(ctypes.cast(a, ctypes.py_object).value) #crashes!
基本思想是我在python中从这个ctypes那里得到的,
import ctypes
s = 10
print(id(s)) #gives the address of s
print(ctypes.cast(id(s), ctypes.py_object).value) #gives the value from address
总之,我想通过c ++发送的地址使用python获取价值。知道我做错了什么吗?预先感谢。
如果a
是同一过程中C整数的正确地址,则您要:
value = ctypes.c_int.from_address(a)
您的第二个代码与s
一起使用,因为s
是Python整数,而不是C整数。