inno-setup

Cannot read value in Inno Setup Pascal Script from an array defined by #define preprocessor


I try to read the value from an array declared by using #define preprocessor. I have done that in the following way

#dim MYARRAY[2]
#define MYARRAY[0] 'foo'
#define MYARRAY[1] 'bar'
#define MYARRAY_SIZE 2

[Code]

procedure do_something();
var
    I: Integer;
begin
    for I := 0 to {#MYARRAY_SIZE} do
    begin
        SuppressibleMsgBox('{#MYARRAY[I]}', mbInformation, MB_OK, IDOK);
    end;
end;

Unfortunately, I got the following error when compiling

Undeclared identifier: I

Any ideas how to solve this problem?


Solution

  • You cannot access a pre-processor defined array within a Pascal script index, remember the Pascal script is executed at run-time, while the pre-processor code is interpreted before the compilation of the installer, much like the C pre-processor.

    Because of that, to access your array elements, you have to use a pre-processor for loop that will generate the actual code passed to the compiler. Taking your code example, it could be something like:

    #dim MYARRAY[2]
    #define MYARRAY[0] "foo"
    #define MYARRAY[1] "bar"
    
    [code]
    procedure do_something();
    begin
    #sub SupMsg
      SuppressibleMsgBox('{#MYARRAY[I]}', mbInformation, MB_OK, IDOK);
    #endsub  
    #define I
    #for {I = 0; I < DimOf(MYARRAY); I++} SupMsg
    end;
    

    When you compile the script, the pre-processor code will be interpreted and executed and you get the result generated in the preprocessor output, which will contain a call to SuppressibleMsgBox for each element, in this case:

    procedure do_something();
    begin
      SuppressibleMsgBox('foo', mbInformation, MB_OK, IDOK);
      SuppressibleMsgBox('bar', mbInformation, MB_OK, IDOK);
    end;