文件与Olevariant

类别:Delphi 点击:0 评论:0 推荐:

//---------------------------读数据到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;

本文地址:http://com.8s8s.com/it/it4820.htm