如何使用Delphi的变长数组 [英] How to use variant arrays in Delphi

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

问题描述

我有两个Delphi7的程序:一个COM自动化服务器(EXE)和正在使用的自动化服务器的其他程序

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?

我知道VarArrayCreate和VarArrayLowBound / VarArrayHighBound,但我对如何做到这一点无法确定正确的。

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

谢谢!

推荐答案

您这样创建:

首先声明

var
  VarArray: Variant;
  Value: Variant;

然后建立:

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

或者你也可以有

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;

最后,你清除阵列时,你不需要它了。编辑:(这是可选的,请参见 2009年德尔福做我需要释放的变体数组?

VarClear(VarArray);

这是所有有给它。另一个例子看看官方的 Embracadero帮助

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

编辑:

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

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

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

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