0

我不太确定我可能做错了什么,但我不断收到 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);
4

1 回答 1

0

您需要向and数组添加一个MarshalAs属性。很可能是一个属性。padVpadAStructLayout

[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;
}
于 2013-04-11T02:45:58.087 回答