Python ctypes 指向结构数组的指针

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

我有一个与 ctypes and array of structs 类似的问题,但我不想返回结构。相反,我的结构作为指针传递到函数调用中,然后我想要指针指向的值。见下文:

来自.h:

typedef struct NIComplexNumberF32_struct {
   ViReal32 real;
   ViReal32 imaginary;
} NIComplexNumberF32;

ViStatus _VI_FUNC niRFSA_FetchIQSingleRecordComplexF32(
   ViSession vi,
   ViConstString channelList,
   ViInt64 recordNumber,
   ViInt64 numberOfSamples,
   ViReal64 timeout,
   NIComplexNumberF32* data,
   niRFSA_wfmInfo* wfmInfo);

我做了几次尝试,但这是我最近的一次:

import ctypes

class NIComplexNumberF32_struct_data(ctypes.Structure):
    _fields_ = [("real", ctypes.c_float),
                ("imaginary",  ctypes.c_float)]

class niRFSA_wfmInfo_struct_data(ctypes.Structure):
    _fields_ = [("absoluteInitialX", ctypes.c_double),
                ("relativeInitialX",  ctypes.c_double),
                ("xIncrement",  ctypes.c_double),
                ("actualSamples",  ctypes.c_double),
                ("offset",  ctypes.c_double),
                ("gain",  ctypes.c_double),
                ("reserved1",  ctypes.c_double),
                ("reserved2",  ctypes.c_double)                
                ]


# ViStatus _VI_FUNC niRFSA_FetchIQSingleRecordComplexF32(
#    ViSession vi,
#    ViConstString channelList,
#    ViInt64 recordNumber,
#    ViInt64 numberOfSamples,
#    ViReal64 timeout,
#    NIComplexNumberF32* data,
#    niRFSA_wfmInfo* wfmInfo);

#create instances
data = ctypes.POINTER(NIComplexNumberF32_struct_data)()
wfmInfo = ctypes.POINTER(niRFSA_wfmInfo_struct_data)()

dll_path = r"C:\Program Files\IVI Foundation\IVI\bin\NiRFSA_64.dll" 
dll = ctypes.cdll.LoadLibrary(dll_path)
dll.niRFSA_FetchIQSingleRecordComplexF32.argtypes =(ViSession,ViString,ViReal64,ViReal64,ViReal64,ctypes.POINTER(NIComplexNumberF32_struct_data),ctypes.POINTER(niRFSA_wfmInfo_struct_data) )
dll.niRFSA_FetchIQSingleRecordComplexF32(handle,bytes('','ascii'),0,1000,1,data,wfmInfo)

Traceback (most recent call last):
RuntimeError: (-1074134952) IVI: (Hex 0xBFFA0058) Null pointer passed for parameter or attribute.

我想要的是来自数据结构数组的真实/图像数据值。

我必须错过一些愚蠢的东西。希望有人能指出我正确的方向。

谢谢大家!!

python arrays structure ctypes
1个回答
0
投票

你创建了空指针。实例化实际结构并通过引用传递,例如,

ctypes.byref(data)
:

data = NIComplexNumberF32_struct_data()
wfmInfo = niRFSA_wfmInfo_struct_data()
© www.soinside.com 2019 - 2024. All rights reserved.