简体   繁体   English

如何使用 ctypes 查看 python 中的指针值变化

[英]How can I see pointer value change in python using ctypes

I want to implement void function in C and in that function I will change pointer value, like this:我想在 C 和 function 中实现 void function 我将更改指针值,如下所示:

void func(int* k){ *k = 9; }

As you can see, here I am changing k value, let's say with 9. How can I write this function in python using ctypes so I can see what is the changed value is, in C.如您所见,这里我正在更改 k 值,假设为 9。如何使用 ctypes 在 python 中编写此 function 以便我可以看到更改后的值是什么,在 Z0D61F8370CAD1D4D412F80B84D17 中lets say following python code example假设遵循 python 代码示例

import ctypes
d = ctypes.CDLL(r'mydll.dll')
d.our_function.argtypes = (ctypes.POINTER(ctypes.c_int))

As you can see there is no return, so how can I see in my python call, that my value is changed?如您所见,没有返回,那么我如何在 python 调用中看到我的值已更改? sorry for my bad English...对不起,我的英语不好...

You need to allocate an int yourself in Python and pass its address, like this:您需要自己在 Python 中分配一个int并传递其地址,如下所示:

k = c_int()
d.our_function(byref(k))
print(k.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM