Inno Setup - Check if file exist in destination or else if doesn't abort the installation - windows

I need my installer to check if a file exists in the destination location, and if is not there, then the installation aborts. My project is a update patch, so I want the installer to avoid installing the update files if the main exe of the application is not in the destination. How can I do this?
Can someone give an example of code to check file version through the Windows registry?
[Files]
Source C:\filename.exe; DestDir {app}; Flags: ignoreversion; BeforeInstall: CheckForFile;
[code]
procedure CheckForFile(): Boolean;
begin
if (FileExists('c:\somefile.exe')) then
begin
MsgBox('File exists, install continues', mbInformation, MB_OK);
Result := True;
end
else
begin
MsgBox('File does not exist, install stops', mbCriticalError, MB_OK);
Result := False;
end;
end;

Just don't let the user proceed until they pick the correct folder.
function NextButtonClick(PageId: Integer): Boolean;
begin
Result := True;
if (PageId = wpSelectDir) and not FileExists(ExpandConstant('{app}\yourapp.exe')) then begin
MsgBox('YourApp does not seem to be installed in that folder. Please select the correct folder.', mbError, MB_OK);
Result := False;
exit;
end;
end;
Of course, it's also a good idea to try to automatically pick the correct folder for them, eg. by retrieving the correct location out of the registry.

Another solution would be the InitializeSetup():
Credit: Manfred
[code]
function InitializeSetup(): Boolean;
begin
if (FileExists(ExpandConstant('{pf}\{#MyAppName}\somefile.exe'))) then
begin
MsgBox('Installation validated', mbInformation, MB_OK);
Result := True;
end
else
begin
MsgBox('Abort installation', mbCriticalError, MB_OK);
Result := False;
end;
end;

Related

Detect if restart is pending before installing in Inno Setup

Is there a way to prevent installation if a reboot/restart is already pending/required?
Our setup installs SQL Server Express and it will sometimes refuse to do so if there is a pending restart in the system. Can Inno Setup detect this condition so I can tell the user to reboot before installing our software?
I know about MakePendingFileRenameOperationsChecksum but it's usually mentioned to detect whether the reboot required condition appeared DURING the setup. Can it be used BEFORE?
If you want to detect, if there is a pending rename that requires a restart, query PendingFileRenameOperations registry value.
See also How to find out if an MSI I just installed requested a Windows reboot?
function IsRestartPending: Boolean;
var
S: string;
begin
if RegQueryMultiStringValue(
HKLM, 'SYSTEM\CurrentControlSet\Control\Session Manager',
'PendingFileRenameOperations', S) then
begin
Log(Format('PendingFileRenameOperations value exists with value [%s]', [S]));
Result := (Trim(S) <> ''); { This additional check is probably not needed }
end
else
begin
Log('PendingFileRenameOperations value does not exist');
Result := False;
end;
end;
function InitializeSetup(): Boolean;
begin
if IsRestartPending then
begin
MsgBox('Restart your machine please', mbError, MB_OK);
Result := False;
Exit;
end;
Result := True;
end;
If you need to test for other actions that may need restart, you will have to adapt the answer by #Jerry for Inno Setup.
The other accepted answer only covered one scenario. However, there are actually numerous different scenarios to check. I found this article describing all the different registry things to check, and wrote a function around it. This is actually in Delphi, but should be easily implemented in Inno Setup as well. In fact, part of it I got from here, which was in Inno Setup, for checking valid GUID.
Please note that this generally checks for pending Windows updates, which may not necessarily be desired. You can modify it and remove those checks you don't wish to perform for your purpose. The second and third checks for PendingFileRenameOperations actually appear not necessary for SQL Server.
const
S_OK = $00000000;
CO_E_CLASSSTRING = $800401F3;
type
LPCLSID = TGUID;
LPCOLESTR = WideString;
function CLSIDFromString(lpsz: LPCOLESTR; pclsid: LPCLSID): HRESULT;
stdcall; external 'ole32.dll';
function IsValidGuid(const Value: string): Boolean;
var
GUID: LPCLSID;
RetVal: HRESULT;
begin
RetVal := CLSIDFromString(LPCOLESTR(Value), GUID);
Result := RetVal = S_OK;
if not Result and (RetVal <> CO_E_CLASSSTRING) then
OleCheck(RetVal);
end;
function IsRestartPending(const RestartOnly: Boolean = True): Boolean;
var
R: TRegistry;
L: TStringList;
X: Integer;
T: String;
begin
R:= TRegistry.Create(KEY_READ);
try
L:= TStringList.Create;
try
Result:= False;
R.RootKey:= HKEY_LOCAL_MACHINE;
if R.OpenKey('SOFTWARE\Microsoft\Updates', False) then begin
try
if R.ValueExists('UpdateExeVolatile') then begin
if R.ReadInteger('UpdateExeVolatile') <> 0 then
Result:= True;
end;
finally
R.CloseKey;
end;
end;
if not RestartOnly then begin
//These next 2 checks are not necessary for a SQL Server installation.
if not Result then begin
if R.OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager', False) then begin
try
Result:= R.ValueExists('PendingFileRenameOperations');
finally
R.CloseKey;
end;
end;
end;
if not Result then begin
if R.OpenKey('SYSTEM\CurrentControlSet\Control\Session Manager', False) then begin
try
Result:= R.ValueExists('PendingFileRenameOperations2');
finally
R.CloseKey;
end;
end;
end;
end;
if not Result then begin
Result:= R.KeyExists('SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired');
end;
if not Result then begin
if R.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending', False) then begin
try
L.Clear;
R.GetKeyNames(L);
for X := 0 to L.Count-1 do begin
if IsValidGuid(L[X]) then begin
Result:= True;
Break;
end;
end;
finally
R.CloseKey;
end;
end;
end;
if not Result then begin
Result:= R.KeyExists('SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting');
end;
if not Result then begin
if R.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce', False) then begin
try
Result:= R.ValueExists('DVDRebootSignal');
finally
R.CloseKey;
end;
end;
end;
if not Result then begin
Result:= R.KeyExists('Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending');
end;
if not Result then begin
Result:= R.KeyExists('Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootInProgress');
end;
if not Result then begin
Result:= R.KeyExists('Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending');
end;
if not Result then begin
Result:= R.KeyExists('SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts');
end;
if not Result then begin
if R.OpenKey('SYSTEM\CurrentControlSet\Services\Netlogon', False) then begin
try
Result:= R.ValueExists('JoinDomain');
finally
R.CloseKey;
end;
end;
end;
if not Result then begin
if R.OpenKey('SYSTEM\CurrentControlSet\Services\Netlogon', False) then begin
try
Result:= R.ValueExists('AvoidSpnSet');
finally
R.CloseKey;
end;
end;
end;
if not Result then begin
if R.OpenKey('SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName', False) then begin
try
T:= R.ReadString('ComputerName');
finally
R.CloseKey;
end;
end;
if R.OpenKey('SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName', False) then begin
try
if R.ReadString('ComputerName') <> T then
Result:= True;
finally
R.CloseKey;
end;
end;
end;
finally
L.Free;
end;
finally
R.Free;
end;
end;

Refresh shell associations in Inno Setup conditionally (only if a function is checked)

As the title says how can I make that Inno Setup use the:
[Setup]
ChangesAssociations=yes
Only when a certain function is checked:
function installation: Boolean;
begin
Result := install.Checked; { only if this is checked }
end;
function portable: Boolean;
begin
Result := porta.Checked;
end;
I need that association doesnt get called when I simply extract the portable version of my software.
Fyi, in the next version you will be able to write:
[Setup]
ChangesAssociations=installation
[Code]
function installation: Boolean;
begin
Result := install.Checked; { only if this is checked }
end;
Thanks for the idea :)
Instead of using ChangesAssociations directive, call SHChangeNotify WinAPI function conditionally from CurStepChanged(ssPostInstall):
[Code]
const
SHCNE_ASSOCCHANGED = $08000000;
SHCNF_IDLIST = $00000000;
procedure SHChangeNotify(wEventID: Integer; uFlags: Cardinal; dwItem1, dwItem2: Cardinal);
external 'SHChangeNotify#shell32.dll stdcall';
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
if installation then
begin
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
end;
end;
end;
This is what ChangesAssociations=yes internally does.
Partially based on: Inno Setup refresh desktop.

FTP file download using Delphi IdFTP

I am new to delphi, I need to write an ftp client program that will go through a text file with list of ftp addresses, and download the subfolders from an ftp site .
I have successfully hooked to the server but got stuck on the download part. can someone please help me with the codes to insert in the download procedure
procedure TCleint.btnConnectClick(Sender: TObject);
begin
try
if not IdFTP.Connected then
begin
IdFTP.Host := 'ftp server';
IdFTP.Username := 'anonymous';
IdFTP.Password := 'emailaddress';
IdFTP.Port := 21;
IdFTP.Connect;
IdFTP.List(listaDirectory.Items, '', false);
btnConnect.Enabled := False;
btnDisconnect.Enabled := True;
btnDownload.Enabled := True;
end;
except
on E:Exception do
begin
MessageDlg('connection error!', mtError, [mbOK], 0);
btnConnect.Enabled := true;
btnDisconnect.Enabled := false;
btnDownload.Enabled := false;
end;
end;
end;
procedure TCleint.btnDisconnectClick(Sender: TObject);
begin
try
if IdFTP.Connected then
begin
IdFTP.Disconnect;
listaDirectory.Clear;
btnConnect.Enabled := True;
btnDisconnect.Enabled := False;
btnDownload.Enabled := False;
end;
except
on E:Exception do
begin
MessageDlg('connection error!', mtError, [mbOK], 0);
btnConnect.Enabled := false;
btnDisconnect.Enabled := true;
btnDownload.Enabled := true;
end;
end;
end;
procedure TCleint.btnDownloadClick(Sender: TObject);
begin
end;
end.
After calling List(), you need to loop through the entries of the DirectoryListing property. That will tell you which items are files and which are subfolders. You can then Get() the files and (recursively) ChangeDir()/List() the subfolders.

InnoSetup making {app} dir for install.log

I need to create an install.log of the selected components in the install destination folder ({app}) but I'm getting in issue when i run that installer that says "File does not exist C:/tmp/exe/install.log" I'm assuming that means it has not created the dir "exe" yet. How can i circumvent this?
procedure CurStepChanged(CurStep: TSetupStep);
var
I: Integer;
LogList: TStringList;
begin
if CurStep = ssInstall then
begin
LogList := TStringList.Create;
try
LogList.Add('Selected components:');
for I := 0 to WizardForm.ComponentsList.Items.Count - 1 do
if WizardForm.ComponentsList.Checked[I] then
LogList.Add('Component: ' + WizardForm.ComponentsList.ItemCaption[I]);
LogList.SaveToFile(ExpandConstant('{app}\install.log'));
finally
LogList.Free;
end;
end;
end;
I suspect you're trying to access the folder too early in the process, before it's actually been created yet.
Try changing to a later step in the process, such as ssPostInstall. At that point, you'll know for certain that the folder has been created. The rest of your code should be able to stay the same.
procedure CurStepChanged(CurStep: TSetupStep);
var
I: Integer;
LogList: TStringList;
begin
if CurStep = ssPostInstall then
begin
LogList := TStringList.Create;
try
LogList.Add('Selected components:');
for I := 0 to WizardForm.ComponentsList.Items.Count - 1 do
if WizardForm.ComponentsList.Checked[I] then
LogList.Add('Component: ' + WizardForm.ComponentsList.ItemCaption[I]);
LogList.SaveToFile(ExpandConstant('{app}\install.log'));
finally
LogList.Free;
end;
end;
end;

Inno Setup TInputDirWizardPage to not allow UNC path

I have a TInputDirWizardPage to allow the user to pick a backup directory. It automatically allows and error checks the entered paths including UNC paths. The backup directory cannot be a UNC path. How can I make it not allow and error check UNC paths?
procedure InitializeWizard();
begin
BackupInfoPage := CreateInputDirPage(100, 'caption', 'desc', 'sub caption', False,'Backup');
BackupInfoPage.Add('Backup Location:');
with BackupInfoPage do
begin
OnNextButtonClick := #BackupInfoForm_NextButtonClick;
end;
end;
function BackupInfoForm_NextButtonClick(Page: TWizardPage): Boolean;
begin
if not DirExists(BackupInfoPage.Values[0]) then
begin
ForceDirectories(BackupInfoPage.Values[0]);
end;
end;
I did this, but it didn't seem to do anything.
[Setup]
AllowUNCPath=false
So I added a check on the NextButtonClick to keep them on the enter path page until they enter a valid, non-unc path.
if (Copy(BackupLocale, 1, 2) = '\\') then
begin
MsgBox('UNC paths are not allowed.', mbError, MB_OK);
Result := False;
end;

Resources