I am creating random numbers and using them in multiple places in other code section. If I use /Create /F /SC DAILY /ST {code:MyRand}:{code:MyRand} ...", directly in Code section it generates the random each time, when it gets called. But I want it create only one random number per installation. So how can I pass the result of MyRand to a variable and use that variable in the other code section?
[Code]
function MyRand(Param: string): string;
begin
Result := IntToStr(Random(1000));
end;
Generate the random number on installer start and refer to the generated value in the scripted constant function.
var
MyRandValue: Integer;
function InitializeSetup(): Boolean;
begin
MyRandValue := Random(1000);
Result := True;
end;
function MyRand(Param: string): string;
begin
Result := IntToStr(MyRandValue);
end;
Related
after trying for two days I finally decided to ask my first question here on Stackoverflow.
I have some experience programming in C#, but can't get my head around simple tasks in Pascal.
Like the title says, i simply want to read out the currently selected radio button, which should change another variable's name.
The variable determines where the file unpacks on my pc.
Note: I am already able to read out my 'VersionNumber' variable, however it doesn't contain my selected element!
[Code]
var
Page1: TInputOptionWizardPage;
SetupString21:string;
SetupString22:string;
SetupBool21:Boolean;
SetupBool22:Boolean;
VersionNumber:string;
procedure InitializeWizard;
begin
SetupString21 := '2021'
SetupString22 := '2022'
VersionNumber := SetupString21
Page1:= CreateInputOptionPage(1, 'Select a version', 'Help text', 'Second help text', True, False);
//add items
Page1.Add(SetupString21);
Page1.Add(SetupString22);
//set initial values (optional)
Page1.Values[0] := True;
//read values into variables
SetupBool21 := Page1.Values[0]
SetupBool22 := Page1.Values[1]
if WizardForm.TypesCombo.SelectedValueIndex = SetupString22 then VersionNumber := SetupString22;
end;
function GetParams(Value: string): string;
begin
Result := VersionNumber;
end;
You didn't give us any context. Though from the signature and name of the GetParams function, I'll make a guess that it is an implementation of a scripted constant for a [Run] section.
Then you probably want this:
var
SetupIndex21: Integer; // will be 0
SetupIndex22: Integer; // will be 1
procedure InitializeWizard;
begin
// ...
SetupIndex21 := Page1.Add(SetupString21);
SetupIndex22 := Page1.Add(SetupString22);
// set initial value
Page1.SelectedValueIndex := SetupIndex21;
// ...
end;
function GetParams(Value: string): string;
begin
if Page1.SelectedValueIndex = SetupIndex21 then Result := SetupString21
else
if Page1.SelectedValueIndex = SetupIndex22 then Result := SetupString22;
end;
In my Inno Setup script there's a task that may be used under certain conditions which are determined by code. In no other conditions this task should be executed. In fact that entire Tasks page is skipped then. Unfortunately the task selection is remembered by Inno Setup and restored on every following update setup, even if the page isn't visible at all.
I now need to uncheck that task generally at every setup initialisation in order to forget the last selected state. But I can't get this to work. Here's my latest try:
[Tasks]
Name: DeleteConfig; Description: "{cm:Task_DeleteConfig}"; Flags: unchecked
#define Task_DeleteConfig_Index 0
[InstallDelete]
; Delete user configuration files if the task is selected
Type: files; Name: "{userappdata}\...\App.conf"; Tasks: DeleteConfig
[Code]
var
IsDowngradeSetup: Boolean;
function InitializeSetup: Boolean;
begin
// More code not shown here, but the following may be set under certain conditions
IsDowngradeSetup := true;
end;
procedure InitializeWizard;
begin
// Clear possibly remembered value from previous downgrade install
WizardForm.TasksList.Checked[{#Task_DeleteConfig_Index}] := false;
end;
function ShouldSkipPage(PageID: Integer): Boolean;
begin
// Make upgrade install quicker
Result := ((PageID = wpSelectTasks) or ((PageID = wpReady) and (GetArrayLength(products) = 0))) and PrevInstallExists;
end;
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageID = wpWelcome then
begin
if PrevInstallExists then
begin
// Change "Next" button to "Upgrade" on the first page, because it won't ask any more
WizardForm.NextButton.Caption := ExpandConstant('{cm:Upgrade}');
WizardForm.FinishedHeadingLabel.Caption := ExpandConstant('{cm:UpdatedHeadingLabel}');
end;
end;
if CurPageID = wpSelectTasks then
begin
if IsDowngradeSetup then
begin
// Pre-select task to delete existing configuration on downgrading (user can deselect it again)
// (Use the zero-based index of all rows in the tasks list GUI)
// Source: http://stackoverflow.com/a/10490352/143684
WizardForm.TasksList.Checked[{#Task_DeleteConfig_Index}] := true;
end;
end;
end;
This gives me a
Runtime error (at 85:77): List index out of bounds (0).
I don't know where "85:77" is supposed to be but from the only recent changes it can only be the quoted code above.
I first had that in the InitializeSetup function but that didn't work either.
Where should I put this code so that it works and finds a fully initialised tasks list? The tasks page may not be shown so I think it's too late to wait for the page to become visible. In fact the code used to be there and wasn't called when the page was skipped.
I do not understand, why you need to reset the task. I have an impression that you have that conditional skip of the task implemented incorrectly.
It's just a guess, but I assume that you skip the task page using the ShouldSkipPage. So the task stays checked, if it was enabled in a previous installation.
Do not use the ShouldSkipPage for this, use the Check parameter instead. If there's a single task only that is conditionally disabled using the Check parameter, whole task page gets skipped.
[Tasks]
Name: DeleteConfig; Description: "{cm:Task_DeleteConfig}"; Flags: unchecked; \
Check: UseDeleteConfig
[Code]
function UseDeleteConfig: Boolean;
begin
Result := IsDowngradeSetup;
end;
To answer your actual question, you can do this:
procedure CurPageChanged(CurPageID: Integer);
begin
if CurPageId = wpReady then
begin
if PrevInstallExists then
begin
{ In Inno Setup 6, you can use WizardSelectTasks }
WizardForm.TasksList.Checked[0] := False;
end;
end;
end;
function UpdateReadyMemo(
Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo,
MemoGroupInfo, MemoTasksInfo: String): String;
begin
if PrevInstallExists then
begin
MemoTasksInfo := '';
end;
end;
Though again, I do not think, that this is a good solution.
Or even easier, use the UsePreviousTasks:
[Setup]
UsePreviousTasks=no
Or similarly using the checkedonce flag:
[Tasks]
Name: DeleteConfig; Description: "{cm:Task_DeleteConfig}"; Flags: unchecked checkedonce
I have some academic interest of how I can store a unique identifier in a dynamically created TThread.
I create something like this:
procedure TForm1.Button1Click(Sender: TObject);
var thrn:word;
begin
for thrn := 0 to 5 do//<--- this is a loop variable that should give the unique numbers
TThread.CreateAnonymousThread(
procedure()
var
i: longint;
r: double;
thrns:string;
begin
thrns:=inttostr(thrn);//in this thread? variable I try to store the ID as string
repeat
for i := 0 to 100000000 do
begin
r := random(high(i));//this loop gives some dummy job
r := sqr(r); //to the thread to slow it down
end;
TThread.Synchronize(nil,
procedure()
begin
memo1.Text:=memo1.Text+#13#10+
'done'+thrns;//it returns strange IDs including '6'
end);
until false;
end).Start;
end;
Can I pass a unique identifier to the dynamically created thread so that it could show it in its synchronize method?
This is a classic misunderstanding. We understand that anonymous methods capture, but what do they capture? The value or the variable?
The answer is the latter. They capture the variable. There is a single variable, thrn, that each of your six anonymous methods capture. Since there is one variable, there is only one value, at any one moment in time.
Of course, since you are executing code in threads, you have a data race on this variable. Hence my "at any one moment in time" proviso. And that's why you have unrepeatable, unpredictable results. And you are likely to access the loop variable after the loop has completed and the value then is undefined.
If you wish to have a different value for each anonymous method, you must make a new variable for each anonymous method. My answer to another question demonstrates that: Anonymous methods - variable capture versus value capture.
So, to illustrate in your context we need some more scaffolding.
function GetThreadProc(thrn: Integer): TProc;
begin
Result :=
procedure
begin
// thrn is passed by value, so a copy is made, i.e. a new variable
....
end;
end;
....
procedure TForm1.Button1Click(Sender: TObject);
var
thrn: Integer;
begin
for thrn := 0 to 5 do
TThread.CreateAnonymousThread(
GetThreadProc(thrn)).Start;
end;
You have to capture the value of your identifier. Here is an example how to do that.
procedure TForm1.Button1Click(Sender: TObject);
function GetAnonProc( ID: Word): TProc;
begin
Result :=
procedure
var
i: longint;
r: double;
thrns:string;
begin
thrns:= inttostr(ID);// Capture value
repeat
for i := 0 to 100000000 do
begin
r := random(high(i));//this loop gives some dummy job
r := sqr(r); //to the thread to slow it down
end;
TThread.Synchronize(nil,
procedure()
begin
memo1.Text:=memo1.Text+#13#10+
'done'+thrns;//it returns strange IDs including '6'
end);
until false;
end;
end;
var
thrn:word;
p: TProc;
begin
for thrn := 0 to 5 do
begin
p := GetAnonProc(thrn); // Capture thrn value
TThread.CreateAnonymousThread(p).Start;
end;
end;
The code above captures 6 different references to a local ID variable. Each with a different value.
The code in the question captures a single variable reference. Since you cannot control when the threads are running, there is no way to predict what value they will retrieve from the variable reference. The value 6 you observe is because of the fact that a loop variable's value is undefined after the loop is completed.
To further understand how anonymous methods works and use variable binding, read Variable Binding Mechanism.
How do you make Inno Setup disable CreateUninstallRegKey via code?
My setup.exe file created in Inno Setup accepts parameters, e.g.:
setup.exe -a
or
setup.exe -b
If -a parameter is supplied, then enable CreateUninstallRegKey, or if -b parameter is supplied, then disable CreateUninstallRegKey.
Is there anyway to set CreateUninstallRegKey via code or do I have to make a function then call the function in script section?
This help page explains about using {code:...} constants, but unfortunately I got this error:
Thanks
Do not use the {code:} expression for passing values to Boolean type directives. Do it this way:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
CreateUninstallRegKey=NeedsUninstallRegKey
[Code]
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
function NeedsUninstallRegKey: Boolean;
begin
Result := CmdLineParamExists('-a');
end;
Saving, editing and loading information. The information that I want to load is something I will add myself. Each line of information will contain 4 pieces, (string, integer, string, integer). Via 4 seperate edit boxes and a button I will add this information to a 'database' (not sure if I need a database or if it can be done via something like a Tstringlist). Everytime the button is clicked it will added the content that is typed at that moment in the 'database'.
The only demand of the saved data is when I type the first string from the list it could place the rest of the information that belongs to it in a memobox or edit boxes as well. So I suppose I have to be able to search. Just want to keep it as simple as possible. There will only be about 10 to 15 lines of information. and if possible it would be good if I can load them again a later time.
Here's some very basic code that should get you on your way. There's no error checking, and you'll no doubt want to develop it and modify it further. The point is that there should be some ideas to help you write code that works for you.
Now that I have comma-separated the fields, but made no attempt to handle the appearance of commas in any of the values. If this is a problem then choose a different delimiter, or escape the commas. I had toyed with writing each field on its own line (effectively using a newline as the separator), but this makes the reading code more tricky to write.
Again, the main point is that this is not final production code, but is intended to give you a starting point.
function Split(const s: string; Separator: char): TStringDynArray;
var
i, ItemIndex: Integer;
len: Integer;
SeparatorCount: Integer;
Start: Integer;
begin
len := Length(s);
if len=0 then begin
Result := nil;
exit;
end;
SeparatorCount := 0;
for i := 1 to len do begin
if s[i]=Separator then begin
inc(SeparatorCount);
end;
end;
SetLength(Result, SeparatorCount+1);
ItemIndex := 0;
Start := 1;
for i := 1 to len do begin
if s[i]=Separator then begin
Result[ItemIndex] := Copy(s, Start, i-Start);
inc(ItemIndex);
Start := i+1;
end;
end;
Result[ItemIndex] := Copy(s, Start, len-Start+1);
end;
type
TValue = record
i1, i2: Integer;
s: string;
end;
TMyDict = class(TDictionary<string,TValue>)
public
procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);
end;
{ TMyDict }
procedure TMyDict.SaveToFile(const FileName: string);
var
Strings: TStringList;
Item: TPair<string,TValue>;
begin
Strings := TStringList.Create;
Try
for Item in Self do begin
Strings.Add(Format(
'%s,%s,%d,%d',
[Item.Key, Item.Value.s, Item.Value.i1, Item.Value.i2]
));
end;
Strings.SaveToFile(FileName);
Finally
FreeAndNil(Strings);
End;
end;
procedure TMyDict.LoadFromFile(const FileName: string);
var
Strings: TStringList;
Item: TPair<string,TValue>;
Line: string;
Fields: TStringDynArray;
begin
Strings := TStringList.Create;
Try
Strings.LoadFromFile(FileName);
for Line in Strings do begin
Fields := Split(Line, ',');
Assert(Length(Fields)=4);
Item.Key := Fields[0];
Item.Value.s := Fields[1];
Item.Value.i1 := StrToInt(Fields[2]);
Item.Value.i2 := StrToInt(Fields[3]);
Add(Item.Key, Item.Value);
end;
Finally
FreeAndNil(Strings);
End;
end;
Note that you don't attempt to search the file on disk. You simply load it into memory, into the dictionary and look things up from there.
A dictionary is great when you always use the same key. If you have multiple keys then a dictionary is less convenient, but who cares about the performance impact if you've only got 15 records?!
Disclaimer: I've not run the code, I've not tested it, etc. etc.