//////////////////////////////////////////////////////////////////////////////////功能: STRING 的内容流化到 OLEVARIANT 中//参数:////////////////////////////////////////////////////////////////////////////////function TextToOleData(const AText: string): OleVariant;varnSize: Integer;pData: Pointer;beginnSize := Length(AText);if nSize = 0 thenResult := Nullelse beginResult := VarArrayCreate([0, nSize - 1], varByte);pData := VarArrayLock(Result);tryMove(Pchar(AText)^, pData^, nSize);finallyVarArrayUnlock(Result);end;end;end;
//////////////////////////////////////////////////////////////////////////////////功能: 由 OLEVARIANT 中加载 STRING 的内容//参数:////////////////////////////////////////////////////////////////////////////////function OleDataToText(const AData: OleVariant): string;varnSize: Integer;pData: Pointer;beginif AData = Null thenResult := ''else beginnSize := VarArrayHighBound(AData, 1) - VarArrayLowBound(AData, 1) + 1;SetLength(Result, nSize);pData := VarArrayLock(AData);tryMove(pData^, Pchar(Result)^, nSize);finallyVarArrayUnlock(AData);end;end;end;
//转换OLEVariant和TMemoryStream:function MemoryStreamToOleVariant(Strm: TMemoryStream): OleVariant;varData: PByteArray;beginResult := VarArrayCreate([0, Strm.Size - 1], varByte);Data := VarArrayLock(Result);tryStrm.Position := 0;Strm.ReadBuffer(Data^, Strm.Size);finallyVarArrayUnlock(Result);end;end;
function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;varData: PByteArray;Size: integer;beginResult := TMemoryStream.Create; try Size := VarArrayHighBound (OV, 1) - VarArrayLowBound(OV, 1) + 1;Data := VarArrayLock(OV); try Result.Position := 0; Result.WriteBuffer(Data^, Size);finally VarArrayUnlock(OV); end; exceptResult.Free; Result := nil; end; end;
function RecordsetToVariant(const Recordset: _Recordset; var Stream: OleVariant): boolean;varRS: OleVariant;vData:TMemoryStream;beginResult := false;if Recordset = nil then Exit;vData:=TMemoryStream.Create;tryRS := CreateOleObject('ADODB.Recordset');RS := Recordset;RS.Save(TStreamAdapter.Create(vData) as IUnknown, adPersistADTG);vData.Position := 0;Result := true;Stream:=MemoryStreamToOleVariant(vData);excepton E:Exception dobeginRS:=E.Message;Stream:=RS;Result:=False;end;end;end;function RecordsetFromVariant(vdata: OleVariant): _Recordset;varRS: OleVariant;Stream:TMemoryStream;beginResult := nil;Stream:=TMemoryStream.Create;Stream:=OleVariantToMemoryStream(vdata);if Stream.Size < 1 then Exit;tryStream.Position := 0;RS := CreateOleObject( 'ADODB.Recordset');RS.Open(TStreamAdapter.Create(Stream) as IUnknown);Result := IUnknown(RS) as _Recordset;finally;end;end;
新闻热点
疑难解答