Home > Net >  C # call c encapsulated DLL a byte alignment structure problems
C # call c encapsulated DLL a byte alignment structure problems

Time:09-29

A. C encapsulation of DLL are defined as follows:
1. C encapsulation DLL library structure defined in
The # pragma pack (1)
Typedef struct ZFY_INFO_S {
Char cSerial [7].
Char userNo [6].
Char userName [32].
Char unitNo [12].
Char unitName [32].
} ZFY_INFO;
2. C encapsulated DLL library interfaces defined in
Extern__stdcall WriteZFYInfo (ZFY_INFO * info, char * sPwd, unsigned short * iRet);
2. The transfer process in c # is as follows:
1. The structure of the definition of c #
[StructLayout (LayoutKind. Sequential, Pack=2)]
Public struct ZFY_INFO
{
[MarshalAs (UnmanagedType ByValArray, SizeConst=7)]
Public byte [] cSerial;
[MarshalAs (UnmanagedType ByValArray, SizeConst=6)]
Public byte [] userNo;
[MarshalAs (UnmanagedType ByValArray, SizeConst=32)]
Public byte [] the userName;
[MarshalAs (UnmanagedType ByValArray, SizeConst=12)]
Public byte [] unitNo;
[MarshalAs (UnmanagedType ByValArray, SizeConst=32)]
Public byte [] unitName;
};
2. In c # call interface transfer process of structure parameter
Var info=new ZFY_T5. ZFY_INFO
{
CSerial=new byte [7],
UserNo=new byte [6],
The userName=new byte [32],
UnitNo=new byte [12],
UnitName=new byte [32]
};
Var buff=Encoding. Unicode. GetBytes (" ");
Array. Copy (buff, info. The userName, buff. Length);
Buff=Encoding. Unicode. GetBytes (" ");
Array. Copy (buff, info. UnitName, buff. Length);

Buff=Encoding. The ASCII. GetBytes (p1Val);
Array. Copy (buff, info. UserNo, buff. Length);
Buff=Encoding. The ASCII. GetBytes (p5Val);
Array. Copy (buff, info. CSerial, buff. Length);
Buff=Encoding. The ASCII. GetBytes (" ");
Array. Copy (buff, info. UnitNo, buff. Length);

RTN=ZFY_T5. WriteZFYInfo (ref info, PSW, ref iRet);
If (RTN)
{
return 0;
}
return 1;

Conclusion: the question now is in c # this way call transfer structure parameters in the device display Settings of the unknown parameters is wrong, what reason is this?

CodePudding user response:

# pragma pack ( 1 ) & lt; 1
-- -
[StructLayout (LayoutKind. Sequential, Pack= 2 )] <- 2 ?

CodePudding user response:

C # structure in the body, remove [MarshalAs (UnmanagedType ByValArray, SizeConst=7)] features, can be defined as a byte [] directly

CodePudding user response:

Char is a symbol in c, c # should be used in sbyte corresponding,
  •  Tags:  
  • C#
  • Related