Evaluate preprocessor macro on run time in Inno Setup Pascal Script

前端 未结 1 381
时光说笑
时光说笑 2020-12-06 21:08

I\'m using an Inno Setup #define directive to define all of the information about a software package in my installer and then other directives to extract portions of that st

相关标签:
1条回答
  • 2020-12-06 21:49

    This cannot work.

    The PartNumber is a preprocessor function/macro. It's evaluated on compile time. It does not exist on run time.


    You can, of course, implement an equivalent Pascal Script function:

    function PartNumberPascal(Package: string): string;
    begin
      Result := Copy(Package, 1, 5);
    end;
    
    procedure Foo(Package: String);
    var
      PartNumber: String;
    begin
      PartNumber := PartNumberPascal(Package);
    end;
    

    What probably confuses you, is this call:

    Foo(ExpandConstant('{#Package1}')); 
    

    It may give you an impression that the ExpandConstant function expands the Package1 preprocessor define.

    It does not!

    The {#...} syntax (contrary to the {...}) is not a constant. It's an inline preprocessor directive call, where, when no directive is explicitly specified, the emit is implied. So the {#Package1} is the same as {#emit Package1}. And as every preprocessor construct, it's evaluated on compile time.

    If you add SaveToFile preprocessor function call to the end of the script:

    procedure Bar();
    begin
      Foo(ExpandConstant('{#Package1}')); 
    end;
    
    #expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")
    

    And after compilation, check, what the Preprocessed.iss is like. You will see:

    procedure Bar();
    begin
      Foo(ExpandConstant('05414 - My Package')); 
    end;
    

    The Package1 is expanded to its value. But the ExpandConstant is still there, and hence it's totally useless! (there are no constants in the '05414 - My Package')

    This would have the same effect:

    procedure Bar();
    begin
      Foo('{#Package1}'); 
    end;
    

    For a similar question, see:
    Evaluate a collection of data from preprocessor on run time in Inno Setup Pascal Script

    0 讨论(0)
提交回复
热议问题