Inno Setup:遍历 Variant 类型的数组(来自 OleObject)

Inno Setup: Iterate through array of type Variant (from OleObject)

我正在尝试使用 Inno Setup 读取和写入 IIS 6 元数据库。
不过我不知道如何访问数组。

IIS := CreateOleObject('IISNamespace');
Compr := IIS.GetObject('IIsCompressionScheme', 'localhost/W3SVC/Filters/Compression/deflate');
Arr := Compr.HcScriptFileExtensions;
{ ... [code to iterate and add items] here ... }
Compr.SetInfo();

元数据库编辑器将我尝试访问的对象类型称为 "multi-string"。

VarType(Arr) 产生 0x200C 作为类型(参见 http://www.jrsoftware.org/ishelp/topic_isxfunc_vartype.htm

如何使用此类变量? Delphi 支持

for I := VarArrayLowBound(Arr, 1) to VarArrayHighBound(Arr, 1) do

但 Inno Setup 没有。或者我是否必须通过一些 OLE/COM-functions?

完全访问数组

Inno 不提供完整的 Delphi 支持,据我所知,脚本语言是基于 Free Pascal 的。

尝试以下操作:

 for I := 0 to  GetArrayLength(myArray) - 1 do
  begin
     //stuff
  end;   

您可以将Variant转换为array of string,读取并写入数组,然后再转换回来:

var
  VariantArray: Variant;
  Count: Integer;
  ArrayOfStrings: array of string;
  I: Integer;
begin
  { ... }
  VariantArray := Compr.HcScriptFileExtensions;

  { Cast to array }
  ArrayOfStrings := VariantArray;

  { Read the array }
  Count := GetArrayLength(ArrayOfStrings);
  Log(Format('Count = %d', [Count]));

  for I := 0 to Count - 1 do
  begin
    Log(Format('%d: %s', [I, ArrayOfStrings[I]]));
  end;

  { Modify the array (append element) }
  SetArrayLength(ArrayOfStrings, Count + 1);
  ArrayOfStrings[Count] := 'new string';

  { Cast back to the variant }
  VariantArray := ArrayOfStrings;
  ...
end;

仅适用于 Inno Setup 的 Unicode 版本。可能是因为 Unicode Inno Setup is compiled with Delphi 2009 instead of Delphi 2 and 3, which likely has better Variant support. See also .