这段代码不是和我写的一样吗,不同的是缓冲中不是一个结构,而是一个结构数组。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Init: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
DateTimePicker1: TDateTimePicker;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Edit11: TEdit;
Edit12: TEdit;
Edit13: TEdit;
Edit14: TEdit;
Button2: TButton;
Edit15: TEdit;
procedure InitClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Type
PHistoryEvent=^THistoryEvent;
THistoryEvent = packed record
EventID: Smallint;
CtrlAddress: Smallint;
PointAddress: Smallint;
CardZone: Integer;
CardNo: Integer;
LinkageNo: Smallint;
UserZone: Smallint;
EventTime: TDateTime;
end;
THistoryEvents=Array of THistoryEvent;
Var
Info:THistoryEvent;
vData:Variant;
function hInfoToVariant(Info
HistoryEvent):Variant;
Var
Data
ointer;
begin
Result:=VarArrayCreate([0,sizeof(THistoryEvent)-1],varByte);
Data:=VarArrayLock(Result);
Try
CopyMemory(Data,PByte(Info),sizeof(THistoryEvent));
Finally
VarArrayUnLock(Result)
end;
end;
function hInfosToVariant(Info:THistoryEvents):Variant;
Var
i:Integer;
Data
ointer;
begin
Result:=VarArrayCreate([0,Length(Info)-1,0,sizeof(THistoryEvent)-1],varByte);
Data:=VarArrayLock(Result);
Try
For i:=Low(Info) to High(Info) do
begin
CopyMemory(Data,PByte(@Info
),sizeof(THistoryEvent));
Inc(PChar(Data),sizeof(THistoryEvent));
end;
Finally
VarArrayUnLock(Result)
end;
end;
function VariantTohInfo(vData:Variant):THistoryEvent;
Var
Len:Integer;
Dataointer;
begin
Len:=VarArrayHighBound(vData,1)+1;
if Len<>sizeof(THistoryEvent) then
Raise Exception.Create('数据包无效!');
Data:=VarArrayLock(vData);
Try
CopyMemory(PByte(@Result),Data,Len);
Finally
VarArrayUnLock(vData)
end;
end;
function VariantTohInfos(vData:Variant):THistoryEvents;
Var
i:Integer;
Len:Integer;
Dataointer;
begin
Len:=VarArrayHighBound(vData,1)+1;
SetLength(Result,Len);
Data:=VarArrayLock(vData);
Try
For i:=Low(Result) to High(Result) do
begin
CopyMemory(PByte(@Result),Data,sizeof(THistoryEvent));
Inc(PChar(Data),sizeof(THistoryEvent));
end;
Finally
VarArrayUnLock(vData)
end;
end;
procedure TForm1.InitClick(Sender: TObject);
Var
i:Integer;
Infos:THistoryEvents;
begin
SetLength(Infos,3);
For i:=0 to 2 do
begin
With Infos do
begin
EventID:=StrToInt(Edit1.Text)+i;
CtrlAddress:=StrToInt(Edit2.Text)+i;
PointAddress:=StrToInt(Edit3.Text)+i;
CardZone:=StrToInt(Edit4.Text)+i;
CardNo:=StrToInt(Edit5.Text)+i;
LinkageNo:=StrToInt(Edit6.Text)+i;
UserZone:=StrToInt(Edit7.Text)+i;
EventTime:=DateTimePicker1.DateTime+i;
end;
end;
vData:=hInfosToVariant(Infos);
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
vInfo:THistoryEvents;
begin
vInfo:=VariantTohInfos(vData);
Edit8.Text:=IntToStr(vInfo[0].EventID)+','+IntToStr(vInfo[1].EventID)+','+IntToStr(vInfo[2].EventID);
Edit9.Text:=IntToStr(vInfo[0].CtrlAddress)+','+IntToStr(vInfo[1].CtrlAddress)+','+IntToStr(vInfo[2].CtrlAddress);
Edit10.Text:=IntToStr(vInfo[0].PointAddress)+','+IntToStr(vInfo[1].PointAddress)+','+IntToStr(vInfo[2].PointAddress);
Edit11.Text:=IntToStr(vInfo[0].CardZone)+','+IntToStr(vInfo[1].CardZone)+','+IntToStr(vInfo[2].CardZone);
Edit12.Text:=IntToStr(vInfo[0].CardNo)+','+IntToStr(vInfo[1].CardNo)+','+IntToStr(vInfo[2].CardNo);
Edit13.Text:=IntToStr(vInfo[0].LinkageNo)+','+IntToStr(vInfo[1].LinkageNo)+','+IntToStr(vInfo[2].LinkageNo);
Edit14.Text:=IntToStr(vInfo[0].UserZone)+','+IntToStr(vInfo[1].UserZone)+','+IntToStr(vInfo[2].UserZone);
Edit14.Text:=DateTimeToStr(vInfo[0].EventTime)+','+DateTimeToStr(vInfo[1].EventTime)+','+DateTimeToStr(vInfo[2].EventTime);
end;
end.