G
GanQuan
Unregistered / Unconfirmed
GUEST, unregistred user!
客户端代码:
TMyRecord=record
Name: String;
Age: Integer;
end;
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
p := VarArrayLock(Data);
try
Stream.Write(p^, VarArrayHighBound(Data,1) + 1);
finally
VarArrayUnlock(Data);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
Info: Olevariant;
Obj: IServerObject;
R: TMyRecord;
begin
Obj := CoServerObject.CreateRemote('LEO');
Obj.GetInfo(Info);
MyStream := TMemoryStream.Create;
VariantToStream(Info,MyStream);
MyStream.Seek(0,soFrombegin
ning);
MyStream.ReadBuffer(R,sizeof(TMyRecord));
//R不可存取,Why???????
MyStream.Free;
Showmessage(R.Name + ':' + IntToStr(R.Age));
end;
服务器端代码:
TMyRecord=record
Name: String;
Age: Integer;
end;
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
p := VarArrayLock(Data);
try
Stream.Write(p^, VarArrayHighBound(Data,1) + 1);
finally
VarArrayUnlock(Data);
end;
end;
procedure TServerObject.GetInfo(var vUserInfo: OleVariant);
var
R: TMyRecord;
MyStream: TMemoryStream;
begin
R.Name := 'AAA';
R.Age := 25;
MyStream := TMemoryStream.Create;
MyStream.Write(R,sizeof(TMyRecord));
MyStream.Seek(0,soFrombegin
ning);
vUserInfo := StreamToVariant(MyStream);
MyStream.Free;
end;
注:
1。以前使用此方法传递数据没有出现此类错误!
2。在客户端封装显示不会出现此类错误!
如下面代码在客户端没有错误。
procedure GetInfo(var vInfo: OleVariant);
var
MyStream: TMemoryStream;
R: TMyRecord;
begin
R.Name := 'AAA';
R.Age := 25;
MyStream := TMemoryStream.Create;
MyStream.Write(R,sizeof(TMyRecord));
MyStream.Seek(0,soFrombegin
ning);
vInfo := StreamToVariant(MyStream);
MyStream.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MyStream: TMemoryStream;
UInfo: TMyRecord;
Info: Olevariant;
begin
GetInfo(Info);
MyStream := TMemoryStream.Create;
VariantToStream(Info,MyStream);
ShowMessage(Inttostr(MyStream.Size));
MyStream.Seek(0,soFrombegin
ning);
MyStream.Read(UInfo,sizeof(TMyRecord));
MyStream.Free;
Showmessage(UInfo.Name + ':' + IntToStr(UInfo.Age));
end;
TMyRecord=record
Name: String;
Age: Integer;
end;
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
p := VarArrayLock(Data);
try
Stream.Write(p^, VarArrayHighBound(Data,1) + 1);
finally
VarArrayUnlock(Data);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyStream: TMemoryStream;
Info: Olevariant;
Obj: IServerObject;
R: TMyRecord;
begin
Obj := CoServerObject.CreateRemote('LEO');
Obj.GetInfo(Info);
MyStream := TMemoryStream.Create;
VariantToStream(Info,MyStream);
MyStream.Seek(0,soFrombegin
ning);
MyStream.ReadBuffer(R,sizeof(TMyRecord));
//R不可存取,Why???????
MyStream.Free;
Showmessage(R.Name + ':' + IntToStr(R.Age));
end;
服务器端代码:
TMyRecord=record
Name: String;
Age: Integer;
end;
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
p := VarArrayLock(Data);
try
Stream.Write(p^, VarArrayHighBound(Data,1) + 1);
finally
VarArrayUnlock(Data);
end;
end;
procedure TServerObject.GetInfo(var vUserInfo: OleVariant);
var
R: TMyRecord;
MyStream: TMemoryStream;
begin
R.Name := 'AAA';
R.Age := 25;
MyStream := TMemoryStream.Create;
MyStream.Write(R,sizeof(TMyRecord));
MyStream.Seek(0,soFrombegin
ning);
vUserInfo := StreamToVariant(MyStream);
MyStream.Free;
end;
注:
1。以前使用此方法传递数据没有出现此类错误!
2。在客户端封装显示不会出现此类错误!
如下面代码在客户端没有错误。
procedure GetInfo(var vInfo: OleVariant);
var
MyStream: TMemoryStream;
R: TMyRecord;
begin
R.Name := 'AAA';
R.Age := 25;
MyStream := TMemoryStream.Create;
MyStream.Write(R,sizeof(TMyRecord));
MyStream.Seek(0,soFrombegin
ning);
vInfo := StreamToVariant(MyStream);
MyStream.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MyStream: TMemoryStream;
UInfo: TMyRecord;
Info: Olevariant;
begin
GetInfo(Info);
MyStream := TMemoryStream.Create;
VariantToStream(Info,MyStream);
ShowMessage(Inttostr(MyStream.Size));
MyStream.Seek(0,soFrombegin
ning);
MyStream.Read(UInfo,sizeof(TMyRecord));
MyStream.Free;
Showmessage(UInfo.Name + ':' + IntToStr(UInfo.Age));
end;