Skip to content

Commit 7de35d8

Browse files
committed
Workaround for Inno not closing the application when uninstalling
1 parent 2251631 commit 7de35d8

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

AutoAudioSwitcher/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static ServiceProvider ConfigureServices()
6060
[STAThread]
6161
public static void Main()
6262
{
63-
using Mutex singleInstance = new(true, "f09f929b-e98f-a1e9-9fb3-e383aae383b3" /* This is my favorite GUID */, out bool createdNew);
63+
Mutex singleInstance = new(true, "f09f929b-e98f-a1e9-9fb3-e383aae383b3" /* This is my favorite GUID */, out bool createdNew);
6464
if (!createdNew)
6565
{
6666
return;

AutoAudioSwitcher/WindowMessageListener.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public WindowMessageListener(ILogger logger)
1717
{
1818
this.logger = logger.ForContext<WindowMessageListener>();
1919

20-
CreateHandle(new CreateParams());
20+
CreateHandle(new CreateParams()
21+
{
22+
// Used by custom uninstaller code to send WM_CLOSE, since Inno doesn't close applications when uninstalling
23+
Caption = "Auto Audio Switcher"
24+
});
2125
}
2226

2327
/// <inheritdoc cref="WM_DISPLAYCHANGE"/>

Setup/Setup.iss

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ DisableProgramGroupPage=yes
3232
LicenseFile=..\LICENSE.txt
3333
OutputBaseFilename=AutoAudioSwitcher-Setup
3434
PrivilegesRequired=lowest
35+
RestartApplications=no
3536
ShowLanguageDialog=auto
3637
SolidCompression=yes
3738
VersionInfoProductTextVersion={#ProductVersion}
@@ -49,11 +50,53 @@ Source: "..\publish\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs cr
4950
Name: "{userstartup}\Auto Audio Switcher"; Filename: "{app}\AutoAudioSwitcher.exe"
5051

5152
[Run]
52-
Filename: "{app}\AutoAudioSwitcher.exe"; Flags: nowait postinstall
53+
Filename: "{app}\AutoAudioSwitcher.exe"; Description: "{cm:LaunchProgram,Auto Audio Switcher}"; Flags: nowait postinstall
5354

5455
[Code]
56+
const
57+
APP_MUTEX = 'f09f929b-e98f-a1e9-9fb3-e383aae383b3';
58+
WINDOW_NAME = 'Auto Audio Switcher';
59+
APP_EXE = 'AutoAudioSwitcher.exe';
60+
WM_CLOSE = 16;
61+
5562
function InitializeSetup: Boolean;
5663
begin
5764
Dependency_AddDotNet100Desktop;
5865
Result := True;
5966
end;
67+
68+
procedure CloseApplication;
69+
var
70+
Hwnd: HWND;
71+
Timeout: Integer;
72+
ErrorCode: Integer;
73+
begin
74+
if not CheckForMutexes(APP_MUTEX) then
75+
exit;
76+
Hwnd := FindWindowByWindowName(WINDOW_NAME);
77+
if Hwnd <> 0 then
78+
begin
79+
SendMessage(Hwnd, WM_CLOSE, 0, 0);
80+
Timeout := 100; // 10s
81+
while (Timeout > 0) and CheckForMutexes(APP_MUTEX) do
82+
begin
83+
Sleep(100);
84+
Timeout := Timeout - 1;
85+
end;
86+
end;
87+
if CheckForMutexes(APP_MUTEX) then
88+
Exec('taskkill.exe', '/f /im ' + APP_EXE, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
89+
end;
90+
91+
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
92+
var
93+
OriginalCaption: String;
94+
begin
95+
if CurUninstallStep = usUninstall then
96+
begin
97+
OriginalCaption := UninstallProgressForm.StatusLabel.Caption;
98+
UninstallProgressForm.StatusLabel.Caption := SetupMessage(msgStatusClosingApplications);
99+
CloseApplication;
100+
UninstallProgressForm.StatusLabel.Caption := OriginalCaption;
101+
end;
102+
end;

0 commit comments

Comments
 (0)