【问题标题】:the managed PInvoke signature does not match the unmanaged target signature托管 PInvoke 签名与非托管目标签名不匹配
【发布时间】:2012-01-06 04:14:50
【问题描述】:

当我将此互操作代码从 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;
    }

我无法让它们匹配,我只是没有想法。大家觉得呢?

【问题讨论】:

    标签: c# vb6-migration


    【解决方案1】:

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

    根据这个summary的Visual Basic 6.0数据类型a

    1. VB6 Integer 的大小为 2 字节,
    2. VB6 Long 大小为 4 字节。

    因此,对于 VB6 Integer,您应该使用 short (Int16) 数据类型,对于 Long,您应该使用 int (Int32) 数据类型。

    希望,这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 2021-04-01
      • 2019-07-14
      • 1970-01-01
      相关资源
      最近更新 更多