I made a project. the client is C # And the server is delphi7. many problems have been encountered during the implementation process. Now, let's summarize several important issues and hope to help you and avoid detours. (If someone does not agree, please smile and never leave a comment .)
I. Data Packet conversion problems in different environments.
1. struct can be used to encapsulate data in C. For example:
[Structlayout (layoutkind. Sequential)] // note that this statement must exist here.
Public struct body
{
[Financialas (unmanagedtype. byvaltstr, sizeconst = 8)] Public String password;
[Financialas (unmanagedtype. byvaltstr, sizeconst = 16)] Public String Tel;
Public int accid;
}
2. The corresponding data packet in Delphi can be represented as follows:
Tbody = packed record // package Definition
Password: array [0 .. 7] of char; // Password
Tel: array [0 .. 15] of char; // mobile phone number
Accid: integer; // number
End;
Everyone is familiar with the above packets. But pay attention to the length of the package type in C. That is, "Byte alignment"
To enable the CPU to efficiently and quickly access variables, the starting address of the variable should have some features,
That is, "alignment ". For example, for a 4-byte int type variable, its starting address should be on the 4-byte boundary,
That is, the start address can be divisible by four. The variable alignment rules are as follows (32-bit system ):
Type
Alignment
Char
Alignment on Byte Boundary
Short (16-bit)
Alignment on the dual-byte Boundary
INT and long (32-bit)
Alignment on the 4-byte Boundary
Float
Alignment on the 4-byte Boundary
Double
Alignment on the 8-byte Boundary
Structures
Consider the individual members of the struct, which are aligned on different byte boundaries.
The maximum number of boundary bytes is the number of boundary bytes of the structure.
Msdn original statement: largest alignment requirement of any member
Understanding the alignment of a struct is a bit scratching its head. If the struct contains struct members,
This is a recursive process.
Alignment affects the offset of struct members in the struct. the maximum number of alignment byte Boundary set by the compiler is N,
For a member item in the struct, It is aligned with the actual number of bytes at the first address of the structure X should meet
The following rules:
X = min (n, sizeof (item ))
For example, for struct {char a; int B} t;
When the 32-bit system is n = 8:
The offset of A is 0,
The offset of B is 4, 3 bytes are filled in the middle, and X of B is 4;
When the 32-bit system is n = 2:
The offset of A is 0,
The offset of B is 2, and 1 byte is filled in the middle. X of B is 2;
Sizeof of struct
Set the last member of the struct to lastitem.
Offset is offset (lastitem), its size is sizeof (lastitem), and the number of bytes in the struct is n,
The sizeof of struct is: If offset (lastitem) + sizeof (lastitem) can be divisible by N,
It is offset (lastitem) + sizeof (lastitem). Otherwise, it will be filled later,
Until it can be divisible by N.
Example: 32-bit system, n = 8,
Struct {char a; char B;} t;
Struct {char a; int B;} T1;
Struct {char a; int B; char C;} T2;
Sizeof (t) = 2; n = 1 not filled
Sizeof (t) = 8; n = 4 filled with 3 bytes
Sizeof (T2) = 12; n = 4 middle, each ending is filled with 3 bytes
Note:
1) For an empty struct, sizeof = 1; because every instance of the struct must be in the memory
Has a unique address.
2) The static members of the struct do not affect the size of the struct because the storage location of the static variables is
The instance address of the struct.
For example:
Struct {static int I;} t; struct {char a; static int I;} T1;
Sizeof (t) = 1; sizeof (T1) = 1;
3) Some compilers support extension instructions to set the alignment of variables or structures, such as Vc,
For details, see msdn (alignment of structures)
Not required # pragma pack (8), each member must be 8-byte aligned
A group of Members must be 8-byte aligned.
Struct S1
{
Short A; // 2 bytes
Long B; // 4 bytes
};
The entire S1 is smaller than 8 bytes, so S1 is 8 bytes.
Struct S2
{
Char C; // 1 byte
S1 D; // 8 bytes
_ Int64 E; // 8 bytes
};
The entire S2 is smaller than 12 bytes, but because # pragma pack (8) is limited, 12 cannot be aligned with 8 bytes, so S2 is 24 bytes, and C occupies 8 bytes
In general, Delphi's package must correspond to the type length in the C # structure and follow the "Byte alignment" principle.