添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
鼻子大的人字拖  ·  How to Prevent Out of ...·  1 月前    · 
闷骚的树叶  ·  How Fast is .NET ...·  1 月前    · 
另类的路灯  ·  Array.prototype.splice ...·  3 周前    · 
豁达的充电器  ·  Anonymous access is ...·  1 年前    · 
乖乖的钱包  ·  了解并解决 Azure Active ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

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);

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.

Thanks! Could you also give some example code on how to read the array back on the client's side? Let's says a vararray is passed to the client in a Variant, how do you read it? Do you call VarArrayCreate or just use VarArrayLowBound etc.? – Steve Sep 1, 2010 at 18:38 Just use the array on the other side. Create it only once. But watch out who is responsible for clearing it. – Runner Sep 1, 2010 at 19:06 Never said it is the same thing. I also provided an URL to where the reference counted nature is explained. – Runner May 16, 2019 at 18:21

For the other side:

(assuming Value is the Variant parameter and the element type is WideString)

Source: PWideStringArray; if VarIsArray(Value) then begin Source:= VarArrayLock(Value); for i:= 0 to TVarData(Value).VArray^.Bounds[0].ElementCount - 1 do DoWhatEverYouWantWith(Source^[i]); finally VarArrayUnlock(Value); What is VarArrayLock for? I'm using a byte array btw. Where is PWideStringArray defined? Is there a PByteArray as well? Thanks! – Steve Sep 1, 2010 at 22:12 PWideStringArray is a self defined type, as the name implies, a pointer to an array of WideStrings. You can find PByteArray in SysUtils. VarArrayLock locks the array and returns a pointer to the memory containing the array for faster access. As you have different processes fiddling around with this array, I suggest to use this functionality. You can look up more information in the D7 help. – Uwe Raabe Sep 2, 2010 at 7:52

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.