如何在Python中使用C/C++结构体等复杂类型

 我来答
从空去听8
2017-11-28 · TA获得超过7441个赞
知道大有可为答主
回答量:6907
采纳率:93%
帮助的人:5615万
展开全部
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()
打完收工。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式