J
jlutt-sadan
Unregistered / Unconfirmed
GUEST, unregistred user!
帮帮忙,我想把对象存成流转成OleVariant传递回客户端,
中间层Com+,接口的方法:
procedure TPowerMaster.Login(const sName, sPassword: WideString;
var bSuccess, sError, sUserPara: OleVariant);
begin
.....................
....................
AUser := TASCUser.Create;
AUser.AssignRecord(adoqPower);
sUserPara := AUser.SaveToVariant;
AUser.Free;
bSuccess := true;
sError := '';
end;
自定义类:
TASCUser=class
private
......
protected
procedure LoadFromStream(AStream: TMemoryStream);
procedure SaveToStream(AStream: TMemoryStream);
public
function SaveToVariant: OleVariant;
procedure LoadFromVariant(const Data: OleVariant);
end;
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
Stream.Position := 0;
Stream.Size := varArrayHighBound(Data,1)-varArrayLowBound(Data,1)+1;
p := VarArrayLock(Data);
try
Stream.Write(p^,Stream.Size);
finally
VarArrayUnlock(Data);
end;
Stream.Position := 0;
end;
function StreamToVariant(Stream: TStream): OleVariant;
var
p: Pointer;
begin
Result := VarArrayCreate([0, Stream.Size - 1], varByte);
p := VarArrayLock(Result);
try
Stream.Position := 0;
Stream.Read(p^, Stream.Size);
finally
VarArrayUnlock(Result);
end;
end;
function TASCUser.SaveToVariant: OleVariant;
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
SaveToStream(AStream);
result := StreamToVariant(AStream);
finally
AStream.Free;
end;
end;
procedure TASCUser.LoadFromVariant(const Data: OleVariant);
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
VariantToStream(Data,AStream);
LoadFromStream(AStream);
finally
AStream.Free;
end;
end;
客户端:
procedure TForm1.Button1Click(Sender: TObject);
var
LoginObj: IPowerMaster;
bSuccess: OleVariant;
sError: OleVariant;
OnLineUser: OleVariant;
AUser: TASCUser;
begin
LoginObj := CoPowerMaster.CreateRemote('localhost');
LoginObj.Login(Edit1.Text,Edit2.Text,bSuccess,sError,OnLineUser);
Label1.Caption := sError;
if bSuccess then
begin
AUser := TASCUser.Create;
AUser.LoadFromVariant(OnLineUser, true);//在这里AUser什么也没读出来
Label2.Caption := AUser.Name;
AUser.Free;
end;
end;
各位大侠帮帮忙。
中间层Com+,接口的方法:
procedure TPowerMaster.Login(const sName, sPassword: WideString;
var bSuccess, sError, sUserPara: OleVariant);
begin
.....................
....................
AUser := TASCUser.Create;
AUser.AssignRecord(adoqPower);
sUserPara := AUser.SaveToVariant;
AUser.Free;
bSuccess := true;
sError := '';
end;
自定义类:
TASCUser=class
private
......
protected
procedure LoadFromStream(AStream: TMemoryStream);
procedure SaveToStream(AStream: TMemoryStream);
public
function SaveToVariant: OleVariant;
procedure LoadFromVariant(const Data: OleVariant);
end;
procedure VariantToStream(const Data: OleVariant;
Stream: TStream);
var
p: Pointer;
begin
Stream.Position := 0;
Stream.Size := varArrayHighBound(Data,1)-varArrayLowBound(Data,1)+1;
p := VarArrayLock(Data);
try
Stream.Write(p^,Stream.Size);
finally
VarArrayUnlock(Data);
end;
Stream.Position := 0;
end;
function StreamToVariant(Stream: TStream): OleVariant;
var
p: Pointer;
begin
Result := VarArrayCreate([0, Stream.Size - 1], varByte);
p := VarArrayLock(Result);
try
Stream.Position := 0;
Stream.Read(p^, Stream.Size);
finally
VarArrayUnlock(Result);
end;
end;
function TASCUser.SaveToVariant: OleVariant;
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
SaveToStream(AStream);
result := StreamToVariant(AStream);
finally
AStream.Free;
end;
end;
procedure TASCUser.LoadFromVariant(const Data: OleVariant);
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
VariantToStream(Data,AStream);
LoadFromStream(AStream);
finally
AStream.Free;
end;
end;
客户端:
procedure TForm1.Button1Click(Sender: TObject);
var
LoginObj: IPowerMaster;
bSuccess: OleVariant;
sError: OleVariant;
OnLineUser: OleVariant;
AUser: TASCUser;
begin
LoginObj := CoPowerMaster.CreateRemote('localhost');
LoginObj.Login(Edit1.Text,Edit2.Text,bSuccess,sError,OnLineUser);
Label1.Caption := sError;
if bSuccess then
begin
AUser := TASCUser.Create;
AUser.LoadFromVariant(OnLineUser, true);//在这里AUser什么也没读出来
Label2.Caption := AUser.Name;
AUser.Free;
end;
end;
各位大侠帮帮忙。