首页 > 解决方案 > 从 Python (ctypes) 指向 C 以保存函数输出的指针

问题描述

我是 Python 中 C 集成的新手。我目前正在使用ctypes将 .dll 库包装到我的 Python 代码中,并且在传递指针以保存特定函数的输出时遇到问题。

我的 C 函数具有以下结构:

函数(int* w, int* h, unsigned short* data_output)

其中hw是输入,data_output是大小为(wxh, 1)的数组。

通过创建zeros(wxh,1)数组并使用libpointer('uint16Ptr', zeros(wxh, 1))将其作为指针传递,我能够成功地从 Matlab 中的函数集成和检索结果。

我怎样才能在 Python 中做到这一点?

对于输出为int*类型的其他函数,我能够使用create_string_buffer成功检索值。但我还没有设法使它适用于这个。

谢谢你。

标签: python-3.xctypes

解决方案


根据[SO]: How to create a Minimal, Complete, and Verifiable example (mcve),您的问题不包含基本信息(例如您的尝试)。确保在下一个中更正。

此外,[Python.Docs]: ctypes - Python 的外部函数库

这是一个说明该概念的虚拟示例。

dll00.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;
}

代码00.py

#!/usr/bin/env python3

import sys
import ctypes as ct


DLL = "./dll00.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(*argv):
    dll_dll = ct.CDLL(DLL)
    function = dll_dll.function
    function.argtypes = [ct.c_int, ct.c_int, ct.POINTER(ct.c_ushort)]
    function.restype = ct.c_int

    h = 3
    w = 5

    ArrayType = ct.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(w, h, ct.cast(array, ct.POINTER(ct.c_ushort)))
    print("{:} returned: {:d}".format(function.__name__, res))

    print_array(array, h, w)


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

输出

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054753828]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
[064bit prompt]> ls
code00.py  dll00.c
[064bit prompt]> gcc -fPIC -shared -o dll00.so dll00.c
[064bit prompt]> ls
code00.py  dll00.c  dll00.so
[064bit prompt]> python3 code00.py
Python 3.6.4 (default, Jan  7 2018, 15:53:53) [GCC 6.4.0] 064bit 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

Done.

推荐阅读