如何在midas通过接口传递Bindary数据(100分)

  • 主题发起人 主题发起人 lbwave
  • 开始时间 开始时间
L

lbwave

Unregistered / Unconfirmed
GUEST, unregistred user!
client端
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
ss:TstringStream;
mm:Tmemorystream;
begin
if not cnn.Connected then
cnn.Open ;
ss:=tstringstream.Create(s);
mm:=Tmemorystream.create;
mm.LoadFromFile('d:/test/l.bmp');
ss.CopyFrom (mm,mm.size);
cnn.AppServer.setperson('1234',ss.datastring,mm.size);
mm.free;
ss.free;
end;

appserver:
procedure TTPhotoSvr.SetPerson(const IDD: WideString;
Bitmap: OleVariant;
var retFlage: OleVariant);
begin
//川过来后 bitmap 变为 varOlestr 类型,cpu window 察看,
//将bitmap->widestring

end;
各位有更好的透过DCOM 接口传图片的方法?比由重筹
 
你用个CLIENTDATASET和一个有BOLB域的表,
很好搞定,相信你会搞的。
 
maming
您说的方法我在用;
我提这个问题只是想找一个好方法方法通过自动化对象传递大数据流,在很多地方很有价值.
这个问题的难点是delphi 在使用接口时,自动将数据类型由String转为OleVariant
(BStr)
谢谢回答
 
通常有两种方法可以通过DCOM传递BIBARY数据,一是用VirantArray Of Byte,另外是用IDATAOBJECT
 
这里给你两个函数,用来转换OLEVariant和TMemoryStream:
function MemoryStreamToOleVariant(Strm: TMemoryStream): OleVariant;
var
Data: PByteArray;
begin
Result := VarArrayCreate([0, Strm.Size - 1], varByte);
Data := VarArrayLock(Result);
try
Strm.Position := 0;
Strm.ReadBuffer(Data^, Strm.Size);
finally
VarArrayUnlock(Result);
end;
end;

function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
var
Data: PByteArray;
Size: integer;
begin
Result := TMemoryStream.Create;
try
Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;
Data := VarArrayLock(OV);
try
Result.Position := 0;
Result.WriteBuffer(Data^, Size);
finally
VarArrayUnlock(OV);
end;
except
Result.Free;
Result := nil;
end;
end;
 
假设服务器端有两个方法,SetBinary and GetBinary, 并且在服务程序的主窗口有两个TImage和一个Memo. 下面给出两个方法:
procedure TBinaryDataObject.GetBinary(out BinaryData: OleVariant);
var MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
Form1.Image1.Picture.Bitmap.SaveToStream(MemoryStream);
BinaryData := MemoryStreamToOleVariant(MemoryStream);
Form1.Memo1.Lines.Add(format('Bitmap is send out, size=%d',[MemoryStream.size]));
finally
MemoryStream.Free;
end;
end;

procedure TBinaryDataObject.SetBinary(BinaryData: OleVariant);
var MemoryStream: TMemoryStream;
begin
try
MemoryStream := OleVariantToMemoryStream(BinaryData);
MemoryStream.Position := 0;
Form1.Image2.Picture.Bitmap.LoadFromStream(MemoryStream);
Form1.Memo1.Lines.Add(format('Bitmap is recieved, size=%d',[MemoryStream.size]));
finally
MemoryStream.free;
end;
end;

与服务器相对应在客户端,同样有两个图像,一个用来发送给服务器,一个用来接受显示的图像,有下列两段程序:
TForm1 = ....
....
public
{ Public declarations }
Binary: IBinaryDataObject;
//IBinaryDataObject是服务器的COMObject
end;

implementation
procedure TForm1.ButtonPutPictureClick(Sender: TObject);
var
OV: OleVariant;
Stream: TMemoryStream;
begin
if Binary = nil then
Binary := CoBinaryDataObject.CreateRemote(你的服务器机器名);
Stream := TMemoryStream.Create;
try
Image1.Picture.Bitmap.SaveToStream(Stream);
OV := MemoryStreamToOleVariant(Stream);
Binary.SetBinary(OV);
finally
Stream.free;
end;
end;

procedure TForm1.ButtonGetPictureClick(Sender: TObject);
var
OV: OleVariant;
Stream: TMemoryStream;
begin
if Binary = nil then
Binary := CoBinaryDataObject.CreateRemote(你的服务器机器名);
Stream := TMemoryStream.Create;
try
Binary.GetBinary(OV);
Stream := OleVariantToMemoryStream(OV);
Stream.Position := 0;
Image2.Picture.Bitmap.LoadFromStream(Stream);
finally
Stream.free;
end;
end;

 
cjfandhf的方法比较普通
maming方法较好
老炖:高手
谢谢大家回答!!
 
后退
顶部