P / Invoke typedef结构到C#

时间:2013-10-01 15:22:09

标签: c# c struct pinvoke

我已经根据我在C中给出的内容在C#中创建了结构。 我仍在学习P / Invoke的绳索,但我不认为这太难了。 第一个结构是请求结构。第二个结构是响应结构。 它确实在第二个结构中返回信息,但是一些值是“正确的”而一些是“错误的”。所以我认为必须是我在c#

中设置结构的方式

我希望有人可以查看我的c#代码,看看我做的是否正确。 感谢。

请求结构:

typedef struct _tagGiftCardReqAdminCard
{
  char Password[9];
  char OperatorID[9];
  char Invoice[11];
  char CardNumber[80];
  char filler2[57];
  char FirstName[21];
  char filler3[126];
  long InitialBalance;
  char FinTrack2[40];
  char filler4[6];
  char ReferenceNumber[13];
  char filler5;
} TGiftCardReqAdminCard, *pTGiftCardReqAdminCard;

在C#中

    [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
    public struct _tagGiftCardReqAdminCard
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
        public string Password;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
        public string OperatorID;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
        public string Invoice;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string CardNumber;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 57)]
        public string filler2;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
        public string FirstName;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 126)]
        public string filler3;

        public int InitialBalance;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
        public string FinTrack2;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
        public string filler4;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
        public string ReferenceNumber;

        public byte filler5;
    }

响应结构

typedef struct _tagGiftCardRespAdminCard
{
  char Result;
  char TerminalId[17];
  char filler1[257];
  char Display[41];
  char Date[11];
  char Time[9];
  char RespCode[4];
  char InvNum[11];
  char filler2;
  char CustLang;
  char UserLang;
  char CardNum[20];
  char ExpDate[5];
  char ReferenceNumber[11];
  char RcptMsg[256];
  char filler3[353];
  long InitialBalance;
  char filler4[8];
  long LifeTimeBenefit;
  long RemainingBalance;
  char filler5[22];
  char MerchantNumber[17];
  char StoreNumber[17];
  char filler6[2];
  char BatchNum[14];
  char Password[9];
  char filler7[4];
  char OrigTransCode[6];
} TGiftCardRespAdminCard, *pTGiftCardRespAdminCard;

在C#中

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct _tagGiftCardRespAdminCard
{
    public byte Result;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
    public string TerminalId;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
    public string filler1;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 41)]
    public string Display;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
    public string Date;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
    public string Time;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
    public string RespCode;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
    public string InvNum;

    public byte filler2;

    public byte CustLang;

    public byte UserLang;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    public string CardNum;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
    public string ExpDate;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
    public string ReferenceNumber;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string RcptMsg;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 353)]
    public string filler3;

    public int InitialBalance;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    public string filler4;

    public int LifeTimeBenefit;

    public int RemainingBalance;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 22)]
    public string filler5;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
    public string MerchantNumber;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
    public string StoreNumber;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
    public string filler6;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
    public string BatchNum;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
    public string Password;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
    public string filler7;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
    public string OrigTransCode;
}

0 个答案:

没有答案