以下C结构如何修改为Delphi结构,非常感谢!
struct __PACKET_BUFFER__
{
BYTE Status;
BYTE AclType;
BYTE NetType;
BYTE TimeType; ULONG AclId; union
{
struct
{
USHORT TcpCode : 6;
USHORT Reserved : 10;
};
struct
{
USHORT TcpFin : 1;
USHORT TcpSyn : 1;
USHORT TcpRst : 1;
USHORT TcpPsh : 1;
USHORT TcpAck : 1;
USHORT TcpUrg : 1; USHORT Direction : 1; // 0: IN, 1: OUT
USHORT SendOrRecv : 1;
USHORT Action : 8;
};
};
BYTE bReserved[2]; BYTE Protocol;
BYTE Week;
BYTE IcmpType;
BYTE IcmpSubType; ULONG Time;
ULONG SourceIp;
ULONG DestinationIp; union
{
ULONG Id;
struct
{
ULONG SourcePort : 16;
ULONG DestinationPort : 16;
};
}; ULONG DataBytes;
ULONG ProcessHandle; char sProcess[16];
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////
struct __PACKET_DIRECTION__
{
ULONG Id; struct
{
BYTE Direction : 4;
BYTE Action : 4;
}; BYTE NetType; struct
{
BYTE AclType : 4;
BYTE Reserved : 2;
BYTE DeleteIn : 1;
BYTE DeleteOut: 1;
}; BYTE Protocol; ULONG AclId;
ULONG Time;
ULONG ProcessHandle;
ULONG SendData;
ULONG RecvData;
ULONG LocalIp;
ULONG RemoteIp; USHORT LocalPort;
USHORT RemotePort; char sProcess[16];
};
struct __PACKET_BUFFER__
{
BYTE Status;
BYTE AclType;
BYTE NetType;
BYTE TimeType; ULONG AclId; union
{
struct
{
USHORT TcpCode : 6;
USHORT Reserved : 10;
};
struct
{
USHORT TcpFin : 1;
USHORT TcpSyn : 1;
USHORT TcpRst : 1;
USHORT TcpPsh : 1;
USHORT TcpAck : 1;
USHORT TcpUrg : 1; USHORT Direction : 1; // 0: IN, 1: OUT
USHORT SendOrRecv : 1;
USHORT Action : 8;
};
};
BYTE bReserved[2]; BYTE Protocol;
BYTE Week;
BYTE IcmpType;
BYTE IcmpSubType; ULONG Time;
ULONG SourceIp;
ULONG DestinationIp; union
{
ULONG Id;
struct
{
ULONG SourcePort : 16;
ULONG DestinationPort : 16;
};
}; ULONG DataBytes;
ULONG ProcessHandle; char sProcess[16];
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////
struct __PACKET_DIRECTION__
{
ULONG Id; struct
{
BYTE Direction : 4;
BYTE Action : 4;
}; BYTE NetType; struct
{
BYTE AclType : 4;
BYTE Reserved : 2;
BYTE DeleteIn : 1;
BYTE DeleteOut: 1;
}; BYTE Protocol; ULONG AclId;
ULONG Time;
ULONG ProcessHandle;
ULONG SendData;
ULONG RecvData;
ULONG LocalIp;
ULONG RemoteIp; USHORT LocalPort;
USHORT RemotePort; char sProcess[16];
};
{
BYTE AclType : 4;
BYTE Reserved : 2;
BYTE DeleteIn : 1;
BYTE DeleteOut: 1;
};D不支持这种格式的结构,你如果要写,只能写个函数来转换。
上面的意思是:
Byte总共占8位1--4位由AclType占用
5--6位由Reserved占用
7位由DeleteIn占用
8位由DeleteOut占用也就是说它们组合后,转成Delphi是一个Byte类型,但包含有4个C/C++语言的域的值。一般通过这种换算:
1..4
也就是二进制:
0001, 0011, 0111, 1111(还有0101之类的),最大为127的组合。
D:换算
0011 = $0001 or $0010;
1111 = $0001 or $0010 or $0100 or $10005..6
也就是:01XXXX, 11XXXX,
如上所述
7, 8类似基本的位运算而已其它的自己译吧。
TCommFlags = record
fBinary: Boolean;
fParity: Boolean;
fOutxCtsFlow: Boolean;
fOutxDsrFlow: Boolean;
fDtrControl: 0..2;
fDsrSensitivity: Boolean;
fTXContinueOnXoff: Boolean;
fOutX: Boolean;
fInX: Boolean;
fErrorChar: Boolean;
fNull: Boolean;
fRtsControl: 0..3;
fAbortOnError: Boolean;
end;const
ENABLE_BINARAY = 1;
ENABLE_PARITRY = 2;
ENABLE_OUTXCTSFLOW = 4;
ENABLE_OUTXDSRFLOW = 8; ENABLE_DTR_CONTROL = $10;
ENABLE_DTR_HANDSHAKE = $20; ENABLE_DSRSENSITIVITY = $40;
ENABLE_TXCONTINUEONXOFF = $80;
ENABLE_OUTX = $100;
ENABLE_INX = $200;
ENABLE_ERRORCHAR = $400;
ENABLE_NULL = $800; ENABLE_RTSCONTROL = $1000;
ENABLE_RTSCONTROL_HANDSHAKE = $2000;
ENABLE_RTSCONTROL_TOGGLE = $3000;
ENABLE_ABORTONERROR = $4000;
//其它位的是保留,为0,不管它function CommFlagsToDWORD(Value: TCommFlags): DWORD;
begin
Result := 0;
if Value.fBinary then Inc(Result, ENABLE_BINARAY);
if Value.fParity then Inc(Result, ENABLE_PARITRY);
if Value.fOutxCtsFlow then Inc(Result, ENABLE_OUTXCTSFLOW);
if Value.fOutxDsrFlow then Inc(Result, ENABLE_OUTXDSRFLOW);
case Value.fDtrControl of
DTR_CONTROL_ENABLE: Inc(Result, ENABLE_DTR_CONTROL);
DTR_CONTROL_HANDSHAKE: Inc(Result, ENABLE_DTR_HANDSHAKE);
end;
if Value.fDsrSensitivity then Inc(Result, ENABLE_DSRSENSITIVITY);
if Value.fTXContinueOnXoff then Inc(Result, ENABLE_TXCONTINUEONXOFF);
if Value.fOutX then Inc(Result, ENABLE_OUTX);
if Value.fInX then Inc(Result, ENABLE_INX);
if Value.fErrorChar then Inc(Result, ENABLE_ERRORCHAR);
if Value.fNull then Inc(Result, ENABLE_NULL);
case Value.fRtsControl of
RTS_CONTROL_ENABLE: Inc(Result, ENABLE_RTSCONTROL);
RTS_CONTROL_HANDSHAKE: Inc(Result, ENABLE_RTSCONTROL_HANDSHAKE);
RTS_CONTROL_TOGGLE: Inc(Result, ENABLE_RTSCONTROL_TOGGLE);
end;
if Value.fAbortOnError then Inc(Result, ENABLE_ABORTONERROR);
end;function DWORDToCommFlags(Value: DWORD): TCommFlags;
begin
FillChar(Result, SizeOf(TCommFlags), 0);
with Result do
begin
fBinary := (Value and ENABLE_BINARAY) = ENABLE_BINARAY;
fParity := (Value and ENABLE_PARITRY) = ENABLE_PARITRY;
fOutxCtsFlow := (Value and ENABLE_OUTXCTSFLOW) = ENABLE_OUTXCTSFLOW;
fOutxDsrFlow := (Value and ENABLE_OUTXDSRFLOW) = ENABLE_OUTXDSRFLOW;
if (Value and ENABLE_DTR_CONTROL) = ENABLE_DTR_CONTROL then
fDtrControl := DTR_CONTROL_ENABLE else
if (Value and ENABLE_DTR_HANDSHAKE) = ENABLE_DTR_HANDSHAKE then
fDtrControl := DTR_CONTROL_HANDSHAKE;
fDsrSensitivity := (Value and ENABLE_DSRSENSITIVITY) = ENABLE_DSRSENSITIVITY;
fTXContinueOnXoff := (Value and ENABLE_TXCONTINUEONXOFF) = ENABLE_TXCONTINUEONXOFF;
fOutX := (Value and ENABLE_OUTX) = ENABLE_OUTX;
fInX := (Value and ENABLE_INX) = ENABLE_INX;
fErrorChar := (Value and ENABLE_ERRORCHAR) = ENABLE_ERRORCHAR;
fNull := (Value and ENABLE_NULL) = ENABLE_NULL;
if (Value and ENABLE_RTSCONTROL) = ENABLE_RTSCONTROL then
fRtsControl := RTS_CONTROL_ENABLE else
if (Value and ENABLE_RTSCONTROL_HANDSHAKE) = ENABLE_RTSCONTROL_HANDSHAKE then
fRtsControl := RTS_CONTROL_HANDSHAKE else
if (Value and ENABLE_RTSCONTROL_TOGGLE) = ENABLE_RTSCONTROL_TOGGLE then
fRtsControl := RTS_CONTROL_TOGGLE;
fAbortOnError := (Value and ENABLE_ABORTONERROR) = ENABLE_ABORTONERROR;
end;
end;//这是调用例子
procedure someCall;
var
dcb: TDCB;
cfFlags: TCommFlags;
begin
...
cfFlags.fBinary := True;
cfFlags.fParity := True;
cfFlags.fOutxCtsFlow := False;
cfFlags.fOutxDsrFlow := False;
cfFlags.fDtrControl := DTR_CONTROL_DISABLE;
cfFlags.fDsrSensitivity := False;
cfFlags.fInX := False;
cfFlags.fOutX := False;
cfFlags.fRtsControl := RTS_CONTROL_DISABLE;
cfFlags.fAbortOnError := False;
with dcb do
begin
Flags := CommFlagsToDWORD(cfFlags);
ByteSize := 8;
BaudRate := CBR_1200;
Parity := ODDPARITY;
StopBits := ONESTOPBIT;
end;
...
...
end;TDCB在C++定义时也是像你上面所定义的那种样子,后来我转译时就写了个函数来转,如上
{
BYTE AclType : 4;
BYTE Reserved : 2;
BYTE DeleteIn : 1;
BYTE DeleteOut: 1;
};
我也可以用一个DWORD因为我只要保持1个字节,对不对,如果需要访问内部的东东只需要转换出来就够了吧