Skip to content

Commit

Permalink
Handle 32bit to 64bit updates in Windows installer
Browse files Browse the repository at this point in the history
Inno Setup uses separate AppIDs for 32bit and 64bit versions, so we need
to do extra work to transparently perform the update: we need to
uninstall the old version first, then install the new one, instead of
doing in-place update.

This is further complicated by a) async nature of the uninstaller (it
spawns a helper process to remove its own executable, so it is not
sufficient to wait until the process) and b) unfortunate behavior of
Poedit's uninstaller, which removes even HKCU user settings. This was
fixed recently (see 2c1e41c), but it still needs to be handled for
old installs.

Handle both of these complications with a small helper DLL that launches
the uninstaller in a safe manner.
  • Loading branch information
vslavik committed Jan 17, 2024
1 parent e9b3062 commit 8dcb2a6
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ xcuserdata

/packages/
/win32/distrib-*
/win32/uninst-helper.dll

/*.snap
/poedit_source.tar.bz2
Expand Down
2 changes: 2 additions & 0 deletions Poedit.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\Tools.InnoSetup.6.2.2\build\Tools.InnoSetup.props" Condition="Exists('packages\Tools.InnoSetup.6.2.2\build\Tools.InnoSetup.props')" />
<Import Project="packages\Gettext.Tools.0.22.3\build\Gettext.Tools.props" Condition="Exists('packages\Gettext.Tools.0.22.3\build\Gettext.Tools.props')" />
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
Expand Down Expand Up @@ -357,5 +358,6 @@
<Error Condition="!Exists('packages\MSBuildTasks.1.5.0.196\build\MSBuildTasks.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\MSBuildTasks.1.5.0.196\build\MSBuildTasks.targets'))" />
<Error Condition="!Exists('packages\WinSparkle.0.8.0\build\native\WinSparkle.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\WinSparkle.0.8.0\build\native\WinSparkle.targets'))" />
<Error Condition="!Exists('packages\Gettext.Tools.0.22.3\build\Gettext.Tools.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Gettext.Tools.0.22.3\build\Gettext.Tools.props'))" />
<Error Condition="!Exists('packages\Tools.InnoSetup.6.2.2\build\Tools.InnoSetup.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Tools.InnoSetup.6.2.2\build\Tools.InnoSetup.props'))" />
</Target>
</Project>
1 change: 1 addition & 0 deletions packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<package id="Gettext.Tools" version="0.22.3" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="1.0.1661.34" targetFramework="native" />
<package id="MSBuildTasks" version="1.5.0.196" targetFramework="native" developmentDependency="true" />
<package id="Tools.InnoSetup" version="6.2.2" targetFramework="native" />
<package id="WinSparkle" version="0.8.0" targetFramework="native" />
</packages>
73 changes: 73 additions & 0 deletions win32/distrib.proj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
InitialTargets="SetupOutputDir"
DefaultTargets="Distrib">

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Common.props" />

<Import Project="version.props" />

<PropertyGroup>
<MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\..\packages\MSBuildTasks.1.5.0.235\tools</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.targets"/>

<PropertyGroup>
<BinDir>../x64/$(Configuration)</BinDir>
<MsvcToolsX86>"$(VCToolsInstallDir)\bin\HostX64\x86"</MsvcToolsX86>
<MsvcLibpathX86>"$(VCToolsInstallDir)\lib\x86"</MsvcLibpathX86>
<InnoCC>..\packages\Tools.InnoSetup.6.2.2\tools\ISCC.exe</InnoCC>
<InnoFlags>/dCONFIG=$(Configuration)</InnoFlags>
</PropertyGroup>

<ItemGroup>
<PdbFiles Include="$(BinDir)/*.pdb" Exclude="$(BinDir)/vc1*.pdb" />
</ItemGroup>

<Target Name="SetupOutputDir" BeforeTargets="PDBs;Distrib;Installer">
<GitCommits>
<Output TaskParameter="CommitsCount" PropertyName="GitBuildNumber" />
</GitCommits>
<CreateProperty Value="distrib-$(Configuration)-$(PoeditVersion).$(GitBuildNumber)">
<Output TaskParameter="Value" PropertyName="OutputDir"/>
</CreateProperty>
</Target>

<Target Name="Distrib" DependsOnTargets="$(BuildDependsOn);Build;Installer;PDBs">
<Message Importance="High" Text="Copying debug symbols..." />
</Target>

<Target Name="Installer" DependsOnTargets="Build;UninstHelper">
<Message Importance="High" Text="Creating InnoSetup installer..." />
<Exec Command="$(InnoCC) /O$(OutputDir) $(InnoFlags) poedit.iss " />
</Target>

<Target Name="Build">
<MSBuild
Projects="..\Poedit.sln"
Properties="Configuration=$(Configuration)"
/>
</Target>

<Target Name="UninstHelper" Inputs="uninst-helper.c" Outputs="uninst-helper.dll">
<!-- the helper needs to be compiled as 32bit: -->
<PropertyGroup>
<HelperCompileCmd>
call "$(VCINSTALLDIR)\Auxiliary\Build\vcvarsall.bat" x64_x86 || exit /b 666
cl /LD /O1 /MT /W3 uninst-helper.c || exit /b 666
</HelperCompileCmd>
</PropertyGroup>
<WriteLinesToFile File="uninst-helper.bat" Lines="$(HelperCompileCmd)" Overwrite="true" />
<Exec Command="uninst-helper.bat" />
<Delete Files="uninst-helper.bat;uninst-helper.exp;uninst-helper.lib;uninst-helper.obj" />
</Target>

<Target Name="PDBs" DependsOnTargets="Build">
<Message Importance="High" Text="Copying debug symbols..." />
<MakeDir Directories="$(OutputDir)\pdb" />
<Copy SourceFiles="@(PdbFiles)" DestinationFolder="$(OutputDir)\pdb" SkipUnchangedFiles="true" />
</Target>

</Project>
51 changes: 50 additions & 1 deletion win32/poedit.iss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#define VERSION "3.4.2"
#define VERSION_WIN VERSION + "." + Str(POEDIT_GIT_BUILD_NUMBER)

#define APP_ID "{68EB2C37-083A-4303-B5D8-41FA67E50B8F}"

#ifndef CRT_REDIST
#define CRT_REDIST GetEnv("VCToolsRedistDir") + "\x64\Microsoft.VC143.CRT"
#endif
Expand Down Expand Up @@ -70,7 +72,7 @@ ShowLanguageDialog=no
DisableWelcomePage=true
AllowUNCPath=true
InternalCompressLevel=ultra
AppID={{68EB2C37-083A-4303-B5D8-41FA67E50B8F}
AppID={{#APP_ID}
VersionInfoVersion={#VERSION_WIN}
VersionInfoTextVersion={#VERSION}
AppCopyright=Copyright © 1999-2023 Vaclav Slavik
Expand Down Expand Up @@ -99,6 +101,7 @@ VersionInfoProductTextVersion={#VERSION}
DisableDirPage=auto

[Files]
Source: win32\uninst-helper.dll; Flags: dontcopy
Source: {#BINDIR}\Poedit.exe; DestDir: {app}; Flags: ignoreversion
Source: {#BINDIR}\*.dll; DestDir: {app}; Flags: ignoreversion
Source: {#BINDIR}\icudt*.dat; DestDir: {app}
Expand Down Expand Up @@ -209,6 +212,7 @@ Name: "ukrainian"; MessagesFile: "compiler:Languages\Ukrainian.isl"

[CustomMessages]
OpenAfterInstall=Open Poedit after installation
Uninstall32bit=Uninstalling 32-bit version of Poedit...
brazilianportuguese.OpenAfterInstall=Abrir o Poedit após a instalação
catalan.OpenAfterInstall=Obre el Poedit després de la instal·lació
corsican.OpenAfterInstall=Apre Poedit dopu à l'installazione
Expand All @@ -229,3 +233,48 @@ slovenian.OpenAfterInstall=Odpri Poedit po namestitvi
spanish.OpenAfterInstall=Abrir Poedit tras la instalación
turkish.OpenAfterInstall=Kurulumdan sonra Poedit'i aç
ukrainian.OpenAfterInstall=Відкрити Poedit після встановлення

[Code]
const UninstallKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#APP_ID}_is1';
function SafelyUninstall32BitVersion(uninstallerExe: String): Integer;
external 'SafelyUninstall32BitVersion@files:uninst-helper.dll cdecl setuponly';
function Find32bitUninstallerPath(): String;
var
uninst: String;
begin
if (not RegKeyExists(HKCU64, UninstallKey)) and (not RegKeyExists(HKLM64, UninstallKey)) and RegKeyExists(HKLM32, UninstallKey) then
begin
RegQueryStringValue(HKLM32, UninstallKey, 'UninstallString', uninst);
Result := RemoveQuotes(uninst);
end
else
Result := '';
end;
procedure Uninstall32bitVersionIfPresent;
var
uninstaller: String;
begin
uninstaller := Find32bitUninstallerPath();
if (uninstaller <> '') then
begin
Log('Migrating from 32bit install, uninstalling via ' + uninstaller);
WizardForm.StatusLabel.Caption := CustomMessage('Uninstall32bit');
SafelyUninstall32BitVersion(uninstaller);
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if (CurStep = ssInstall) then
begin
Uninstall32bitVersionIfPresent();
end;
end;
93 changes: 93 additions & 0 deletions win32/uninst-helper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* This file is part of Poedit (https://poedit.net)
*
* Copyright (C) 2024 Vaclav Slavik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

#ifndef _M_IX86
#error This file must be compiled for 32bit x86 target
#endif

// This DLL is used by the uninstaller to remove Poedit's 32bit version during upgrades.
//
// Inno Setup treats 64bit and 32bit versions as separate applications, so it can't
// upgrade existing install seamlessly; we need to remove the old version first.
//
// Unfortunately, the installer for older versions of Poedit uninstalled HKCU registry
// keys, i.e. deleted user settings; that wouldn't be good during a simple upgrade.
// Hence this DLL: it is loaded by the uninstaller and it removes the old version,
// while taking care to preserve user settings.

#define UNICODE
#include <windows.h>
#pragma comment(lib, "advapi32.lib")


__declspec(dllexport) int SafelyUninstall32BitVersion(LPCWSTR uninstallerExe)
{
// check if the uninstaller file exists:
if (GetFileAttributesW(uninstallerExe) == INVALID_FILE_ATTRIBUTES)
return 0;

// rename the HKCU\Software\Vaclav Slavik\Poedit key to *.backup:
HKEY key = 0;
if (SUCCEEDED(RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Vaclav Slavik\\Poedit", 0, KEY_ALL_ACCESS, &key)))
{
if (FAILED(RegRenameKey(key, NULL, L"Poedit.backup")))
{
RegCloseKey(key);
key = 0;
}
}

// run the uninstaller:
wchar_t cmdline[10240];
wcscpy_s(cmdline, 10240, uninstallerExe);
wcscat_s(cmdline, 10240, L" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART");

STARTUPINFOW si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessW(uninstallerExe, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

// Inno Setup's uninstaller spawns a subprocess in order to be able to delete itself, so wait until
// the uninstaller is removed (or 60 seconds pass, whichever comes first):
for (int i = 0; i < 600; i++)
{
if (GetFileAttributesW(uninstallerExe) == INVALID_FILE_ATTRIBUTES)
break;
Sleep(100);
}

// rename the backup key back:
if (key)
{
RegRenameKey(key, NULL, L"Poedit");
RegCloseKey(key);
}

return 1;
}

0 comments on commit 8dcb2a6

Please sign in to comment.