// 有关三层应用的函数集合<br> // 李大林 整理自网上<br> //<br> //<br><br>unit MIDAS_U;<br><br>interface<br> uses Windows, Messages, SysUtils, Classes,Variants;<br> procedure RecordToVariant(var ARec; Size: integer; var v: oleVariant);<br> procedure VariantToRecord(v: oleVariant; var ARec; Size: integer);<br> //////////////变体与记录 转换<br> function FileToVariant(FileName: String): OleVariant;<br> procedure VariantToFile(FileName: String; var AVariant: OleVariant);<br> ////变体与文件转换<br> procedure VariantToStream (const v : olevariant;Stream : TMemoryStream);<br> procedure StreamToVariant (Stream : TMemoryStream; var v : OleVariant);<br> //变体与流 转换<br><br><br>implementation<br>{<br> p_p:=VarArrayCreate([0,length(记录数组)-1,0,1],varVariant);<br> for i:=0 to length(记录数组)-1 do<br> begin<br> p_p[i,0]:=记录数组.dm;<br> p_p[i,1]:=记录数组.mc;<br> end;<br>}<br>function FileToVariant(FileName: String): OleVariant;<br>var<br> AStream: TFileStream;<br> MyBuffer: Pointer;<br>begin<br> AStream:=TFileStream.create(FileName,fmOpenRead);<br> try <br> aStream.Seek(0, soFromBeginning); <br> Result:=VarArraycreate([0, AStream.size-1], VarByte); <br> MyBuffer:=VarArrayLock(Result); <br> AStream.ReadBuffer(MyBuffer^, AStream.Size); <br> VarArrayUnlock(Result); <br> finally <br> AStream.Free; <br> end; <br>end; <br><br><br>procedure VariantToFile(FileName: String; var AVariant: OleVariant);<br>var<br> AStream: TFileStream; <br> MyBuffer: Pointer; <br> Size: Integer; <br>begin <br> AStream:=TFileStream.create(FileName,fmCreate); <br> try <br> aStream.Seek(0, soFromBeginning); <br> Size:=VarArrayHighBound(AVariant,1)+VarArrayLowBound(AVariant,1)+1; <br> MyBuffer:=VarArrayLock(AVariant); <br> AStream.WriteBuffer(MyBuffer^, Size); <br> VarArrayUnlock(AVariant); <br> finally <br> AStream.Free; <br> end; <br>end; <br>procedure VariantToStream (const v : olevariant;Stream : TMemoryStream);<br>var <br> p : pointer; <br>begin <br> Stream.Position := 0;<br> Stream.Size := VarArrayHighBound (v, 1) - VarArrayLowBound(v, 1) + 1; <br> p := VarArrayLock (v); <br> Stream.Write (p^, Stream.Size); <br> VarArrayUnlock (v); <br> Stream.Position := 0; <br>end; <br><br>procedure StreamToVariant (Stream : TMemoryStream; var v : OleVariant);<br>var <br> p : pointer; <br>begin <br> v := VarArrayCreate ([0, Stream.Size - 1], varByte); <br> p := VarArrayLock (v); <br> Stream.Position := 0; <br> Stream.Read (p^, Stream.Size); <br> VarArrayUnlock (v); <br>end;<br>procedure RecordToVariant(var ARec; Size: integer; var v: oleVariant); //待测试<br>var<br> p: PByteArray;<br> prec: Pointer;<br> i: integer;<br>begin<br> v := VarArrayCreate([0, Size -1], varByte);<br> p := VarArrayLock(v);<br> prec := @ARec;<br> for i:=0 to Size-1 do<br> p^ := (PByteArray(prec))^;<br> VarArrayUnLock(v);<br>end;<br><br>procedure VariantToRecord(v: oleVariant; var ARec; Size: integer); //待测试<br>var<br> i: integer;<br> p: PByteArray;<br> prec: Pointer;<br>begin<br> prec:=@ARec;<br> p := VarArrayLock(v);<br> for i:=0 to Size-1 do<br> (PByteArray(prec))^ := p^;<br> VarArrayUnLock(v);<br>end;<br> <br>end.<br>-----------以上为需要用到的函数