using c++ dll in asp.NET c# project?

using c++ dll in asp.NET c# project?

本文关键字:project NET asp c++ dll in using      更新时间:2023-10-16

我在.NET项目中使用了一个dll。我试着用c#来管理它,并且存在函数使用_H_ cardData_MG结构和返回_H_。

    c++
int __Read_And_Get_Card_Data ( DWORD deviceSerialNo, DWORD password, DWORD ip, WORD port, 
                                   _H_cardData_MG *cardData )
    C#   
      public static  extern int __Read_And_Get_Card_Data(System.UInt32 deviceSerialNo, System.UInt32 password, System.UInt32 ip, System.UInt16 port, ref _H_cardData_MG kartdata);

如上所述,我试图将头文件更改为c#class,但我想我不能定它们的尺寸?

错误为"指定的数组不是预期的类型。"你知道这是怎么回事吗??我可以只通过字节数组获取值吗?我的意思是;

 byte[] bytarr = new byte[256] 
public static  extern int __Read_And_Get_Card_Data(System.UInt32 deviceSerialNo, System.UInt32 password, System.UInt32 ip, System.UInt16 port, ref byte[] bytarr);

但我收到错误:引发了"System.ExecutionEngineException"类型的异常

c++头文件代码;

struct _H_cardData_MG 
{ 
int cardCode; 
int credit; 
int minCredit; 
WORD reserved01; 
WORD endUserCode; 
WORD password; 
BYTE groupCode; 
BYTE cardType:2; 
BYTE disabled:1; 
BYTE outOfCtrlDirection:1; 
BYTE bonusUsage:2; 
BYTE :2; 
char name[16]; 
struct _H_lastAccess_MF appData[4]; 
BYTE creditMultipler; 
BYTE bonusRatio; 
BYTE reserved02; 
BYTE reserved03; 
int bonus; 
BYTE reserved04[4]; 
}; 

struct _H_lastAccess_MF // size=12 
{ WORD lastUsedHourMin:11; 
WORD in:1; 
WORD :4; 
SHORT lastUsedDate; 
WORD wStart:11; 
WORD limitCnt:3; 
WORD :2; 
WORD wEnd:11; 
WORD limitMax:3; 
WORD :2; 
WORD expireDate; 
WORD expireHourMin:11; 
WORD prePaymentCredit:5; 
}; 

C#编码

public class _H_cardData_MG 
{ 
public int cardCode { get; set; } 
public int credit { get; set; } 
public int minCredit { get; set; } 
public UInt16 reserved01 { get; set; } 
public UInt16 endUserCode { get; set; } 
public UInt16 password { get; set; } 
public byte groupCode { get; set; } 
public byte cardType { get; set; } 
public byte disabled { get; set; } 
public byte outOfCtrlDirection { get; set; } 
public byte bonusUsage { get; set; } 
public byte bos { get; set; } 
public char[] name = new char[16] ; 
public _H_lastAccess_MF [] appData = new _H_lastAccess_MF [4]; 
public byte creditMultipler{ get; set; } 
public byte bonusRatio{ get; set; } 
public byte reserved02{ get; set; } 
public byte reserved03{ get; set; } 
public int bonus{ get; set; } 
public byte[] reserved04 = new byte[4]; 
}; 
public struct _H_lastAccess_MF // size=12 
{ 
public UInt16 lastUsedHourMin { get; set; } 
public UInt16 zin { get; set; } 
public UInt16 bos1 { get; set; } 
public short lastUsedDate { get; set; } 
public UInt16 wStart{ get; set; } 
public UInt16 limitCnt{ get; set; } 
public UInt16 bos2{ get; set; } 
public UInt16 wEnd{ get; set; } 
public UInt16 limitMax{ get; set; } 
public UInt16 bos3 { get; set; } 
public UInt16 expireDate { get; set; } 
public UInt16 expireHourMin{ get; set; } 
public UInt16 prePaymentCredit{ get; set; } 
}; 

我做错了什么,或者有什么办法吗???

这应该适用于C#结构来保存您想要的数据:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct _H_cardData_MG {
    /// int
    public int cardCode;
    /// int
    public int credit;
    /// int
    public int minCredit;
    /// WORD->unsigned short
    public ushort reserved01;
    /// WORD->unsigned short
    public ushort endUserCode;
    /// WORD->unsigned short
    public ushort password;
    /// BYTE->unsigned char
    public byte groupCode;
    /// cardType : 2
    ///disabled : 1
    ///outOfCtrlDirection : 1
    ///bonusUsage : 2
    ///AnonymousMember1 : 2
    public uint bitvector1;
    /// char[16]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=16)]
    public string name;
    /// _H_lastAccess_MF[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.Struct)]
    public _H_lastAccess_MF[] appData;
    /// BYTE->unsigned char
    public byte creditMultipler;
    /// BYTE->unsigned char
    public byte bonusRatio;
    /// BYTE->unsigned char
    public byte reserved02;
    /// BYTE->unsigned char
    public byte reserved03;
    /// int
    public int bonus;
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] reserved04;
    public uint cardType {
        get {
            return ((uint)((this.bitvector1 & 3u)));
        }
        set {
            this.bitvector1 = ((uint)((value | this.bitvector1)));
        }
    }
    public uint disabled {
        get {
            return ((uint)(((this.bitvector1 & 4u) 
                        / 4)));
        }
        set {
            this.bitvector1 = ((uint)(((value * 4) 
                        | this.bitvector1)));
        }
    }
    public uint outOfCtrlDirection {
        get {
            return ((uint)(((this.bitvector1 & 8u) 
                        / 8)));
        }
        set {
            this.bitvector1 = ((uint)(((value * 8) 
                        | this.bitvector1)));
        }
    }
    public uint bonusUsage {
        get {
            return ((uint)(((this.bitvector1 & 48u) 
                        / 16)));
        }
        set {
            this.bitvector1 = ((uint)(((value * 16) 
                        | this.bitvector1)));
        }
    }
    public uint AnonymousMember1 {
        get {
            return ((uint)(((this.bitvector1 & 192u) 
                        / 64)));
        }
        set {
            this.bitvector1 = ((uint)(((value * 64) 
                        | this.bitvector1)));
        }
    }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct _H_lastAccess_MF {
    /// lastUsedHourMin : 11
    ///in : 1
    ///AnonymousMember1 : 4
    public uint bitvector1;
    /// SHORT->short
    public short lastUsedDate;
    /// wStart : 11
    ///limitCnt : 3
    ///AnonymousMember2 : 2
    ///wEnd : 11
    ///limitMax : 3
    ///AnonymousMember3 : 2
    public uint bitvector2;
    /// WORD->unsigned short
    public ushort expireDate;
    /// expireHourMin : 11
    ///prePaymentCredit : 5
    public uint bitvector3;
    public uint lastUsedHourMin {
        get {
            return ((uint)((this.bitvector1 & 2047u)));
        }
        set {
            this.bitvector1 = ((uint)((value | this.bitvector1)));
        }
    }
    public uint @in {
        get {
            return ((uint)(((this.bitvector1 & 2048u) 
                        / 2048)));
        }
        set {
            this.bitvector1 = ((uint)(((value * 2048) 
                        | this.bitvector1)));
        }
    }
    public uint AnonymousMember1 {
        get {
            return ((uint)(((this.bitvector1 & 61440u) 
                        / 4096)));
        }
        set {
            this.bitvector1 = ((uint)(((value * 4096) 
                        | this.bitvector1)));
        }
    }
    public uint wStart {
        get {
            return ((uint)((this.bitvector2 & 2047u)));
        }
        set {
            this.bitvector2 = ((uint)((value | this.bitvector2)));
        }
    }
    public uint limitCnt {
        get {
            return ((uint)(((this.bitvector2 & 14336u) 
                        / 2048)));
        }
        set {
            this.bitvector2 = ((uint)(((value * 2048) 
                        | this.bitvector2)));
        }
    }
    public uint AnonymousMember2 {
        get {
            return ((uint)(((this.bitvector2 & 49152u) 
                        / 16384)));
        }
        set {
            this.bitvector2 = ((uint)(((value * 16384) 
                        | this.bitvector2)));
        }
    }
    public uint wEnd {
        get {
            return ((uint)(((this.bitvector2 & 134152192u) 
                        / 65536)));
        }
        set {
            this.bitvector2 = ((uint)(((value * 65536) 
                        | this.bitvector2)));
        }
    }
    public uint limitMax {
        get {
            return ((uint)(((this.bitvector2 & 939524096u) 
                        / 134217728)));
        }
        set {
            this.bitvector2 = ((uint)(((value * 134217728) 
                        | this.bitvector2)));
        }
    }
    public uint AnonymousMember3 {
        get {
            return ((uint)(((this.bitvector2 & 3221225472u) 
                        / 1073741824)));
        }
        set {
            this.bitvector2 = ((uint)(((value * 1073741824) 
                        | this.bitvector2)));
        }
    }
    public uint expireHourMin {
        get {
            return ((uint)((this.bitvector3 & 2047u)));
        }
        set {
            this.bitvector3 = ((uint)((value | this.bitvector3)));
        }
    }
    public uint prePaymentCredit {
        get {
            return ((uint)(((this.bitvector3 & 63488u) 
                        / 2048)));
        }
        set {
            this.bitvector3 = ((uint)(((value * 2048) 
                        | this.bitvector3)));
        }
    }
}

希望这是你想要的:)

方法p/Invoke签名应该如下所示:

[System.Runtime.InteropServices.DllImportAttribute("Your.dll", EntryPoint="__Read_And_Get_Card_Data")]
public static extern  int __Read_And_Get_Card_Data(uint deviceSerialNo, uint password, uint ip, ushort port, ref _H_cardData_MG cardData) ;