InnoSetup:如何将二维字符串数组传递给函数

时间:2013-08-29 09:17:09

标签: inno-setup pascalscript

Innosetup正在杀了我。我得到了RUNTIME' Type Mismatch'错误,对我而言,非常意外。我正在使用Inno-setup 5.5.3(u) (其中' u'表示unicode版本)

我试图将二维数组传递给方法。

这是我的完整示例。

[Setup]
AppName=EmptyProgram
AppVerName=EmptyProgram 1
UsePreviousAppDir=false
DefaultDirName={pf}\EmptyProgram
Uninstallable=false
OutputBaseFilename=HelloWorld
PrivilegesRequired=none

[Messages]
SetupAppTitle=My Title

[Code]
var
    langMap : array[0..3] of array[0..1] of String;


function getMapVal(map : array of array[0..1] of String; key: String ) : String;
begin
    Result:='not testing the body of the method';
end;

function InitializeSetup(): Boolean;
begin
    MsgBox('Hello world.', mbInformation, MB_OK);

    getMapVal(langMap, 'hello');    // this line here fails with type mismatch! Why?

    Result := FALSE;
end;

此示例将运行,但是对于方法的调用:

getMapVal(langMap,' hello');

它编译,因此对声明感到满意。但是关于invokation,错配错误。我究竟做错了什么?

1 个答案:

答案 0 :(得分:3)

首先,您不是制作哈希映射,而是制作纯键值列表。目前无法在InnoSetup中制作真正的泛型哈希映射。无论如何,您当前的代码需要一个完整的重构。我宁愿这样写:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
type
  TKey = string;
  TValue = string;
  TKeyValue = record
    Key: TKey;
    Value: TValue;
  end;
  TKeyValueList = array of TKeyValue;

function TryGetValue(const KeyValueList: TKeyValueList; const Key: TKey; 
  var Value: TValue): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 0 to GetArrayLength(KeyValueList) - 1 do
    if KeyValueList[I].Key = Key then
    begin
      Result := True;
      Value := KeyValueList[I].Value;
      Exit;
    end;
end;

procedure InitializeWizard;
var 
  I: Integer;
  Value: TValue;
  KeyValueList: TKeyValueList;
begin
  SetArrayLength(KeyValueList, 3);
  for I := 0 to 2 do
  begin
    KeyValueList[I].Key := 'Key' + IntToStr(I);
    KeyValueList[I].Value := 'Value' + IntToStr(I);
  end;

  if TryGetValue(KeyValueList, 'Key2', Value) then
    MsgBox('Value: ' + Value, mbInformation, MB_OK);
end;