我不太确定我可能做错了但是我一直在收到System.ArgumentException
当我查看IDL(由ITyteLib Veiwer生成)时,我可以看到结构和模块
typedef struct tagXxxStruct {
short type;
short file;
short rec;
short word;
short start_bit;
short length;
short flags;
short padding;
short value;
short padV[3];
short status;
short padA[3];
} XxxStruct;
[entry("dat"), helpstring("...")]
short _stdcall dat_Int(
[in] LPSTR Server,
[in] short num_points,
[in, out] SAFEARRAY(XxxStruct)* XxxStruct_data);
我在使用DllImport时如此:
[DllImport("hscnetapi.dll", EntryPoint = "dat", CallingConvention = CallingConvention.StdCall)]
public static extern unsafe short dat_SA([MarshalAs(UnmanagedType.LPStr)] string host, short num_points, [MarshalAs(UnmanagedType.SafeArray)] XxxStruct[] dat_data);
并称之为
public struct XxxStruct
{
public short type;
public short file;
public short rec;
public short word;
public short start_bit;
public short Length;
public short Flags;
public short padding;
public short value;
public short[] padV;
public short Status;
public short[] padA;
}
server = "localhost";
XxxStruct[] xobj= new XxxStruct[2];
for (i = 0; i <= 1; i++)
{
var _with1 = xobj[i];
_with1.type = 2;
_with1.file = 8;
_with1.rec = 1;
_with1.word = ((short)(359 + (i)));
_with1.Flags = 0;
_with1.padV = new short[3];
_with1.padA = new short[3];
xobj[i] = _with1;
}
dat_SA(server, (short)2, xobj);
答案 0 :(得分:0)
您需要向MarshalAs
和padV
数组添加padA
属性。很可能是StructLayout
属性。
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct XxxStruct
{
public short type;
public short file;
public short rec;
public short word;
public short start_bit;
public short Length;
public short Flags;
public short padding;
public short value;
[MarshalAs(UnmanagedType.LPArray, SizeConst=3)]
public short[] padV;
public short Status;
[MarshalAs(UnmanagedType.LPArray, SizeConst=3)]
public short[] padA;
}