从Python(ctypes)到C的指针以保存函数输出 [英] Pointer from Python (ctypes) to C to save function output
问题描述
我是Python的C集成的新手.我目前正在使用 ctypes 将.dll库包装到我的Python代码中,并且在传递指针以保存特定函数的输出时遇到问题.
I'm new to C integration in Python. I'm currently wrapping a .dll library into my Python code using ctypes and I'm having issues passing a pointer to save the output of a particular function.
我的C函数具有以下结构:
My C function has the following structure:
函数(int * w,int * h,无符号short * data_output)
function (int* w, int* h, unsigned short* data_output)
其中 h 和 w 是输入,而 data_output 是大小为(wxh,1)的数组.
Where h and w are inputs and data_output is an array of size (w x h, 1).
我能够通过创建 zeros(wxh,1)数组并使用 libpointer('uint16Ptr'零(wxh,1)).
I was able to successfully integrate and retrieve results from the function in Matlab by creating a zeros(w x h,1) array and passing it as a pointer using libpointer('uint16Ptr', zeros(w x h, 1)).
如何在Python中做到这一点?
How can I do that in Python?
对于其他函数,其输出为 int * 类型,我能够使用 create_string_buffer 成功检索值.但是我没有设法使它适用于这个.
For other functions, where the output was of int* type I was able to successfully retrieve the values using create_string_buffer. But I haven't managed to make it work for this one.
谢谢.
推荐答案
根据 [SO]:如何创建最小的,完整的和可验证的示例(mcve),您的问题不包含基本信息(例如您的尝试).请确保在接下来的内容中予以更正.
According to [SO]: How to create a Minimal, Complete, and Verifiable example (mcve), your question doesn't contain basic info (e.g. your attempts). Make sure to correct that in the next ones.
此外, [Python 3]:ctypes-外部函数库对于Python .
这是一个虚拟的例子,说明了这个概念.
Here's a dummy example that illustrates the concept.
dll.c :
#if defined(_WIN32)
# define DLL_EXPORT __declspec(dllexport)
#else
# define DLL_EXPORT
#endif
DLL_EXPORT int function(int w, int h, unsigned short *pData) {
int k = 1;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++, k++)
pData[i * w + j] = k;
return w * h;
}
code.py :
#!/usr/bin/env python3
import sys
import ctypes
DLL = "./dll.so"
def print_array(data, h, w):
for i in range(h):
for j in range(w):
print("{:2d}".format(data[i * w + j]), end=" ")
print()
def main():
dll_dll = ctypes.CDLL(DLL)
function_func = dll_dll.function
function_func.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_ushort)]
function_func.restype = ctypes.c_int
h = 3
w = 5
ArrayType = ctypes.c_ushort * (h * w) # Dynamically declare the array type: `unsigned short[15]` in our case
array = ArrayType() # The array type instance
print_array(array, h, w)
res = function_func(w, h, ctypes.cast(array, ctypes.POINTER(ctypes.c_ushort)))
print("{:} returned: {:d}".format(function_func.__name__, res))
print_array(array, h, w)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
输出:
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054753828]> ls
code.py dll.c
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054753828]> gcc -fPIC -shared -o dll.so dll.c
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054753828]> ls
code.py dll.c dll.so
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054753828]> python3 code.py
Python 3.6.4 (default, Jan 7 2018, 15:53:53)
[GCC 6.4.0] on cygwin
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
function returned: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
这篇关于从Python(ctypes)到C的指针以保存函数输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!