当我将此Interop代码从VB6移植到C#
时,我遇到了麻烦VB6:
Type Dpi_t
dpiDrSuPsd(DPI_PRG_LEN) As Byte
dpiMyPort As Long
dpiHostAdr(DPI_MAX_HOST) As Byte
dpiHostCnt As Integer
dpiVoidCom As Long
dpiRspBdy As Long
dpiCmData As Long
dpiRdcxData As Long
dpiLstErr As Long
dpiONoUa As Byte
dpiOTooMuch As Byte
dpiOBar As Byte
dpiVPin As Byte
DpiPin As Long
dpiCda(DPI_CDA_LEN) As Byte
dpiEcCyc(DPI_CYC_LEN) As Byte
dpitemp(6000) As Byte
End Type
C#
[StructLayout(LayoutKind.Sequential)]
public struct Dpi_t
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DPI_PRG_LEN)]
public byte[] dpiDrSuPsd;
public long dpiMyPort;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DPI_MAX_HOST)]
public byte[] dpiHostAdr;
public int dpiHostCnt;
public long dpiVoidCom;
public long dpiRspBdy;
public long dpiCmData;
public long dpiRdcxData;
public long dpiLstErr;
public byte dpiONoUa;
public byte dpiOTooMuch;
public byte dpiOBar;
public byte dpiVPin;
public long DpiPin;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DPI_CDA_LEN)]
public byte[] dpiCda;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DPI_CYC_LEN)]
public byte[] dpiEcCyc;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6000)]
public byte[] dpitemp;
}
我无法让他们匹配,我只是没有想法。你们觉得怎么样?
答案 0 :(得分:2)
我从未做过VB6到C#interop。但我认为您在.Net结构中使用了错误的C#数据类型。
根据此summary的Visual Basic 6.0数据类型a
Integer
的大小为2字节,Long
的大小为4字节。因此,对于VB6 Integer
,您应该使用short
(Int16
)数据类型;对于Long
,您应该使用int
({{1}数据类型。
希望,这有帮助。