How to pass a C structure in Python

时间:2015-06-15 14:36:21

标签: python c function structure ctypes

I am trying to pass a structure into a function using python. Currently, my code is:

import ctypes

Mydll = ctypes.cdll.LoadLibrary("c\...place")
success=Mydll.ConnectToServer

Class PULSETRIGGER_SETUP(ctypes.structure):
    _fields_ = [("dpulseFrequency", ctypes.c_double),
                ("iPulsesA",ctypes.c_int)
                ("iPulsesB",ctypes.c_int)]

sPulseTriggerSetup=PULSETRIGGER_SETUP(200.0,2,2)
print(Mydll.PulseTriggerSetupSet(ctypes.byref(sPulseTriggerSetup)))#-1
print(Mydll.PulseTriggerSetupSet(ctypes.addressof(sPulseTriggerSetup)))#-1
print(Mydll.PulseTriggerSetupSet(ctypes.pointer(sPulseTriggerSetup)))#-1
print(Mydll.PulseTriggerSetupSet(sPulseTriggerSetup))#WindowsError: Exception: access violation reading 0X00000000

When I run this code, the errors produced by each method of passing the PULSETRIGGER_SETUP strucure is shown as comments.

The PulseTriggerSetupSet function is:

int PulseTriggerSetupSet(PULSETRIGGER_SETUP &sPulseTriggerSetup)

where -1 is an error and 0 is success. The C structure for PULSETRIGGER_SETUP is:

typedef struct { 
    double dPulseFrequency; 
    int iPulsesA; 
    int iPulsesB; 
} PULSETRIGGER_SETUP;

Is there a better way to create the sPulseTriggerSetup structure or to pass it into the PulseTriggerSetupSet function?

0 个答案:

没有答案