如何在Python中使用C/C++结构体等复杂类型
1个回答
展开全部
C封装了一个Dll,名为SpjMatlabTest.dll,暴露实现:
typedef struct{
double *Min;
double *Max;
} ST_TESTLIMIT;
typedef struct{
int NumVDIVs;
int NumPts;
double *TestedVDIVs; // Pointer to first element in array that is 1xNumVDIVs long.
double *TestPtsinFracFS; // Pointer to first element in array that is 1xNumPts long.
double *VerrinFracFS; // Pointer to first element in array that is NumVDIVs*NumPts*NumChans long.
double DCAcc;
double OffConstAcc;
double *GainErrorPct; // Pointer to first element in array of NumVDIVs*NumChans long.
double *OffsetErrorFracFS; // Pointer to first element in array of NumVDIVs*NumChans long.
double *MaxNonLinearityFracFS; // Pointer to first element in array of NumVDIVs*NumChans long.
double GainErrorLimitPctAbs;
double TDIV;
} ST_LINACCD1M;
#include <windows.h>
#ifndef BUILD_DLL
//Define the function prototypes to be used by the calling application.
typedef bool (__cdecl *MATLABAPPINITIALIZE)(void);
typedef bool (__cdecl *MATLABAPPTERMINATE)(void);
typedef bool (__cdecl *LINACCD1M)(ST_LINACCD1M *stOutput, const bool *Channels, const char *ScopeAddress, const int GPIBAddress);
typedef void (__cdecl *LINACCD1MCLEANUP)(void);
#endif
现在需要在py代码中使用ST_LINACCD1M结构体。
测试代码:
from ctypes import *
class C_ST_LINACCD1M(Structure):
_fields_ = [('NumVDIVs', c_int),
('NumPts', c_int),
('TestedVDIVs', c_void_p),
('TestPtsinFracFS', c_void_p),
('VerrinFracFS', c_void_p),
('DCAcc', c_double),
('OffConstAcc', c_double),
('GainErrorPct', c_void_p),
('OffsetErrorFracFS', c_void_p),
('MaxNonLinearityFracFS', c_void_p),
('GainErrorLimitPctAbs', c_double),
('TDIV', c_double)]
import sys
def visit():
info = C_ST_LINACCD1M()
#print info.sth...
dllName = "SpjMatlabTest.dll"
dllload = windll.LoadLibrary(dllName)
print dllload
py_LINACCD1M = dllload.LINACCD1M
py_LINACCD1M.argtypes = [c_void_p, c_void_p, c_char_p, c_int]
#.restypep = c_void_p
para_in_1 = True
para_in_2 = c_char_p("/0")
para_in_3 = 0
nRetVal = dllload.LINACCD1M(byref(info), para_in_1, para_in_2, para_in_3)
#print info.sth...
if __name__ == "__main__":
visit()
打完收工。
typedef struct{
double *Min;
double *Max;
} ST_TESTLIMIT;
typedef struct{
int NumVDIVs;
int NumPts;
double *TestedVDIVs; // Pointer to first element in array that is 1xNumVDIVs long.
double *TestPtsinFracFS; // Pointer to first element in array that is 1xNumPts long.
double *VerrinFracFS; // Pointer to first element in array that is NumVDIVs*NumPts*NumChans long.
double DCAcc;
double OffConstAcc;
double *GainErrorPct; // Pointer to first element in array of NumVDIVs*NumChans long.
double *OffsetErrorFracFS; // Pointer to first element in array of NumVDIVs*NumChans long.
double *MaxNonLinearityFracFS; // Pointer to first element in array of NumVDIVs*NumChans long.
double GainErrorLimitPctAbs;
double TDIV;
} ST_LINACCD1M;
#include <windows.h>
#ifndef BUILD_DLL
//Define the function prototypes to be used by the calling application.
typedef bool (__cdecl *MATLABAPPINITIALIZE)(void);
typedef bool (__cdecl *MATLABAPPTERMINATE)(void);
typedef bool (__cdecl *LINACCD1M)(ST_LINACCD1M *stOutput, const bool *Channels, const char *ScopeAddress, const int GPIBAddress);
typedef void (__cdecl *LINACCD1MCLEANUP)(void);
#endif
现在需要在py代码中使用ST_LINACCD1M结构体。
测试代码:
from ctypes import *
class C_ST_LINACCD1M(Structure):
_fields_ = [('NumVDIVs', c_int),
('NumPts', c_int),
('TestedVDIVs', c_void_p),
('TestPtsinFracFS', c_void_p),
('VerrinFracFS', c_void_p),
('DCAcc', c_double),
('OffConstAcc', c_double),
('GainErrorPct', c_void_p),
('OffsetErrorFracFS', c_void_p),
('MaxNonLinearityFracFS', c_void_p),
('GainErrorLimitPctAbs', c_double),
('TDIV', c_double)]
import sys
def visit():
info = C_ST_LINACCD1M()
#print info.sth...
dllName = "SpjMatlabTest.dll"
dllload = windll.LoadLibrary(dllName)
print dllload
py_LINACCD1M = dllload.LINACCD1M
py_LINACCD1M.argtypes = [c_void_p, c_void_p, c_char_p, c_int]
#.restypep = c_void_p
para_in_1 = True
para_in_2 = c_char_p("/0")
para_in_3 = 0
nRetVal = dllload.LINACCD1M(byref(info), para_in_1, para_in_2, para_in_3)
#print info.sth...
if __name__ == "__main__":
visit()
打完收工。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询