如何在Delphi中使用变体数组 [英] How to use variant arrays in Delphi

查看:243
本文介绍了如何在Delphi中使用变体数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Delphi7程序:一个COM自动化服务器(EXE),另一个使用自动化服务器的程序。



我需要传递一个字节数组从一个程序到另一个程序。



经过一些搜索,我发现使用变量数组是一种方式(如果你知道更好的方法,请更正我)。



我的问题是:
如何在一个程序中创建一个变体数组,然后如何在另一个程序中读取它的值?



我知道VarArrayCreate和VarArrayLowBound / VarArrayHighBound,但我不确定如何正确地执行此操作。



谢谢! / p>

解决方案

您创建的这样:



首先声明

  var 
VarArray:Variant;
值:Variant;

然后创建:

  VarArray:= VarArrayCreate([0,Length  -  1],varVariant); 

或者你也可以有

  VarArray:= VarArrayCreate([0,Length  -  1],varInteger); 

取决于数据的类型。然后你像这样迭代:

  i:= VarArrayLowBound(VarArray,1); 
HighBound:= VarArrayHighBound(VarArray,1);

而i< = HighBound do
begin
值:= VarArray [i];
...做某事...
Inc(i);
结束

最后,当您不再需要它时,您将清除数组。编辑:(这是可选的,请参阅在德尔福2009年做我需要自由的变体数组?

  VarClear(VarArray); 

这就是它的一切。另一个例子,请查看官方的 Embracadero帮助< a>



编辑:



数组应该只创建一次。然后只需使用它,如上面的例子所示。


I have two Delphi7 programs: a COM automation server (EXE) and the other program which is using the automation server.

I need to pass an array of bytes from one program to the other.

After some searching I've found that using variant arrays is the way to go (correct me please if you know any better methods).

My question is: How do I create a variant array in one program, and then how do I read its values in the other?

I know about VarArrayCreate and VarArrayLowBound/VarArrayHighBound, but I'm unsure on how to do this properly.

Thanks!

解决方案

You create it like that:

Declarations first

var
  VarArray: Variant;
  Value: Variant;

Then the creation:

VarArray := VarArrayCreate([0, Length - 1], varVariant);

or you could also have

VarArray := VarArrayCreate([0, Length - 1], varInteger);

Depends on the type of the data. Then you iterate like this:

i := VarArrayLowBound(VarArray, 1);
HighBound := VarArrayHighBound(VarArray, 1);

while i <= HighBound do
begin
  Value := VarArray[i];
  ... do something ...
  Inc(i);
end;

Finally you clear the array when you don't need it anymore. EDIT: (This is optional, see In Delphi 2009 do I need to free variant arrays? )

VarClear(VarArray);

That is all there is to it. For another example look at the official Embracadero Help

EDIT:

The array should be created only once. Then just use it like shown in the above example.

这篇关于如何在Delphi中使用变体数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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