如何通过C ++发送的地址使用python获取值?

问题描述 投票:0回答:1

在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获取价值。知道我做错了什么吗?预先感谢。

python c++ ctypes memory-address
1个回答
0
投票

如果a是同一过程中C整数的正确地址,则您要:

value = ctypes.c_int.from_address(a)

您的第二个代码与s一起使用,因为sPython整数,而不是C整数。

© www.soinside.com 2019 - 2024. All rights reserved.