托管 PInvoke 签名与非托管目标签名不匹配
时遇到了麻烦,
当我将此互操作代码从 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;
}
我无法让它们匹配,而且我已经没有想法了。你们觉得怎么样?
I'm having trouble when I ported this Interop Code from VB6 to 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;
}
I can't get them to match, and I just ran out of ideas. What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未做过 VB6 到 C# 的互操作。但我认为您在 .Net 结构中使用了错误的 C# 数据类型。
根据 Visual Basic 的摘要 6.0 数据类型
Integer
的大小为 2 字节,Long
的大小为 4 字节。因此,对于 VB6
Integer
,您应该使用short
(Int16
) 数据类型,对于Long
您应该使用int
(Int32
) 数据类型。希望这有帮助。
I've never done VB6 to C# interop. But I think you are using the wrong C# data types in your .Net structure.
According to this summary of Visual Basic 6.0 data types a
Integer
is 2 Bytes in size and aLong
is 4 Bytes in size.So for a VB6
Integer
you should use theshort
(Int16
) data type and forLong
you should use theint
(Int32
) data type.Hope, this helps.