• 0

Problem with Delphi's record to C#


Question

I have a Delphi dll ( no source code) definition here:

 TRealRecordInfo = packed record
    Size: Integer; 
    CardNo: array[0..17] of char;
    timeString: array[0..15]of char;
    IsIDCard: Boolean; 
    NoCard: Boolean; 
    Reader: Integer; 
    Flag:   Integer; 
    Mark:   Integer;
    times:  Integer; 
    balance: Integer;
    consume: Integer; 
    sign_time: TDateTime;

    OverPwd: array[0..9] of char; 
    reserved1: DWORD; 
    reserved2: DWORD;

    Clock_ver    : Integer;
    Clock_ID     : Integer; 
    POS_Sequ     : Integer; 
    Card_Sequ    : Integer; 
    Op_CardNo    : array[0..19] of Char; 
    Date_ver     : array[0..19] of Char;
    reserved3    : array[0..19] of Char; 
    reserved4    : array[0..19] of Char; 
  end;

I've tried to define it in C# like this:

        [StructLayoutAttribute(LayoutKind.Sequential,CharSet = CharSet.Ansi,Pack=1)]
        public struct TRealRecordInfo
        {

            public int Size;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
            public string CardNo;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string TimeString;

            public bool IsIDCard;

            [MarshalAs(UnmanagedType.Bool)]
            public bool NoCard;

            public int Reader;

            public int Flag;

            public int Mark;

            public int Times;

            public int Balance;

            public int Consume;

            [MarshalAs(UnmanagedType.R8)]
            public double Sign_Time;

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
            public string OverPassword;

            public int DwReserved1;

            public int DwReserved2;

            public int Clock_ver;

            public int Clock_ID;

            public int POS_Sequ;

            public int Card_Sequ;

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

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

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

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

        }

The TRealRecordInfo 's size should be 186 :

( in delphi code Sizeof(TRealRecordInfo) = 186 ),

but when I used struct defined above in my c# code, i got the size 192.

(

TRealRecordInfo arcd = new TRealRecordInfo(); //is this correct way to allocate a instance of TRealRecordInfo?

arcd.Size = Marshal.SizeOf(arcd); //got 192

)

The size in memory is different, so the related API calling (with DllImport) methods will fail to get the right result.

Can anybody help me to finger out the right way to marshal the structure in C#?

Any help will be appriciated.

Link to comment
https://www.neowin.net/forum/topic/958976-problem-with-delphis-record-to-c/
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.