I am running this code:
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
begin
if CompareText(Copy(ParamStr(I), 1, Length(Value)), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
end;
function GetAppName(Value: string): string;
begin
if CmdLineParamExists('/COMPONENTS=prog2') then
begin
Result := 'Программа 2'; //<----This shows up as ????????? 2
end
else
begin
Result := '{#SetupSetting("AppName")}';
end;
end;
procedure CurPageChanged(CurPageID: Integer);
var
S: string;
Begin
if CurPageID = wpSelectDir then
begin
S := SetupMessage(msgSelectDirLabel3);
StringChange(S, '[name]', GetAppName(''));
WizardForm.SelectDirLabel.Caption := S;
end;
end;
Now, I'm not sure what I'm doing wrong here. Every other string shows up correctly, except when I use the result of GetAppName. Should I convert anything to AnsiString at some point?
I'm assuming that you are using Ansi version of Inno Setup.
In the Ansi version, the culprit is probably the StringChange as it does not play nicely with non-Ansi character sets. Try using StringChangeEx.
Though you should be using Unicode version of Inno Setup anyway.
Only the most recent version of Inno Setup, 5.6, does support Unicode string literals. So make sure you have the latest version.
If you are stuck with an older version:
Encode the string like
#$041F#$0440#$043E#$0433#$0440#$0430#$043C#$043C#$0430 + ' 2'
Or, actually the most correct way is to add a new custom message to the language files (like Russian.isl):
[CustomMessages]
Program2=Программа 2
And load it like:
CustomMessage('Program2')
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;
I am reading and writing data from a file using a filestream but am having a problem reading strings from my file.
In a test VCL form program I have written:
procedure tform1.ReadfromFile4;
var
fs: TFileStream;
arrayString: Array of String;
i, Len1 : Cardinal;
// s : string;
begin
fs := TFileStream.Create('C:\Users\Joe\Documents\Delphi\Streamtest.tst',
fmOpenRead or fmShareDenyWrite);
Memo1.lines.clear;
try
fs.ReadBuffer(Len1, SizeOf(Len1));
SetLength(arrayString, Len1);
FOR i := 0 to Len1-1 do begin
fs.ReadBuffer(Len1, SizeOf(Len1));
SetLength(arrayString[i], Len1);
Fs.ReadBuffer(arrayString[i], Len1);
memo1.lines.add (arrayString[i]);
end;
finally
fs.free;
end;
end;
procedure tform1.WriteToFile4;
var
fs: TFileStream;
arrayString: Array of String;
Len1, c, i: Cardinal;
begin
Memo1.lines.clear;
SetLength(arrayString, 4);
arrayString[0] := 'First string in this Array';
arrayString[1] := 'the Second Array string';
arrayString[2] := 'String number three of this Array';
arrayString[3] := 'this is the fourth String';
fs := TFileStream.Create('C:\Users\Joe\Documents\Delphi\Streamtest.tst',
fmCreate or fmOpenWrite or fmShareDenyWrite);
try
c := Length(arrayString);
Fs.WriteBuffer(c, SizeOf(c));
for i := 0 to c-1 do begin
Len1 := Length(arrayString[i]);
fs.WriteBuffer(Len1, SizeOf(Len1));
if Len1 > 0 then begin
fs.WriteBuffer(arrayString[i], Len1);
end;
end;
finally
fs.free;
end;
end;
The Save button action enters the four strings correctly, but the Load button (readFromFile4) fails to load the strings from the file. Using the Watch list, I find that the string lengths are set correctly for each string, but the data accessed is not the correct string values. I believe I am faithfully following the instructions on the website : http://www.angelfire.com/hi5/delphizeus/customfiles.html]1 in the section titled
Writing and Reading Dynamic Arrays of Non-Fixed Size Variables
Can anyone shed light on why this does not read the strings from the file correctly?
I'm creating a Inno Setup installer/updater for my application. Now I need to find a way to check if a new version is available and if it is available it should be installed automatically over the already installed version.
The special case is that the version number is in a file with other data.
The file that Inno Setup need to read looks like:
#Eclipse Product File
#Fri Aug 18 08:20:35 CEST 2017
version=0.21.0
name=appName
id=appId
I already found a way to update the application using a script that only read a text file with the version number in it.
Inno setup: check for new updates
But in my case it contains more data that the installer does not need. Can someone help me to build a script that can parse the version number out of the file?
The code that I already have looks like:
function GetInstallDir(const FileName, Section: string): string;
var
S: string;
DirLine: Integer;
LineCount: Integer;
SectionLine: Integer;
Lines: TArrayOfString;
begin
Result := '';
Log('start');
if LoadStringsFromFile(FileName, Lines) then
begin
Log('Loaded file');
LineCount := GetArrayLength(Lines);
for SectionLine := 0 to LineCount - 1 do
Log('File line ' + lines[SectionLine]);
if (pos('version=', Lines[SectionLine]) <> 0) then
begin
Log('version found');
S := RemoveQuotes(Trim(Lines[SectionLine]));
StringChangeEx(S, '\\', '\', True);
Result := S;
Exit;
end;
end;
end;
But when running the script the check for checking if the version string is on the line does not work.
Your code is almost correct. You are only missing begin and end around the code, that you want to repeat in the for loop. So only the Log line repeats; and the if is executed for out-of-the-range LineCount index.
It becomes obvious, if you format the code better:
function GetInstallDir(const FileName, Section: string): string;
var
S: string;
DirLine: Integer;
LineCount: Integer;
SectionLine: Integer;
Lines: TArrayOfString;
begin
Result := '';
Log('start');
if LoadStringsFromFile(FileName, Lines) then
begin
Log('Loaded file');
LineCount := GetArrayLength(Lines);
for SectionLine := 0 to LineCount - 1 do
begin { <--- Missing }
Log('File line ' + lines[SectionLine] );
if (pos('version=', Lines[SectionLine]) <> 0) then
begin
Log('version found');
S := RemoveQuotes(Trim(Lines[SectionLine]));
StringChangeEx(S, '\\', '\', True);
Result := S;
Exit;
end;
end; { <--- Missing }
end;
end;
My program has 4 services and the first small service controls the other three services (stops them on a file server for overwriting). The first one is very small and simple and normally it should not be closed (only when newer).
[Files]
Source: "ctrlserversvc3.exe"; DestDir: "{code:GetInstallDir|Program}"
It is in the Files section. Inno Setup asks me everytime for closing the service and restarting it at the end. But it should only ask me when my service in the setup has a newer version, not when it is the same version.
How can I tell Inno Setup to skip this file or question when nothing to change?
Inno Setup, unfortunately, does not check file version before checking if the file is locked. It should, imho. Because the file won't get installed in the end, if it has the same version.
Anyway, you can implement the check yourself, using the Check parameter;
[Files]
Source: "ctrlserversvc3.exe"; DestDir: "{code:GetInstallDir|Program}"; \
Check: IsNewer('{#GetFileVersion("ctrlserversvc3.exe")}')
[Code]
procedure CutVersionPart(var VersionString: string; var VersionPart: Word);
var
P: Integer;
begin
P := Pos('.', VersionString);
if P > 0 then
begin
VersionPart := StrToIntDef(Copy(VersionString, 1, P - 1), 0);
Delete(VersionString, 1, P);
end
else
begin
VersionPart := StrToIntDef(VersionString, 0);
VersionString := '';
end;
end;
function IsNewer(InstalledVersion: string): Boolean;
var
Filename: string;
ExistingMS, ExistingLS: Cardinal;
ExistingMajor, ExistingMinor, ExistingRev, ExistingBuild: Cardinal;
InstalledMajor, InstalledMinor, InstalledRev, InstalledBuild: Word;
begin
Filename := ExpandConstant(CurrentFilename);
Log(Format('Checking if %s should be installed', [Filename]));
if not FileExists(Filename) then
begin
Log(Format('File %s does not exist yet, allowing installation', [FileName]));
Result := True;
end
else
if not GetVersionNumbers(FileName, ExistingMS, ExistingLS) then
begin
Log(Format('Cannot retrieve version of existing file %s, allowing installation',
[FileName]));
Result := True;
end
else
begin
ExistingMajor := ExistingMS shr 16;
ExistingMinor := ExistingMS and $FFFF;
ExistingRev := ExistingLS shr 16;
ExistingBuild := ExistingLS and $FFFF;
Log(Format('Existing file %s version: %d.%d.%d.%d',
[FileName, ExistingMajor, ExistingMinor, ExistingRev, ExistingBuild]));
Log(Format('Installing version: %s', [InstalledVersion]));
CutVersionPart(InstalledVersion, InstalledMajor);
CutVersionPart(InstalledVersion, InstalledMinor);
CutVersionPart(InstalledVersion, InstalledRev);
CutVersionPart(InstalledVersion, InstalledBuild);
Log(Format('Installing version: %d.%d.%d.%d',
[InstalledMajor, InstalledMinor, InstalledRev, InstalledBuild]));
if (InstalledMajor > ExistingMajor) or
((InstalledMajor = ExistingMajor) and (InstalledMinor > ExistingMinor)) or
((InstalledMajor = ExistingMajor) and (InstalledMinor = ExistingMinor) and
(InstalledRev > ExistingRev)) then
begin
Log('Installing file that is newer than existing file, ' +
'allowing installation.');
Result := True;
end
else
begin
Log('Installing file that is same or older than existing file, ' +
'skipping installation.');
Result := False;
end;
end;
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.