【问题标题】:Inno Setup: Iterate through array of type Variant (from OleObject)Inno Setup:遍历 Variant 类型的数组(来自 OleObject)
【发布时间】:2015-12-16 17:02:25
【问题描述】:

我正在尝试使用 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();

元数据库编辑器调用我试图访问“多字符串”的对象类型。

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 函数完全访问数组?

【问题讨论】:

    标签: inno-setup ole variant pascalscript iis-metabase


    【解决方案1】:

    您可以将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;
    

    仅适用于 Unicode 版本的 Inno Setup。可能是因为 Unicode Inno Setup 是用 Delphi 2009 而不是 Delphi 2 和 3 编译的,后者可能具有更好的 Variant 支持。另见Upgrading from Ansi to Unicode version of Inno Setup (any disadvantages)

    【讨论】:

    • 这会引发“类型不匹配”异常 (ArrayOfStrings := VariantArray)。
    • 我使用的是 Ansi 版本(或者更准确地说:启动这个项目的开发人员)。它适用于 Unicode 版本,所以我将移植设置(反正已经在我的议程上)。非常感谢!
    • 好消息。我已将此信息添加到答案中。
    【解决方案2】:

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

    尝试以下方法:

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

    【讨论】:

    • 您不能在Variant 上致电GetArrayLength。你得到运行时错误 "Could not call proc."
    • 我的错,我假设是字符串数组。
    猜你喜欢
    • 2014-03-29
    • 1970-01-01
    • 2011-09-29
    • 2013-11-04
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    相关资源
    最近更新 更多