托管 PInvoke 签名与非托管目标签名不匹配

发布于 2024-12-25 09:13:31 字数 1743 浏览 1 评论 0原文

时遇到了麻烦,

当我将此互操作代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2025-01-01 09:13:31

我从未做过 VB6 到 C# 的互操作。但我认为您在 .Net 结构中使用了错误的 C# 数据类型。

根据 Visual Basic 的摘要 6.0 数据类型

  1. VB6 Integer 的大小为 2 字节,
  2. VB6 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

  1. VB6 Integer is 2 Bytes in size and a
  2. VB6 Long is 4 Bytes in size.

So for a VB6 Integer you should use the short (Int16) data type and for Long you should use the int (Int32) data type.

Hope, this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文