0

Cで与えられたものに基づいて、C#で構造体を作成しました。私はまだ P/Invoke のコツを学んでいますが、これが難しすぎるとは思いませんでした。最初の構造体はリクエスト構造体です。2 番目の構造体は応答構造体です。2 番目の構造体で情報を返しますが、値の一部は「正しく」、一部は「間違っています」。だから私はそれが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;
}
4

0 に答える 0