巨简单的问题,却没有人知道,帮帮我啊 我有个MIDAS结构的,我想通过调用服务端的方法把某个文件传上去,关键是怎么把个文件流转换为VARIANT? (100分)

  • 主题发起人 主题发起人 葬月
  • 开始时间 开始时间

葬月

Unregistered / Unconfirmed
GUEST, unregistred user!
比如说服务端有个
STDMETHODIMP TTextServerImpl::Test(VARIANT s)
{
TVariant f;
f=s;
void * ptr;
int DataSize= VarArrayHighBound(f,1) - VarArrayLowBound(f,1);
ptr = VarArrayLock(f);
TMemoryStream * aStr;
aStr = new TMemoryStream;
aStr->Write(ptr,DataSize);
VarArrayUnlock(f);
aStr->Seek(0,0);
aStr->SaveToFile("e://ss.exe");
delete aStr;
}
在客户端
TMemoryStream *m=new TMemoryStream();
m->LoadFromFile("d://ime.exe");
int iSize = m->Size;
int Bounds[] = {0,iSize-1};
Variant vtData = VarArrayCreate(Bounds,1,varByte);
void * pDatas = VarArrayLock(vtData);
m->Position =0;
m->Read(pDatas,iSize);
VarArrayUnlock(vtData);
MainForm->m_DCOMConnection->AppServer.OleFunction("Test",vtData);
但是却说服务端发生意外事故
我哪里错了?应该怎么做?
 
你不妨看看李维编的<delphi 5.x分布式多层应用 系统篇>那里有解决办法。
 
注意,,是VARIANT 不是Variant
李维可没有讲怎么把个文件流转换为VARIANT
 
差成字符串传
 
能否举个例子
 
我记得不能用char*来传递二进制文件,,遇到jpg就不行,因为jpg第五个字就是 00
 
unit Unit1;

interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs,jpeg, ExtCtrls, StdCtrls,ActiveX, AxCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Button2: TButton;
Image2: TImage;
procedure Button2Click(Sender: TObject);
private
procedure StreamToVariant(Stream: TMemoryStream;
var v: OleVariant);
procedure VariantToStream(const v: olevariant;
Stream: TMemoryStream);
procedure JPEGToVariant(aJPEG : TJPEGImage;
var aVariant:OleVariant);
procedure VariantToJPEG(aValue : OleVariant;
var aJPEG:TJPEGImage);
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;

implementation
{$R *.DFM}
procedure TForm1.VariantToStream (const v : olevariant;
Stream : TMemoryStream);
var
p : pointer;
begin

Stream.Position := 0;
Stream.Size := VarArrayHighBound (v, 1) - VarArrayLowBound (v, 1) + 1;
p := VarArrayLock (v);
Stream.Write (p^, Stream.Size);
VarArrayUnlock (v);
Stream.Position := 0;
end;

procedure TForm1.StreamToVariant (Stream : TMemoryStream;
var v : OleVariant);
var
p : pointer;
begin

v := VarArrayCreate ([0, Stream.Size - 1], varByte);
p := VarArrayLock (v);
Stream.Position := 0;
Stream.Read (p^, Stream.Size);
VarArrayUnlock (v);
end;

procedure TForm1.JPEGToVariant(aJPEG : TJPEGImage;
var aVariant:OleVariant);
var
Stream : TMemoryStream;
begin

try
Stream := TMemoryStream.Create;

aJPEG.Assign(Image1.Picture.Bitmap);
aJPEG.SaveToStream(Stream);

StreamToVariant(Stream, aVariant);
finally
Stream.Free;
end;

end;


procedure TForm1.VariantToJPEG(aValue : OleVariant;var aJPEG:TJPEGImage);
var
Stream : TMemoryStream;
begin

try
Stream := TMemoryStream.Create;

VariantToStream (aValue,Stream);
aJPEG.LoadfromStream(Stream);

finally
VariantClear(aValue);
Stream.free;
end;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
aJPEG :TJPEGImage;
aVariant : OleVariant;
begin

try
aJPEG:=TJPEGImage.Create;

//JPEG To Variant
aJPEG.Assign(Image1.Picture.Bitmap);
JPEGToVariant(aJPEG,aVariant);

//Variant To JPEG
VariantToJPEG(aVariant,aJPEG);
Image2.Picture.assign(aJPEG);
finally
aJPEG.free;
end;

end;

end.

 
blue_morning大哥
你能不能把它翻成C++Builder
我就是因为翻不来
 
抱歉、 BCB我不熟悉,但上面的贴的这个例子是可以用的。
 
如果只懂Delphi是会失业的
 
同意楼上!
 
错在这句吧: aStr->Write(ptr,DataSize);
不能直接用ptr, 应该用ptr指向的地址(delphi中表示为ptr^, c中怎么表示忘了)
 
多人接受答案了。
 
后退
顶部