使用int *成员将C ++结构编组到C#

时间:2014-11-26 11:17:57

标签: c# c++ marshalling

我在下面有一个C ++结构:

struct CTMDeviceError {
    struct CTMDeviceInfo sDeviceInfo;
    int32_t              iResultCode;
    int32_t              iExtendedResultCode;
    int32_t *            piDenomination;
    int32_t *            piChangeDue;
};

我创建了一个等效的c#结构,但我在编组int32 *类型时遇到了麻烦。

[StructLayout(LayoutKind.Sequential)]
public struct CTMDeviceError
{
    public CTMDeviceInfo deviceInfo;

    [MarshalAs(UnmanagedType.I4)]
    public Int32 resultCode;

    [MarshalAs(UnmanagedType.I4)]
    public Int32 extendedResultCode;

    public ??? denomination;

    public ??? changeDue;
};

我尝试过使用IntPtr或Int32 [],但Visual Studio显示不匹配错误。 我可以请一些建议吗? 谢谢!

这是结构细节的其余部分:

C ++

struct CTMDeviceInfo {
    enum CTMDeviceType eDeviceType;
    char *             szDeviceModel;
    char *             szDeviceSubModel;
    int32_t *          piDeviceID;
};

enum CTMDeviceType {
    CTM_DEVICETYPE_CASHCHANGER  = 5,
    CTM_DEVICETYPE_CASHACCEPTOR = 15,
    CTM_DEVICETYPE_COINACCEPTOR = 16,
    CTM_DEVICETYPE_OTHER        = 17
};

c#中

[StructLayout(LayoutKind.Sequential)]
public struct CTMDeviceInfo
{
    public CTMDeviceType deviceType;

    [MarshalAs(UnmanagedType.LPStr)]
    public string deviceModel;

    [MarshalAs(UnmanagedType.LPStr)]
    public string deviceSubModel;

    public ??? deviceId;
};

public enum CTMDeviceType
    {
        CTM_DEVICETYPE_CASHCHANGER = 5,
        CTM_DEVICETYPE_CASHACCEPTOR = 15,
        CTM_DEVICETYPE_COINACCEPTOR = 16,
        CTM_DEVICETYPE_OTHER = 17
    };

1 个答案:

答案 0 :(得分:1)

指针的类型应为System.IntPtr