Silent Setup exit codes required by Microsoft Store: Disk Space full - inno-setup

Microsoft Store seems to require nowadays a quite few EXE RETURN (exit) codes during Silent setup that are not available by default. How to e.g. return an exit code when the DISK is FULL? I can see its possible to return error code when reboot is required with /RESTARTEXITCODE=exit but it would be required to have exit codes also in these cases:
Application already exists
Installation already in progress
Disk space is full
More exe return codes listed here:
https://learn.microsoft.com/en-us/windows/uwp/publish/msiexe/provide-package-details
I can see the current Inno Setup exit codes here:
https://jrsoftware.org/ishelp/index.php?topic=setupexitcodes

Imo, you cannot really implement this cleanly in Inno Setup. You would have to re-implement the disk space check and forcefully abort the installation with the required custom exit code.
I recommend you add a "Store installation" command-line switch to your installer to enable all Store-related hacks you will likely need to implement.
Something like this:
[Code]
function IsStoreInstallation: Boolean;
begin
Result := WizardSilent() and CmdLineParamExists('/StoreInstallation');
end;
procedure StoreInstallationExit(Code: Integer);
begin
Log(Format('Aborting store installation with code %d', [Code]));
ExitProcess(Code);
end;
function PrepareToInstall(var NeedsRestart: Boolean): String;
var
Free, Total: Int64;
AbortCode: Integer;
begin
Log('prepare');
if IsStoreInstallation() then
begin
Log('Store installation, checking for available disk space');
AbortCode := 0;
if not GetSpaceOnDisk64(WizardDirValue, Free, Total) then
begin
Log('Failed to check for available disk space, aborting');
AbortCode := 15;
end
else
if Free < Int64(10) * 1024 * 1024 then
begin
Log(Format('Too low available disk space (%s), aborting', [
IntToStr(Free)]));
AbortCode := 15;
end
else
begin
Log(Format('Enough available disk space (%s)', [IntToStr(Free)]));
end;
if AbortCode <> 0 then
begin
StoreInstallationExit(AbortCode);
end;
end;
end;
For CmdLineParamExists, see Is it possible to accept custom command line parameters with Inno Setup
For ExitProcess, see Exit from Inno Setup installation from [Code]

Related

What does it mean that message boxes are being suppressed in Inno Setup?

Here's a page from documentation: SuppressibleMsgBox.
What does it mean If message boxes are being suppressed...?
There's a link to the explanation right after the part you have quoted:
If message boxes are being suppressed (see Setup Command Line Parameters), Default is returned.
In the link, there's /SUPPRESSMSGBOXES commandline parameter documented:
Instructs Setup to suppress message boxes. Only has an effect when combined with '/SILENT' or '/VERYSILENT'.
So normally, the SuppressibleMsgBox behaves as the MsgBox. But if you run the installer with the /SUPPRESSMSGBOXES parameter, the SuppressibleMsgBox does nothing, only silently returns the value of the Default parameter.
A practical example of use of the function:
function NextButtonClick(CurPageID: Integer): Boolean;
var
Dir: string;
Msg: string;
begin
Result := True;
if CurPageID = wpSelectDir then
begin
Dir := WizardForm.DirEdit.Text;
if Pos(' ', Dir) > 0 then
begin
Msg :=
'It is not recommended to install the application to a path with spaces. '
+ 'Do you want to continue anyway?';
if SuppressibleMsgBox(Msg, mbInformation, MB_YESNO, IDYES) = IDNO then
begin
Result := False;
end;
end;
end;
end;
In an interactive installation, the installer will warn, if the user tries to install to a path with spaces. But if you are automating a silent installation with /SILENT /SUPPRESSMSGBOXES, the installer will proceed.
It's good idea to use SuppressibleMsgBox whenever you do not want that particular message to break silent installation. So for most cases.

Inno Setup Sometimes Fails to Change File During Installation

During an Inno Setup install I am adding files to a zip archive, see here: Inno Setup Copy Files and Folders to an Existing Zip File. I immediately after adding the files, the archive is renamed by changing the file extension from .zip to .doc.
Code used to rename is:
RenameFile(ExpandConstant('{app}\MyFile.zip'), expandconstant('{app}\MyFile.doc'))
While this used to work well under windows 7 and 8, it has become less reliable and only sometimes works under windows 10.
Note, things I have tried include:
adding sleep(###); intervals but this does not work...
copying the file with a different name as per the accepted answer: Is it possible to move existing directories/files with an INNO script?
Looking for suggestions to make a robust solution and or debugging tips.
[Edit: added the codes... have renamed some bits to make it easier to read]
function SetFileAttributes(lpFileName : String; dwAttribs : LongInt) : Boolean;
external 'SetFileAttributesA#kernel32.dll stdcall';
procedure RepackZip();
var
ResultCode, i: Integer;
x1, x2: string;
begin
// Find files
x1 := FindFile('xmlns="sl:SLA"');
x2 := FindFile('xmlns="sl:SLB"');
log(ExpandConstant('{app}'));
// 2. Copy files to archive
SetFileAttributes ((expandconstant('{app}\MyFile.zip')), 0);
if not FileCopy(ExpandConstant('{tmp}\SLA.xml'), ExpandConstant('{app}\Temp\customXml\') + x1, False) then
MsgBox(x1 + 'failed!', mbError, MB_OK);
if not FileCopy(ExpandConstant('{tmp}\SLB.xml'), ExpandConstant('{app}\Temp\customXml\') + x2, False) then
MsgBox(x2 + 'failed!', mbError, MB_OK);
CopyToArchive();
SetFileAttributes ((expandconstant('{app}\MyFile.zip')), 0);
sleep(100);
// HAVE TRIED COPY & RENAME
// Everything works up to here and both FileCopy and FileRename fail on the same computers (permissions?)
// Have told Inno to Require Admin, makes no difference.
//RenameFile(ExpandConstant('{app}\MyFile.zip'), expandconstant('{app}\MyFile.dotm'))
FileCopy(ExpandConstant('{app}\MyFile.zip'), ExpandConstant('{app}\MyFile.doc'), false);
For i := 0 to 5 do
begin
if not FileExists(ExpandConstant('{app}\MyFile.doc')) then
begin
sleep (250);
end
else begin
// SetFileAttributes ((expandconstant('{app}\MyFile.doc')), 1);
exit;
end;
end;
if not FileExists(expandconstant('{app}\MyFile.doc')) then
MsgBox('Failed - rename archive to .doc', mbError, MB_OK);
end;
And CopyToArchive (this works - but I was wondering if CopyToArchive might somehow be holding the archive open and preventing the rename):
procedure CopyToArchive(); //(const Archive, Content: string);
var
Shell: Variant;
Folder: Variant;
Archive, Content: string;
objFSO, h: Variant;
max0, max1: integer;
begin
Shell := CreateOleObject('Shell.Application');
Archive := ExpandConstant('{app}') + '\MyFile.zip';
Folder := Shell.NameSpace(Archive);
log('Archive Location: ' + Archive);
objFSO := CreateOleObject('Scripting.FileSystemObject');
h := objFSO.getFile(Archive);
Content := ExpandConstant('{app}\Temp\customXml\');
Folder.CopyHere(Content, $0100);
sleep(2000);
end;
One thing that I started looking into was to use objFSO to rename the Archive, but I was unable to figure it out...
There are two problems:
.CopyHere call is asynchronous. After you call it, you have to wait for the archiving to complete. While Sleep is not really realiable, it should do. .CopyHere actually does not lock the file, so it won't prevent the rename, but you may end up renaming an incomplete file.
What causes the rename to fail is your call to objFSO.getFile(Archive), which locks the file and you never unlock it. And you actually never use h. So remove that call.
Why don't you rename the file before archiving? It would prevent all these problems.

Specify the registry uninstall key location/hive via [Code]

Inno Setup by default looks at the PrivilegesRequired setup variable, if this is set to admin or poweruser, the installer installs the uninstall registry key to HKLM. If this is set to lowest, then it will install the registry entries to HKCU.
I have a requirement to provide the user an option to install for "just me" or "everybody", and have done so by replacing the dir selection page with a radio selection of these two options. what I need to do now is also modify the registry install location based on this setting. If I install the app into the local user app data, it won't make sense to register the uninstall data at the HKLM level, because then other users will see it in the programs list and still be unable to uninstall or use it.
Edit: After looking through the documentation and the source of Install.pas, I found the CreateUninstallRegKey setup directive, which will disable Inno from installing registry keys at all, after which I can add my own registry entries, but is this really the only way?
Edit #2 (marked as duplicate): I've already taken a look at this Conditional Elevation question (and actually implemented it), and it's not the same as mine. The current elevation state does not alter where Inno Setup actually saves the uninstall registry info (in HKCU or HKLM). If you look at the Inno source code (Install.pas #507) you'll see that the PrivilegesRequired directive is the primary factor in where the registry is stored. If this is set to lowest, it doesnt matter if the installer is elevated or not - it will install the registry keys to HKCU, when the desired behavior is to select one or the other based on the users install preference, NOT the current elevation state. So all this being said, I'm looking for a solution to alter the registry root based on a code variable, regardless of current PrivilegesRequired or Elevation setting.
Inno Setup 6 has a built-in support for selecting between "Install for all users" and "Install for me only".
Basically, you can simply set PrivilegesRequiredOverridesAllowed:
[Setup]
PrivilegesRequiredOverridesAllowed=commandline dialog
For Inno Setup 5: As you found yourself, the logic is hard-coded. You cannot really control that.
The closest you can get is by using the undocumented (deprecated) PrivilegesRequired=none.
With this value (and with a help of installer-autodetection in Windows):
When you start the installer with an un-privileged account, it starts without prompting you for elevation. If you decide you need to elevate during the installation, you can restart the installer elevated.
When you start the installer with a privileged account, it always prompts you for elevation and won't start, if you reject that. So the installer always runs elevated. Again, you would have to restart the installer, if you decide you to proceed un-elevated. See How to Start a Process Unelevated or maybe Run un-elevated command from an elevated prompt?.
It's not exactly, what you want, but I do not think you can get any closer.
You can of course copy (move) the registry key between the HKCU and HKLM yourself by a code:
function MoveHKCUUninstallKeyToHKLM: Boolean;
var
UninstallKey: string;
AppId: string;
I: Integer;
ValueNames: TArrayOfString;
ValueName: string;
ValueStr: string;
ValueDWord: Cardinal;
begin
if '{#emit SetupSetting("AppId")}' <> '' then
begin
AppId := '{#emit SetupSetting("AppId")}';
end
else
begin
AppId := '{#emit SetupSetting("AppName")}';
end;
Result := False;
if AppId = '' then
begin
Log('Cannot identify AppId');
end
else
begin
UninstallKey :=
'Software\Microsoft\Windows\CurrentVersion\Uninstall\' + AppId + '_is1';
Log(Format(
'AppId identified as "%s", using uninstall key "%s"', [AppId, UninstallKey]));
if not RegKeyExists(HKCU, UninstallKey) then
begin
Log('HKCU uninstall key not found');
end
else
if RegKeyExists(HKLM, UninstallKey) then
begin
Log('HKLM uninstall key exists already');
end
else
begin
Log('HKCU uninstall key found and HKLM key not exists yet');
if not RegGetValueNames(HKCU, UninstallKey, ValueNames) then
begin
Log('Cannot list uninstall key values');
end
else
begin
I := 0;
Result := True;
while (I < GetArrayLength(ValueNames)) and Result do
begin
ValueName := ValueNames[I];
if RegQueryStringValue(HKCU, UninstallKey, ValueName, ValueStr) then
begin
if not RegWriteStringValue(HKLM, UninstallKey, ValueName, ValueStr) then
begin
Log(Format('Error moving "%s" string value', [ValueName]));
Result := False;
end
else
begin
Log(Format('Moved "%s" string value', [ValueName]));
end;
end
else
if RegQueryDWordValue(HKCU, UninstallKey, ValueName, ValueDWord) then
begin
if not RegWriteDWordValue(HKLM, UninstallKey, ValueName, ValueDWord) then
begin
Log(Format('Error moving "%s" dword value', [ValueName]));
Result := False;
end
else
begin
Log(Format('Moved "%s" dword value', [ValueName]));
end;
end
else
begin
{ All uninstall values written by Inno Setup are either string or dword }
Log(Format('Value "%s" is neither string nor dword', [ValueName]));
Result := False;
end;
Inc(I);
end;
if Result then
begin
if not RegDeleteKeyIncludingSubkeys(HKCU, UninstallKey) then
begin
Log('Error removing HKCU uninstall key');
Result := False;
end
else
begin
Log('Removed HKCU uninstall key');
end;
end;
if not Result then
begin
if not RegDeleteKeyIncludingSubkeys(HKCU, UninstallKey) then
begin
Log('Failed to move uninstall key to HKLM, ' +
'and also failed to rollback the changes');
end
else
begin
Log('Failed to move uninstall key to HKLM, rolled back the changes');
end;
end;
end;
end;
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
Log('Post install');
MoveHKCUUninstallKeyToHKLM;
end;
end;
The PrivilegesRequired=none solution was not what I wanted. In some cases, it still prompts for elevation on administrator accounts and also the registry destination was still not reflective of the users selection.
Since I was already using a native helper DLL in my Inno Setup project, I coded this in C++ as I'm more comfortable there. I'm calling this method is called in CurStepChanged where CurPage=ssDoneInstall. Just call this method with the [Setup] AppId and whether or not the registry keys should be installed locally or not.
#include <shlwapi.h>
extern "C" __declspec(dllexport)
bool DetectAndMoveRegKeyW(LPCWSTR app_id, bool install_local)
{
std::wstring s_app = app_id;
std::wstring path =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + s_app + L"_is1";
LPCWSTR c_path = path.c_str();
LRESULT res;
HKEY source = nullptr, subKey = nullptr;
// try to find source in HKLM
source = HKEY_LOCAL_MACHINE;
res = RegOpenKeyExW(source, c_path, 0, KEY_READ, &subKey);
if (subKey != nullptr)
RegCloseKey(subKey);
// try to find source in HKCU
if (res != ERROR_SUCCESS)
{
subKey = nullptr;
source = HKEY_CURRENT_USER;
res = RegOpenKeyExW(source, c_path, 0, KEY_READ, &subKey);
if (subKey != nullptr)
RegCloseKey(subKey);
}
if (res != ERROR_SUCCESS)
return false; // cant find the registry key
HKEY dest = install_local ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
if (source == dest)
return true; // registry already in the right place
// copy registry key to correct destination
HKEY hOldKey;
HKEY hNewKey;
bool bResult = false;
if (RegOpenKeyW(source, c_path, &hOldKey) == 0)
{
if (RegCreateKeyW(dest, c_path, &hNewKey) == 0)
{
bResult = (SHCopyKeyW(hOldKey, nullptr, hNewKey, 0) == 0);
RegCloseKey(hNewKey);
}
RegCloseKey(hOldKey);
if (bResult)
{
RegDeleteKeyW(source, c_path);
}
}
return bResult;
}
I'm exporting this method as cdecl instead of stdcall, this is because VC++ ignores the C extern and mangles method names anyways when using stdcall. You'll need to import this as cdecl in inno (see inno docs for this). Also, of course this is the Unicode-only implementation, if you require an Ansi version it should be simple enough.
IMPORTANT NOTICE:
This code is incomplete, it doesn't account for 64bit registry redirection. Inno-Setup completely ignores windows registry redirection and this code doesn't search the 64 bit registry at all since Inno and itself are running in 32bit.

Inno Setup Log setup exit code

The Inno Setup log file does not, by default, include the setup exit code. I am looking for a way to include this in the log file. I am assuming this would be done using the Log function and including it in the DeinitializeSetup event. Something like this:
procedure DeinitializeSetup();
begin
Log('Exit code: ' + ExitCode);
end;
What I don't know, and cannot seem to find, is how to return the setup exit code, so that I can use it in the Log function. Is this the best way to do this and how do I return the setup exit code?
There's no way to retrieve the exit code in Pascal Script.
All you can do is to log, if installation was successful or not (what is logged anyway already).
One way to do that is by checking, if the GetCustomSetupExitCode event function was called or not (it's called when exit code would be 0 only).
var
ZeroExitCode: Boolean;
function GetCustomSetupExitCode: Integer;
begin
ZeroExitCode := True;
Result := 0;
end;
procedure DeinitializeSetup();
begin
if ZeroExitCode then
Log('Zero exit code')
else
Log('Non-zero exit code');
end;

How to set automaticly DefaultDirName for previous Inno Setup installation?

my previous installation (A) in Inno Setup has AppID={{8ADA0E54-F327-4717-85A9-9DE3F8A6D100}.
I have another installation (B) with different AppID and I want to install it into the same directory as installation (A).
How do I get automaticly DefaultDirName? I don't want to use the same AppID, because when I uninstall the installation (B) and installation (A) stays installed, it will delete AppID string from registry (installation (A) string).
Can you help me, please?
You'll probably need some code to do what you want. You'll also need a way to find the installation directory of Application A. Here's some code that I've used
[Setup]
DefaultDirName={code:GetDefaultDir}
[Code]
function GetDefaultDir(def: string): string;
var
sTemp : string;
begin
//Set a defualt value so that the install doesn't fail.
sTemp := ExpandConstant('{pf}') + '\MyCompany\MyAppA';
//We need to get the current install directory.
if RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\MyCompany\Products\MyAppNameA',
'InstallDir', sTemp) then
begin
//We found the value in the registry so we'll use that. Otherwise we use the default
end;
Result := sTemp;
end;
I developed the following code to find the installation directory based on AppID. It accommodates per-user registry entries as well as those for the entire machine. It has been tested on Windows 7 Enterprise on a domain and in a Virtual PC XP Professional machine:
[code]
const
PreviousAppID = '8ADA0E54-F327-4717-85A9-9DE3F8A6D100';
AppFolder = 'SomeFolder';
UninstallPath = 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{'
+ PreviousAppID + '}_is1';
// Some posts have 'InstallDir', but I have never observed that
InstallKey = 'InstallLocation';
function GetDefaultDir( Param: String ) : String;
var
UserSIDs: TArrayOfString;
I: Integer;
begin
// Check if the current user installed it
if RegQueryStringValue( HKEY_CURRENT_USER, UninstallPath,
InstallKey, Result ) then
// Current user didn't install it. Did someone else?
else if RegGetSubkeyNames( HKEY_USERS, '', UserSIDs ) then begin
for I := 0 to GetArrayLength( UserSIDs ) - 1 do begin
if RegQueryStringValue( HKEY_USERS, UserSIDs[I] + '\' + UninstallPath,
InstallKey, Result ) then break;
end;
end;
// Not installed per-user
if Result = '' then begin
// What about installed for the machine?
if RegQueryStringValue( HKEY_LOCAL_MACHINE, UninstallPath,
InstallKey, Result ) then
// Doesn't appear to be installed, as admin default to Program Files
else if IsAdminLoggedOn() then begin
Result := ExpandConstant('{pf}\') + AppFolder;
// As non-admin, default to Local Application Data
end else begin
Result := ExpandConstant('{localappdata}\') + AppFolder;
end;
end;
end;

Resources