Inno Setup Function - Invalid Prototype [duplicate] - inno-setup

This question already has an answer here:
"Identifier Expected" or "Invalid Prototype" when implementing a scripted constant in Inno Setup
(1 answer)
Closed 1 year ago.
I'm using Inno Setup for my installation and am adding the following function to the existing [Code] section:
function Get64Or32: String;
begin
if IsWin64 then
begin
Result := 'Program Files (x86)';
end
else
begin
Result := 'Program Files';
end;
end;
I keep getting the following error:
10:41:08 Invalid prototype for 'Get64Or32'
I have tried several permutations including having the parenthesis after the function name but get the same error each time.
I'm attempting to use this from the [Registry] section to pinpoint the Java location:
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType:string; ValueName:"JRE_HOME"; ValueData:"C:\{code:Get64or32}\Java\jre_1.8.0_151"; Flags: preservestringtype uninsdeletevalue uninsdeletekeyifempty; Permissions: users-modify;

If you look here Pascal Scripting: Scripted Constants it explains it all. To quote:
The syntax of a {code:...} constant is: {code:FunctionName|Param}
The Pascal script can contain several functions which are called when
Setup wants to know the value of a scripted {code:...} constant. The
called function must have 1 String parameter named Param, and must
return a String or a Boolean value depending on where the constant is
used.
The syntax of a {code:...} constant is: {code:FunctionName|Param}
It also provides:
An example function:
[Code]
function MyConst(Param: String): String;
begin
Result := ExpandConstant('{autopf}');
end;
An example useage:
[INI]
FileName: "{app}\MyIni.ini"; Section: "MySettings"; Key: "ShortApp"; String: "{code:GetShortName|{app}}"
You don't have to use the parameter but it must be part of the prototype.

Related

Array variables and dynamic access in [Code] section

My installer has Components which come associated with downloadable files. These things are changing from build to build, so I'm using #insert to create the [Components] section as well as the appropriate entries in the [Files] section.
Some of these components rely on common downloadable files.
To now include the correct urls in the downloads page, I'm currently defining array variables that are named like the component and have as values the names of the required downloadable files, for example:
#dim myfeature[2] {"01aed27862e2087bd117e9b677a8685aebb0be09744723b4a948ba78d6011bac", "677756ac5969f814fd01ae677dbb832ab2e642513fea44ea0a529430d5ec1fdc"}
In the code for the download page I'm checking which components where selected via WizardSelectedComponents() and after converting the string to an array of strings, I'm trying to get to the previously defined variable and that is where I'm failing:
function GetDownloads(): Array of String;
var
Downloads: Array of String;
SelectedComponents: String;
SelectedArray: Array of String;
begin
SelectedComponents := WizardSelectedComponents(False);
// a custom procedure to parse the comma seperated string
SelectedArray := ParseArray(SelectedComponents, SelectedArray);
// trying to get to the constant array now this works:
MsgBox(ExpandConstant('{#myfeature[0]}'), mbInformation, MB_OK);
// same but trying to use the selected component value returns this as a literal
// '+SelectedArray[0]+' instead the expanded value
MsgBox(ExpandConstant('{#' + SelectedArray[0] + '[0]}'), mbInformation, MB_OK);
end;
So I understand something is up with the # mark but I could not find a way to solve this properly.
Thank you!
Markus
ExpandConstant expands Inno Setup "constants", not preprocessor values. See also Evaluate preprocessor macro on run time in Inno Setup Pascal Script.
You cannot access elements of a preprocessor compile-time array using run-time indexes.
If you know C/C++, it's like if you were trying to do:
#define VALUE1 123
#define VALUE2 456
int index = 1;
int value = VALUE ## index
I'm not really sure I completely understand what are you doing. But it seems that you need to create an array on compile time from various sources and use it on runtime.
There are several approaches that can be used for that. But you definitely need runtime array initialized on run time. But the code that initializes it can be generated on compile time.
An example of the approach follows (and some links to other approaches are at the end).
At the beginning of your script, define these support functions:
[Code]
var
FeatureDownloads: TStrings;
function AddFeature(
Feature: Integer; CommaSeparatedListOfDownloads: string): Boolean;
begin
if not Assigned(FeatureDownloads) then
begin
FeatureDownloads := TStringList.Create();
end;
while FeatureDownloads.Count <= Feature do
FeatureDownloads.Add('');
if FeatureDownloads[Feature] <> '' then
RaiseException('Downloads for feature already defined');
FeatureDownloads[Feature] := CommaSeparatedListOfDownloads;
Result := True;
end;
#define AddFeature(Feature, CommaSeparatedListOfDownloads) \
"<event('InitializeSetup')>" + NewLine + \
"function InitializeSetupFeature" + Str(Feature) + "(): Boolean;" + NewLine + \
"begin" + NewLine + \
" Result := AddFeature(" + Str(Feature) + ", '" + CommaSeparatedListOfDownloads + "');" + NewLine + \
"end;"
In your components include files, do:
#emit AddFeature(2, "01aed27862e2087bd117e9b677a8685aebb0be09744723b4a948ba78d6011bac,677756ac5969f814fd01ae677dbb832ab2e642513fea44ea0a529430d5ec1fdc")
If you add:
#expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")
to the end of your main script, you will see in the Preprocessed.iss generated by the preprocessor/compiler that the #emit directive expands to:
<event('InitializeSetup')>
function InitializeSetupFeature2(): Boolean;
begin
Result := AddFeature(2, '01aed27862e2087bd117e9b677a8685aebb0be09744723b4a948ba78d6011bac,677756ac5969f814fd01ae677dbb832ab2e642513fea44ea0a529430d5ec1fdc');
end;
Now you have FeatureDownloads Pascal Script runtime variable that you can access using FeatureDownloads[SelectedArray[0]] to get comma-separated string, which you can parse to the individual downloads.
This can be optimimized/improved a lot, but I do not know/understand the extent of your task. But I believe that once you grasp the concept (it might be difficult at the beginning), you will be able to do it yourself.
Another similar questions:
Evaluate a collection of data from preprocessor on run time in Inno Setup Pascal Script (simple example that be easier to grasp initially)
Scripting capabilities in the Registry section (slightly different approach from times event attributes were not available yet – and that's YOUR question)

Generating application arguments for Run section in scripted constant based on state of checkboxes and radio buttons on Finished page of Inno Setup

I want to modify the parameters of a FileName in the [Run] section according to the state of some radio and check buttons. In my code section I have added:
function GetParameterString:String;
var
I: Integer;
s1, s2: String;
begin
for I := 0 to WizardForm.RunList.Items.Count - 1 do
if (wizardform.runlist.items.checked[i] = true) then begin
if (wizardform.runlist.items.itemcaption[i] = 'View Whats''s New in ' + {#MyAppVersion}) then
s1 := GetShellFolderByCSIDL(CSIDL_APPDATA, True) + '\Positron Studio\What''s New.pdf';
if (wizardform.runlist.items.itemcaption[i] = 'Positron Studio Dark' then
s2 := '-d'
else if (wizardform.runlist.Items.itemcaption[i] = 'Positron Studio Light' then
s2 := '-l'
end;
end;
Result := s1 + s2
end;
and I am calling it from the [Run] section like this:
FileName:"{app}\{#MyAppExename}"; parameters: Code: GetParameterString; \
Flags: postinstall nowait
It fails on the wizardform.runlist.items.checked[i] = true with:
Unknown Identifier Checked
How do I get the Checked value of a checkbox or radiobutton?
Lookup Pascal Scripting: Scripted Constants where it states:
The Pascal script can contain several functions which are called when Setup wants to know the value of a scripted {code:...} constant. The called function must have 1 String parameter named Param, and must return a String or a Boolean value depending on where the constant is used.
So try:
FileName: "{app}\{#MyAppExename}"; Parameters: {code:GetParameterString}; Flags: postinstall nowait
The above has not been tested. It may not fix the underlaying issue with what you are actually doing in the GetParameterString method.
Your immediate problem is that there's indeed no wizardform.runlist.items.checked (nor wizardform.runlist.items.itemcaption).
You want WizardForm.RunList.Checked (and WizardForm.RunList.ItemCaption).
See the TNewCheckListBox documentation.
Your next problem will be the invalid syntax of the reference to your scripted constant in the Parameters parameter (as Andrew has already shown in his answer). It should be:
Parameters: {code:GetParameterString};
Your next problem will be "Invalid prototype":
"Identifier Expected" or "Invalid Prototype" when implementing a scripted constant in Inno Setup
It should be:
function GetParameterString(Param: string): string;
Depending on how you application handles the argument, you also might need to the path to the .pdf, as there are spaces in it.
It also bit strange, how you add the -d and -l to the path. Did you really intend to pass something like this to the application?
C:\Users\user\AppData\Roaming\Positron Studio\What's New.pdf-d
It also bit unclear to me, how are you combining multiple "run list" entries states into arguments of one particular entry. But I assume you know what you are doing.

Inno Setup Inputbox for serialcode with output into the registry

I used the code from this Answer from the Member TLama. I think it's exactly what i need, but I have two problems with it:
I need the Serial from the edit boxes in the registry. This is what i tried:
Root: "HKCU"; Subkey: "Software\myProg"; ValueType: string; ValueName: "Serial"; ValueData: "{code:GetSerialNumber}"; Flags: deletevalue uninsdeletevalue
but Inno gives me an error. TLama wrote in his answer (from the Link above), it's enought to call the GetSerialNumber part, but I do sth. wrong...
The other question: Is it possible to prefill the serialbox with an example code? E.g. 12345 or abcde? I'm using only one input box with 10 chars...
Hope someone can help, and sorry for my bad english ;)
You can use UserInfoPage and then {userinfoserial} but if you want to use TLama's solution then you should slightly change NextButtonClick function:
function NextButtonClick(CurPageID: Integer): Boolean;
var
S: string;
I: Integer;
begin
Result := True;
if CurPageID = SerialPage.ID then
begin
S := '';
for I := 0 to High(SerialEdits) do
S := S + SerialEdits[I].Text + '-';
SetLength(S, Length(S)-1);
RegWriteStringValue(HKEY_CURRENT_USER, 'Software\myProg',
'Serial', S);
end;
end;
Var SerialEdits: array of TEdit; has to be set as Global for the script. You also may want to add key to registry later (e.g. with CurStepChanged when ssDone or something) or write your own function that will pass Serial to Result as String and then call it in Registry Section.

Extra character added using {#emit SetupSetting("AppId")} in Inno Setup

I'm having problems using the following code, it's adding an extra "{".
For example:
[Setup]
AppID={{E643099E-1ECE-474F-B043-1E7A7CE405AA}
[Code]
const
INSTALL_KEY = 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1';
Returns the following:
INSTALL_KEY = 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{{E643099E-1ECE-474F-B043-1E7A7CE405AA}_is1';
Which obviously won't work detecting that key with RegKeyExists(HKLM, INSTALL_KEY) because of the extra "{" that isn't in the real path and the script won't compile if you remove the extra character in [Setup] because then it thinks it's a constant.
The issue looks to be resolved on later versions of Inno Setup (5.5.5). Following code works just fine:
[Setup]
AppId={{********-****-****-****-********}
...
[code]
sAppId := ExpandConstant('{#emit SetupSetting("AppId")}_is1');
sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\') + sAppId;
I think that parser doesn't allow to use the custom constants enclosed by the {} characters, because they are used as introductors for the constants, so I think you will have to workaround it. Here is one of the options:
[Setup]
AppID=E643099E-1ECE-474F-B043-1E7A7CE405AA
[code]
const
INSTALL_KEY = '...\Uninstall\{{#emit SetupSetting("AppId")}}_is1';
My guess is that case you've described is just an unexpected bug, because the compiler prompts you to use double bracket in the beginning of your constant though but when you emit such constant you get it with the same double bracket back.
Use StringChange() to remove the extra {:
[Setup]
AppID={{E643099E-1ECE-474F-B043-1E7A7CE405AA}
[Code]
const INSTALL_KEY = '...\Uninstall\{#emit StringChange(SetupSetting("AppId"),"{{","{")}_is1';
The solution is to use ExpandConstant function as already mentioned by others.
[Setup]
AppId={{E643099E-1ECE-474F-B043-1E7A7CE405AA}
[Code]
var
INSTALL_KEY: String;
function InitializeSetup(): Boolean;
begin
INSTALL_KEY := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}_is1');
MsgBox(INSTALL_KEY, mbInformation, MB_OK);
end;
This is the expected behavior, not a bug. In [Code] section, constants and scripted constants are not expanded while emit preprocessor directives {#emit ...}/{#...} are replaced with their values by ISPP. So you need to use ExpandConstant function to convert {{ to {.
In this case, you can't define INSTALL_KEY as a Pascal constant as ExpandConstant function is used.
You could also define your application ID as a constant and use it like this:
#define MyAppID "{{E643099E-1ECE-474F-B043-1E7A7CE405AA}"
[Setup]
AppId={#MyAppID}
[Code]
const INSTALL_KEY = '...\Uninstall\' + ExpandConstant('{#MyAppID}') + '_is1';

How to change defaultdirname parameter just before Install in Inno Setup?

I want to change defaultdirname parameter in ssInstall part. How can I do that? Is there a function for setting [Setup] parameters.
The following global objects are available :
MainForm of type TMainForm, WizardForm of type TWizardForm and UninstallProgressForm of type TUninstallProgressForm and one special constant: crHand of type TControl.Cursor.
If you want to set the default directory in the wizard, just access to it's componants like you would in normal delphi code.
For example, set the directory to a custom value:
WizardForm.DirEdit.Text := 'c:\test';
to read that value you can use the WizardDirValue function.
I say 'just access'... but it took me an hour to figure out ;)
There seems to be no way to change a script constant via scripting.
I think your best bet is to modify the target directory for each entry in the [Files] section, e.g.
[Files]
Source: "MYPROG.EXE"; DestDir: "{code:NewTargetDir}"
and derive your new installation directory like this:
[Code]
function NewTargetDir(Param: String): String;
begin
Result := ExpandConstant('{app}') + '\MySubDir';
end;
Since the NewTargetDir function will be called just before the file is actually copied, this should work.
However, I think you should reconsider your approach. First asking the user to specify a directory to installinto, and then actually installing into a different directory, which seems to be your intent, is the wrong way, IMO. Do you really have a compelling reason to install into another directory than the one specified by the user? Besides, the result of my example code could just as well be achieved by specifying
[Files]
Source: "MYPROG.EXE"; DestDir: "{app}\MySubDir"
without any scripting needed. When in doubt, go for the simpler solution.
I have a similar situation, where the setup app is receiving the install path from the command line.
I'm using the solution proposed by Jonx:
WizardForm.DirEdit.Text := 'c:\test';
Example:
function CompareParameter(param, expected: String): Boolean;
begin
Result := False;
if Length(param) >= Length(expected) then
begin
if CompareText(Copy(param, 1, Length(expected)), expected) = 0 then
begin
Result := True;
end;
end;
end;
function GetParameter(expectedParam: String): String;
var
i : LongInt;
begin
Result := '';
for i := 0 to ParamCount() do
begin
if CompareParameter(ParamStr(i), '/' + expectedParam + '=') then
begin
Result := Copy(ParamStr(i), Length(expectedParam) + 3, Length(ParamStr(i)));
break;
end;
end;
end;
procedure InitializeWizard();
var
newInstallFolder: String;
begin
newInstallFolder := GetParameter('INSTALL_FOLDER');
if Length(newInstallFolder) > 2 then
begin
if Copy(newInstallFolder, 1, 1) = '"' then
newInstallFolder := Copy(newInstallFolder, 2, Length(newInstallFolder) - 2)
if Length(newInstallFolder) > 1 then
WizardForm.DirEdit.Text := newInstallFolder;
end;
end;
The setup app is being started from another setup, in silent mode. It worked OK for me.

Resources