Can not kill process InnoSetup - inno-setup

Trying to kill process via InnoSetup installer. But it still shows an error that the processes are running.
I use this.
[Files]
Source: Files\CefSharp.BrowserSubprocess.exe; DestDir: {app}; Flags: overwritereadonly ignoreversion uninsremovereadonly; BeforeInstall: TaskKill('CefSharp.BrowserSubprocess.exe')
[Code]
procedure TaskKill(fileName: String);
var
ResultCode: Integer;
begin
Exec(ExpandConstant('{sys}/taskkill.exe'), '/f /im ' + '"' + fileName + '"', ExpandConstant('{sys}'), SW_HIDE, ewWaitUntilTerminated, ResultCode);
end;
or
[Code]
procedure TaskKill(fileName: String);
var
ResultCode: Integer;
begin
Exec(ExpandConstant('taskkill.exe'), '/f /im ' + '"' + fileName + '"', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
end;
Please help

Related

How do I perform a move folder and delete operation using Innosetup [Windows Platform]

I am a beginner in Pascal coding and Innosetup. Would like to request some advice on how to use the procedure to execute a delete folder and transfer folder operation.
I was trying to move files from the release folder in 'C:\Program Files (x86)\winpxe1\Apache24\htdocs\current\Storygame*'
to the current folder instead.
'C:\Program Files (x86)\winpxe1\Apache24\htdocs\current\"
Next, I wanted to delete another release folder of the same name in 'C:\Program Files (x86)\winpxe1\Apache24\Storygame'.
The following is an extract of my code:
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}";GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "C:\Program Files (x86)\Inno Setup 5\Examples\MyProg.exe";
DestDir: "{app}"; Flags: ignoreversion
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram, {#StringChange(MyAppName, '&', '&&')}}";AfterInstall: DirectoryCopy DeleteTransferFolder Flags: nowait postinstall skipifsilent
[Code]
procedure DirectoryCopy(SourcePath, DestPath: string);
var
FindRec: TFindRec;
SourceFilePath: string;
DestFilePath: string;
begin
if FindFirst(SourcePath + '\*', FindRec) then
begin
try
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
begin
SourceFilePath := SourcePath + '\' + FindRec.Name;
DestFilePath := DestPath + '\' + FindRec.Name;
if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
begin
if FileCopy(SourceFilePath, DestFilePath, False) then
begin
Log(Format('Copied %s to %s', [SourceFilePath, DestFilePath]));
end
else
begin
Log(Format('Failed to copy %s to %s', [SourceFilePath, DestFilePath]));
end;
end
else
begin
if DirExists(DestFilePath) or CreateDir(DestFilePath) then
begin Log(Format('Created %s', [DestFilePath]));
DirectoryCopy(SourceFilePath, DestFilePath);
end
else
begin
Log(Format('Failed to create %s', [DestFilePath]));
end;
end;
end;
until not FindNext(FindRec);
finally
FindClose(FindRec);
end;
end
else
begin
Log(Format('Failed to list %s', [SourcePath]));
end;
end;
procedure DirectoryCopy(SourcePath, DestPath: string);
begin
Result := DirectoryCopy( 'C:\Program Files (x86)\winpxe1\Apache24 \htdocs\current\Storygame\*','C:\Program Files (x86)\winpxe1\Apache24\htdocs\current': string);
end;
procedure DeleteTransferFolder();
begin
MsgBox('DeleteTransferFolder 1', mbInformation, MB_OK);
if (FileExists (ExpandConstant( 'C:\Program Files (x86)\winpxe1\Apache24 \Storygame'))) then
begin DelTree(ExpandConstant('C:\Program Files (x86)\winpxe1 \Apache24 \Storygame'), True, True, True);
MsgBox('DeleteTransferFolder 2', mbInformation, MB_OK); end;
After running this script, I encountered the "Compiler error": "Directive or Parameter "AfterInstall" expression error: Invalid token "DeleteTransferFolder" found."
I would like to also give credit to the authors of the original code for the directorycopy and deletetransferfolder as it was extracted from other questions in this forum.
Any advice appreciated on how I can integrate these codes with the existing support class references of innosetup.
Regards

Inno Setup FileCopy using {app} variable as part of directory

How can i use variables in Inno Setup FileCopy command in the [Code] section?
`FileCopy(ExpandConstant('{app}\Backup\config.ini'),ExpandConstant('{app}\bin\config.ini'),False);`
This is what i am using but not seem to work... Im using this inside a Procedure... And calling this Procedure using Afterinstall: in [Files] section.
[Files]
Source: "C:\dev\bin\config.ini"; DestDir: "{app}\bin\"; Flags: ignoreversion recursesubdirs createallsubdirs; Tasks: Steam; AfterInstall: RunOtherInstaller;
[Code]
procedure RunOtherInstaller;
var
Path: String;
ErrorCode: Integer;
begin
if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\Valve\Steam', 'InstallPath', Path)) and (FileExists(Path + '\Steam.exe')) then
begin
//ShellExec('', ExpandConstant('"' + Path + '\Steam.exe' + '"'), 'steam://','', SW_SHOW, ewNoWait, ErrorCode);
Exec(ExpandConstant(Path + '\Steam.exe'), 'steam://', '', SW_SHOWNORMAL, ewNoWait, ErrorCode)
end
else
begin
MsgBox('Steam not found', mbError, MB_OK);
FileCopy(ExpandConstant('{app}\Backup\config.ini'),ExpandConstant('{app}\bin\config.ini'),False);
end
end;
FileCopy function does not create directories. It just copies files from an existing directory to other existing directory.
What you try to do is to copy config.ini from Backup directory to bin directory.
I have changed the call for steam:// with an example how to run Sang-Froid game (you can change it anyway you need) and then I have added few sample checks that are just an example. At the end there is your FileCopy function with additional check with message if copy action is not possible either due to Config.ini or destination directory missing.
[Files]
Source: "C:\dev\bin\config.ini"; DestDir: "{app}\bin\"; Flags: ignoreversion recursesubdirs createallsubdirs; Tasks: Steam; AfterInstall: RunOtherInstaller;
[Code]
procedure RunOtherInstaller;
var
Path: String;
ErrorCode: Integer;
begin
if (RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\Valve\Steam', 'InstallPath', Path)) and (FileExists(Path + '\Steam.exe')) then
begin
//proper call for Steam:// - this sample tries to run Sang-Froid - Tales of Werewolves
ShellExec('', 'steam://rungameid/227220', '', '', SW_SHOW, ewNoWait, ErrorCode);
//your EXEC
//Exec(ExpandConstant(Path + '\Steam.exe'), 'steam://', '', SW_SHOWNORMAL, ewNoWait, ErrorCode)
end
else
begin
MsgBox('Steam not found', mbError, MB_OK);
//just some checks
MsgBox(ExpandConstant('{app}'), mbError, MB_OK);
if DirExists(ExpandConstant('{app}') + '\bin') then
MsgBox('''bin'' directory Exists', mbError, MB_OK)
else
MsgBox('''bin'' directory does not Exist!', mbError, MB_OK);
if DirExists(ExpandConstant('{app}') + '\Backup') then
MsgBox('''Backup'' directory Exists', mbError, MB_OK)
else
MsgBox('''Backup'' directory does not Exist!', mbError, MB_OK);
//end of checks
//check if Source file exists, check if destination directory exists
if FileExists(ExpandConstant('{app}') + '\Backup\config.ini') and DirExists(ExpandConstant('{app}') + '\bin') then begin
FileCopy(ExpandConstant('{app}') + '\Backup\config.ini',ExpandConstant('{app}') + '\bin\config.ini',False);
end
else begin
MsgBox('Either ''Config.ini'' file or ''bin'' directory does not Exist!', mbError, MB_OK);
end;
end
end;

Backup files and restore them on uninstall with Inno Setup [duplicate]

This question already has answers here:
Backup files and restore them on uninstall with InnoSetup?
(3 answers)
Closed 7 years ago.
How to take backup of older version files while the older version is uninstalled and new version is installed using Inno Setup compiler?
[Files]
Source: "RecordUpdateProcessor\*"; DestDir: "{app}\RecordUpdateProcessor"; \
Flags: ignoreversion recursesubdirs createallsubdirs; \
Source: "setup\nssm.exe*"; DestDir: "{app}\"; Flags: ignoreversion
and in run section using nssm service has been installed.
There's no built-in support for this.
You can implement it using Pascal Script.
Using BeforeInstall function is probably the best way to go.
[Files]
Source: "RecordUpdateProcessor*"; DestDir: "{app}\RecordUpdateProcessor"; \
Flags: ignoreversion recursesubdirs createallsubdirs; \
Source: "setup\nssm.exe*"; DestDir: "{app}\"; \
BeforeInstall: BackupBeforeInstall
[Code]
function BackupFolderPath: string;
begin
Result := AddBackslash(ExpandConstant('{app}')) + 'Backup';
end;
procedure BackupBeforeInstall;
var
SourcePath: string;
DestPath: string;
begin
if not DirExists(BackupFolderPath) then
begin
Log('Creating ' + BackupFolderPath);
CreateDir(BackupFolderPath);
end;
SourcePath := ExpandConstant(CurrentFileName);
DestPath := BackupFolderPath + '\' + ExtractFileName(SourcePath);
Log(Format('Backing up %s to %s', [SourcePath, DestPath]));
if not FileCopy(SourcePath, BackupFolderPath + '\' + ExtractFileName(CurrentFileName), False) then
begin
Log('Backup failed');
end;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
FindRec: TFindRec;
SourceDir: string;
SourcePath: string;
DestDir: string;
DestPath: string;
begin
if CurUninstallStep = usPostUninstall then
begin
SourceDir := BackupFolderPath;
DestDir := RemoveBackslash(ExpandConstant('{app}'));
if FindFirst(SourceDir + '\*.*', FindRec) then
begin
repeat
if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
begin
SourcePath := SourceDir + '\' + FindRec.Name;
DestPath := DestDir + '\' + FindRec.Name
Log(Format('Restoring %s to %s', [SourcePath, DestPath]));
if not RenameFile(SourcePath, DestPath) then
begin
Log('Restore failed');
end;
end;
until not FindNext(FindRec);
end;
FindClose(FindRec);
RemoveDir(SourceDir);
end;
end;
Note the the code needs improving, if you need a recursive backup. The recursesubdirs suggests that. On the contrary setup\nssm.exe* does not look like it could match folders.

INNO - checking file is installed with msgbox

Want to check if 1 file is in directory and if so not to reinstall, do message, and stop program. Near it with code but when I add message it repeats 4 times and still installs! Can you or anyone correct what I am doing please Many thanks Michael
#define MyAppName "Secretary Assistant"
#define MyAppVersion "3.74"
#define MyAppPublisher ""
[Setup]
AppId={{807EB06A-3962-4AF0-967B-D14B0FEF4E9C}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName=C:\Users\{username}\Google Drive\Congregation
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
OutputBaseFilename=Secretary Assistant 3.74 Setup
Compression=lzma
SolidCompression=yes
UsePreviousAppDir=no
DirExistsWarning=No
DisableWelcomePage=yes
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Files]
Source: "D:\GoogleDrive\MapSecAssist\CongData.accdb"; DestDir: "{app}"; Flags: ignoreversion; Check: File1(ExpandConstant('{app}\CongData.accdb'));
[Code]
function File1(FilePath3: String): Boolean;
begin
If FileExists(FilePath3) then
Result := False
else
Result := True
begin
if FileExists(FilePath3) then
MsgBox('Congregation Data file is already in your directory ' + FilePath3 + '. '#13 #13 +
'Setup would OVERWRITE this DATA FILE.'#13 #13+
'Please "Back it up" and then DELETE it from this Directory then start install again !', mbError, MB_OK);
end;
end;
On your place I would use BeforeInstall instead of Check.
Example below:
[Files]
Source: "D:\GoogleDrive\MapSecAssist\CongData.accdb"; DestDir: "{app}";
Flags: ignoreversion; BeforeInstall: File1;
[Code]
var CancelWithoutPrompt: boolean;
function InitializeSetup(): Boolean;
begin
CancelWithoutPrompt := false;
result := true;
end;
procedure File1;
begin
if FileExists(ExpandConstant('{app}\CongData.accdb')) then
begin
MsgBox('Congregation Data file is already in your directory ' +
ExpandConstant('{app}') + '. '#13 #13 +
'Setup would OVERWRITE this DATA FILE.'#13 #13+
'Please "Back it up" and then DELETE it from this Directory then
start install again !', mbError, MB_OK);
CancelWithoutPrompt := true;
WizardForm.Close;
end;
end;
procedure CancelButtonClick(CurPageID: Integer; var Cancel, Confirm: Boolean);
begin
if CurPageID=wpInstalling then
Confirm := not CancelWithoutPrompt;
end;

Backup files before replace [duplicate]

I would make a backup of files and folders before the [InstallDelete] section deletes them
[Files]
Source: "{app}\res_mods\configs\wotstat\cache.json"; \
DestDir: "{app}\_backup\res_mods_{#DateTime}\configs\wotstat\"; \
Flags: external skipifsourcedoesntexist uninsneveruninstall
Source: "{app}\res_mods\0.9.17.1\vehicles\*"; \
DestDir:"{app}\_backup\res_mods_{#DateTime}\0.9.17.1\vehicles\"; \
Flags: external skipifsourcedoesntexist createallsubdirs recursesubdirs uninsneveruninstall
This works fine. But, if i check
[InstallDelete]
Type: filesandordirs; Name: "{app}\mods\*.*"; Tasks: cleanres
Type: filesandordirs; Name: "{app}\res_mods\*.*"; Tasks: cleanres
No files are saved
How I can made it work. Thx
The [InstallDelete] section is processed (as one would expect) before the [Files] section. See the installation order.
You can code the backup in the CurStepChanged(ssInstall) event, that happens before the installation starts:
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
SourcePath: string;
DestPath: string;
begin
if CurStep = ssInstall then
begin
SourcePath := ExpandConstant('{app}\res_mods\0.9.17.1\vehicles');
DestPath :=
ExpandConstant('{app}\_backup\res_mods_{#DateTime}\0.9.17.1\vehicles');
Log(Format('Backing up %s to %s before installation', [
SourcePath, DestPath]));
if not ForceDirectories(DestPath) then
begin
Log(Format('Failed to create %s', [DestPath]));
end
else
begin
DirectoryCopy(SourcePath, DestPath);
end;
SourcePath := ExpandConstant('{app}\res_mods\configs\wotstat\cache.json');
DestPath :=
ExpandConstant('{app}\_backup\res_mods_{#DateTime}\configs\wotstat');
if not ForceDirectories(DestPath) then
begin
Log(Format('Failed to create %s', [DestPath]));
end
else
begin
if not FileCopy(SourcePath, DestPath + '\cache.json', False) then
begin
Log(Format('Failed to copy %s', [SourcePath]));
end
else
begin
Log(Format('Backed up %s', [SourcePath]));
end;
end;
end;
end;
The code uses the DirectoryCopy function from the Inno Setup: copy folder, subfolders and files recursively in Code section.

Resources