WEB开发网
开发学院软件开发Delphi 文件与Olevariant 阅读

文件与Olevariant

 2006-02-04 13:51:14 来源:WEB开发网   
核心提示://---读数据到olevariant//pfileName:带路径的文件名function Getobj(pfileName:string): OleVariant;var fs:TFileStream;begin fs := TFileStream.Create(pfileName,fmOpenRead); t

//---------------------------读数据到olevariant
//pfileName:带路径的文件名
function Getobj(pfileName:string): OleVariant;
var fs:TFileStream;
begin
  fs := TFileStream.Create(pfileName,fmOpenRead);
  try
   result := VarArrayCreate([0,fs.size-1],varbyte);
   fs.position := 0;
   fs.readbuffer(varArrayLock(result)^, fs.size);
  finally
   fs.free;
   varArrayunlock(result);
  end;
end;

//---------------------------从 olevariant 写到新文件
function writeFileFromOle(pfileName:string;pOleVariant:OleVariant):integer;
var fs:TFileStream;
begin
  result:=0;
  fs := TFileStream.Create(pfileName,fmCreate);
  try
   try
    fs.WriteBuffer(varArrayLock(pOleVariant)^, VarArrayHighBound(pOleVariant,1)+1);
   except
    result:=-1;
   end;
  finally
   fs.free;
   varArrayunlock(pOleVariant);
  end;
end;


------------------------------ 函数说明:
//取得数组A的第Dim维的长度
function VarArrayHighBound(const A: Variant; Dim: Integer): Integer;
//创建数组
function VarArrayCreate(const Bounds: array of Integer; VarType: Integer): Variant;

Tags:文件 Olevariant

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接