• TMemoryStream、String与OleVariant互转


    ////////////////////////////////////////////////////////////////////////////////
    //功能: STRING 的内容流化到 OLEVARIANT 中
    //参数:
    ////////////////////////////////////////////////////////////////////////////////
    function TextToOleData(const AText: string): OleVariant;
    var
    nSize: Integer;
    pData: Pointer;
    begin
    nSize := Length(AText);
    if nSize = 0 then
    Result := Null
    else begin
    Result := VarArrayCreate([0, nSize - 1], varByte);
    pData := VarArrayLock(Result);
    try
    Move(Pchar(AText)^, pData^, nSize);
    finally
    VarArrayUnlock(Result);
    end;
    end;
    end;

    ////////////////////////////////////////////////////////////////////////////////
    //功能: 由 OLEVARIANT 中加载 STRING 的内容
    //参数:
    ////////////////////////////////////////////////////////////////////////////////
    function OleDataToText(const AData: OleVariant): string;
    var
    nSize: Integer;
    pData: Pointer;
    begin
    if AData = Null then
    Result := ''
    else begin
    nSize := VarArrayHighBound(AData, 1) - VarArrayLowBound(AData, 1) + 1;
    SetLength(Result, nSize);
    pData := VarArrayLock(AData);
    try
    Move(pData^, Pchar(Result)^, nSize);
    finally
    VarArrayUnlock(AData);
    end;
    end;
    end;

    //转换OLEVariant和TMemoryStream:
    function MemoryStreamToOleVariant(Strm: TMemoryStream): OleVariant;
    var
    Data: PByteArray;
    begin
    Result := VarArrayCreate([0, Strm.Size - 1], varByte);
    Data := VarArrayLock(Result);
    try
    Strm.Position := 0;
    Strm.ReadBuffer(Data^, Strm.Size);
    finally
    VarArrayUnlock(Result);
    end;
    end;

    function OleVariantToMemoryStream(OV: OleVariant): TMemoryStream;
    var
    Data: PByteArray;
    Size: integer;
    begin
    Result := 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; 
    except
    Result.Free; 
    Result := nil; 
    end; 
    end;

    function RecordsetToVariant(const Recordset: _Recordset; var Stream: OleVariant): boolean;
    var
    RS: OleVariant;
    vData:TMemoryStream;
    begin
    Result := false;
    if Recordset = nil then Exit;
    vData:=TMemoryStream.Create;
    try
    RS := CreateOleObject('ADODB.Recordset');
    RS := Recordset;
    RS.Save(TStreamAdapter.Create(vData) as IUnknown, adPersistADTG);
    vData.Position := 0;
    Result := true;
    Stream:=MemoryStreamToOleVariant(vData);
    except
    on E:Exception do
    begin
    RS:=E.Message;
    Stream:=RS;
    Result:=False;
    end;
    end;
    end;
    function RecordsetFromVariant(vdata: OleVariant): _Recordset;
    var
    RS: OleVariant;
    Stream:TMemoryStream;
    begin
    Result := nil;
    Stream:=TMemoryStream.Create;
    Stream:=OleVariantToMemoryStream(vdata);
    if Stream.Size < 1 then Exit;
    try
    Stream.Position := 0;
    RS := CreateOleObject( 'ADODB.Recordset');
    RS.Open(TStreamAdapter.Create(Stream) as IUnknown);
    Result := IUnknown(RS) as _Recordset;
    finally;
    end;
    end;

  • 相关阅读:
    app爬虫(python)开发——通过docker部署多任务端app应用数据抓取系统(终结篇)
    app爬虫(python)开发——app软件数据抓取进阶之实战笔记
    app爬虫(python)开发——简单app数据抓取入门实战案例笔记
    app爬虫(python)开发——移动端自动化控制工具(appium,inspector等)笔记
    JavaCV FFmpeg采集摄像头YUV数据
    java安全编码指南之:lock和同步的正确使用
    《.net之美》之读书笔记(二) C#中的泛型
    跟我一起学.NetCore之WebApi接口裸奔有风险(Jwt)
    一个爬虫的故事:这是人干的事儿?
    从零开始针对 .NET 应用的 DevOps 运营实践
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2934790.html
Copyright © 2020-2023  润新知