使用c++;asp.NET c#项目中的dll?

使用c++;asp.NET c#项目中的dll?,c#,c++,asp.net,dll,header-files,C#,C++,Asp.net,Dll,Header Files,我在.NET项目中使用了一个dll。 我试着用c#来管理它, 还有函数use_H_cardData_MG struct和return_H_cardData_MG值 c++ int __Read_And_Get_Card_Data ( DWORD deviceSerialNo, DWORD password, DWORD ip, WORD port, _H_cardData_MG *cardData ) C#

我在.NET项目中使用了一个dll。 我试着用c#来管理它, 还有函数use_H_cardData_MG struct和return_H_cardData_MG值

    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#类,如上图所示, 但是我想我不能确定他们的尺寸

错误为“指定的数组不是预期的类型。” 这有什么问题吗?你知道吗?? 我可以只通过字节数组获取值吗? 我是说

但我得到一个错误:抛出了“System.ExecutionEngineeException”类型的异常

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]; 
}; 

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#struct来说,这应该可以用来保存所需的数据:

[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) ;

我看不出你在哪里说新字节[256]。为什么不直接调用C++ DLL?“不起作用”-爱它很抱歉投票失败,但澄清你的问题…你想做什么?问题出在哪里!看看C++的一些示例代码。如果你是分配字节数组的那个,你需要知道它必须提前多久(否则C++库会对你的内存做什么)。此外,您的C#struct正在使用属性——您必须使用字段,即公共字节cardType而不是
公共字节cardType{get;set;}
-并且它必须是一个结构,而不是类。在使用C++数组的任何地方都要小心——它们是非托管的,甚至不能问它们有多长,你需要从其他地方知道。谢谢回答,当我做STRCT时,我不能说公开的HyLaSTAccess MF[] AppDATABOR=新的HyLaSTAccess MF(4);4是用C++头文件写的,我怎么说4?公共字符[]名称=新字符[16];阿尔斯也有同样的问题???我用了非常非常方便的工具来制作这个。希望它对你有用:)@luan是的,绝对是这样:)非常感谢你的帮助;)@Luann我如何使用该产品??有没有什么例子?如果你有一个C++头文件,你只要把所有相关的东西放到最后一个选项卡上的左上角文本框中,点击生成。它将在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)));
        }
    }
}
[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) ;