我需要从C DLL获取数据到C#应用程序。这是错误:
无法编组“KnittingWago.Common.WAGO_DATA_TO_USER_T”类型的字段“计数器”:无效的托管/非托管类型组合(数组字段必须与ByValArray或SafeArray配对)。
这是DLL .h
#define WAGO_NB_COUNTERS_C 80
#define WAGO_NB_ENCODERS_C 10
struct WAGO_DATA_TO_USER_T
{
unsigned int Counters[WAGO_NB_COUNTERS_C];
int Encoders[WAGO_NB_ENCODERS_C];
unsigned int Weight;
bool CalibrationValid;
bool LastCalibrationFailed;
};
这是C#Struct声明:
const int WAGO_NB_COUNTERS_C = 80;
const int WAGO_NB_ENCODERS_C = 10;
struct WAGO_DATA_TO_USER_T
{
[MarshalAs(UnmanagedType.U4, SizeConst = GlobalConstant.WAGO_NB_COUNTERS_C)]
UInt32[] Counters;
[MarshalAs(UnmanagedType.I4, SizeConst = GlobalConstant.WAGO_NB_ENCODERS_C)]
Int32[] Encoders;
UInt32 Weight;
Boolean CalibrationValid;
Boolean LastCalibrationFailed;
};
如何声明C#结构以获取没有错误的rigth数据?
由于
编辑:
我将Struct重写为:
internal struct WAGO_DATA_TO_USER_T
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = GlobalConstant.WAGO_NB_COUNTERS_C)]
UInt32[] Counters;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = GlobalConstant.WAGO_NB_ENCODERS_C)]
Int32[] Encoders;
UInt32 Weight;
Boolean CalibrationValid;
Boolean LastCalibrationFailed;
};
没有错误,但值不符合预期。
应该像:
Counter[0] = ~ 5000
Counter[1] = ~ 30000
Counter[2-79] = 0
真的是:
Counter[0] = 40
Counter[1] = 1080228
Counter[2] = 82964616
Counter[3-79] = All have a non-zero value
答案 0 :(得分:0)
编组是正确的,但我调用的设备未被外部非托管dll初始化。所以我正在读取导致无效数据的uninitialisez内存