如何将Delphi OleVariant内容转储到文件中? [英] How to dump Delphi OleVariant content to the file?

查看:112
本文介绍了如何将Delphi OleVariant内容转储到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多层应用程序,这些应用程序使用OleVariant变量通过自定义通信框架跨线发送和接收数据。通常,此OleVariant变量的内容来自TClientDataSet.Data(当然,它是OleVariant类型的)。我怀疑通信框架会更改此OleVariant变量的内容,因此我想在发送和接收数据之前转储该变量的内容。我使用代码:

I have multi-tiered application that use OleVariant variables to send and receive data accross the line using custom communication framework. Usually the content of this OleVariant variable comes from TClientDataSet.Data (which, of course, is of OleVariant type). I have suspicion that the communication frmework changes the content of this OleVariant variable and therefore I would like to dump the contenct of the variable before sending and after receiving data. I use code:

TmpOrder: OleVariant;
Tmp: TStringList;

Tmp:=TStringList.Create;
try
  Tmp.LoadFromFile('D:\test.txt');
  Tmp.Add('---');
  Tmp.Add(TmpOrder);
  Tmp.SaveToFile('D:\test.txt');
finally
  Tmp.Free;
end;

但是文件的内容是没有意义的。例如,文件的以下内容可以表示TClientDataSet.Data的内容:

But the content of the file is meaningless. E.g., the following content of the file can represent the content of the TClientDataSet.Data:

---
??

当然,情况有所不同。

所以-如何捕获OleVariant变量的内容。

So - how to capture the content of the OleVariant variable.

将TClientDataSet保存为XML并不是我的选择,因为通信使用的是OleVariant而不是XML。

Saving TClientDataSet as XML is not option for me, because communication uses OleVariant not XML.

我正在使用Unciode Delphi(Delphi 2009及更高版本)。

I am using Unciode Delphi (Delphi 2009 and up).

推荐答案

此处是一些将TClientDataSet.Data转换为字符串以及对其进行测试的测试例程的代码。

Here is some code which converts TClientDataSet.Data to a string and a test routine which exercises it.

function VariantToString(V : OleVariant) : String;
  procedure VariantToStringInner(V : OleVariant; var AString : String);
  var
    IsArray : Boolean;
    DimCount : Integer;
    i : Integer;
  begin
    IsArray := VarIsArray(V);

    if not IsArray then begin
      //  Handle the case where V is not an array
      if (VarIsEmpty(V) or VarIsNull(V)) then begin
        AString := AString + ' ';
        exit;
      end
      else
        AString := AString + VarToStr(V);
    end
    else begin
      //  V is an array, so first we need to know how many dimensions it has
      DimCount := VarArrayDimCount(V);

      //  For TClientDataSet.Data the answer AFAIK should be 1
      //  so we'll process only dimension 1
      Assert(DimCount = 1);

      //  Recurse into this proc so that we handle the cases where V[i] is an array
      //  and where it isn't
      for i := VarArrayLowBound(V, 1) to VarArrayHighBound(V, 1) do begin
        VariantToStringInner(V[i], AString);
      end;
    end;
  end;

begin
  Result := '';
  VariantToStringInner(V, Result);
end;

procedure TForm1.TestVarToStr;
var
  V : OleVariant;
  S : String;
begin
  V := 'hello world';
  S := VariantToString(V) + #13#10;

  V := CDS1.Data;
  S := S + VariantToString(V);
  Memo1.Lines.Text := S;
end;

很明显,如果要将结果保存到文件中,只需调用 Memo1.Lines.SaveToFile()

Obviously, if you want to save the result to a file, simply call Memo1.Lines.SaveToFile()

我得到的结果是


'p>的hello world
150252241891000240005020003000202027368401000107838566848980692073080651171161111051109904689711610140600000678117109981011144010000047897109101107300010587736884722020200586971081171011073000105877368847220202002010677265787169957679714013006000100000004000200000004000126585847973786786657685694010300044011000578971091014944002000578971091015026567

hello world 150252241891000240005020003000202027368401000107838566848980692073080651171161111051109904689711610140600000678117109981011144010000047897109101107300010587736884722020200586971081171011073000105877368847220202002010677265787169957679714013006000100000004000200000004000126585847973786786657685694010300044011000578971091014944002000578971091015026567

同样CDS数据,保存为XML如下:

The same CDS data, saved as XML is as follows:

<?xml version="1.0" standalone="yes"?>  
<DATAPACKET Version="2.0">
<METADATA>
  <FIELDS>
    <FIELD attrname="ID" fieldtype="i4" SUBTYPE="Autoinc"/><FIELD   attrname="Date" fieldtype="date"/>
    <FIELD attrname="Number" fieldtype="i4"/><FIELD attrname="Name" fieldtype="string" WIDTH="20"/><FIELD attrname="Value" fieldtype="string" WIDTH="20"/>
  </FIELDS>
  <PARAMS CHANGE_LOG="1 0 4 2 0 4" AUTOINCVALUE="3"/>
</METADATA>
<ROWDATA>
  <ROW RowState="4" ID="1" Name="Name1"/><ROW RowState="4" ID="2" Name="Name2" Value="AC"/>
</ROWDATA>
</DATAPACKET>

这篇关于如何将Delphi OleVariant内容转储到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆