综合

img LuckyJan

文件与Olevariant

发表于2004/10/9 10:22:00  997人阅读

分类: Delphi基础

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

阅读全文
0 0

相关文章推荐

img
取 消
img