-
Notifications
You must be signed in to change notification settings - Fork 6
/
PathMan.iss
166 lines (148 loc) · 5.62 KB
/
PathMan.iss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
; Copyright (C) 2021-2024 by Bill Stewart (bstewart at iname.com)
;
; This program is free software; you can redistribute it and/or modify it under
; the terms of the GNU Lesser General Public License as published by the Free
; Software Foundation; either version 3 of the License, or (at your option) any
; later version.
;
; This program is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
; FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
; details.
;
; You should have received a copy of the GNU Lesser General Public License
; along with this program. If not, see https://www.gnu.org/licenses/.
; Sample Inno Setup (https://www.jrsoftware.org/isinfo.php) script
; demonstrating use of PathMgr.dll.
;
; This script uses PathMgr.dll in the following ways:
; * Copies PathMgr.dll to the target machine (required for uninstall)
; * Defines a task in [Tasks] that should modify the Path
; * Imports the AddDirToPath() DLL function at setup time
; * Imports the RemoveDirFromPath() DLL function at uninstall time
; * Stores task state as custom setting using RegisterPreviousData()
; * Retrieves task state custom setting during setup and uninstall initialize
; * At post install, adds app dir to Path if task selected
; * At uninstall, removes dir from Path if custom setting present
; * Unloads and deletes DLL and removes app dir at uninstall deinitialize
#if Ver < EncodeVer(6,0,0,0)
#error This script requires Inno Setup 6 or later
#endif
[Setup]
AppId={{82425B5C-8B45-4F8C-9830-3D8E2DB64581}
AppName=PathMan
AppVersion=2.0.0.0
UsePreviousAppDir=false
DefaultDirName={autopf}\PathMan
ChangesEnvironment=yes
Uninstallable=true
OutputDir=.
OutputBaseFilename=PathMan-Setup
ArchitecturesInstallIn64BitMode=x64
PrivilegesRequired=none
PrivilegesRequiredOverridesAllowed=dialog
[Files]
; Install PathMgr.dll for use with both setup and uninstall; use
; uninsneveruninstall flag because DeinitializeSetup() will delete after
; unloading the DLL; install the 32-bit version of PathMgr.dll because both
; setup and uninstall executables are 32-bit
Source: "i386\PathMgr.dll"; DestDir: "{app}"; Flags: uninsneveruninstall
; Other files to install on target system
Source: "i386\PathMan.exe"; DestDir: "{app}"; Check: not Is64BitInstallMode()
Source: "x86_64\PathMan.exe"; DestDir: "{app}"; Check: Is64BitInstallMode()
Source: "PathMan.md"; DestDir: "{app}"
[Tasks]
Name: modifypath; Description: "&Add to Path"
[Code]
const
MODIFY_PATH_TASK_NAME = 'modifypath'; // Specify name of task
var
PathIsModified: Boolean; // Cache task selection from previous installs
ApplicationUninstalled: Boolean; // Has application been uninstalled?
// Import AddDirToPath() at setup time ('files:' prefix)
function DLLAddDirToPath(DirName: string; PathType, AddType: DWORD): DWORD;
external 'AddDirToPath@files:PathMgr.dll stdcall setuponly';
// Import RemoveDirFromPath() at uninstall time ('{app}\' prefix)
function DLLRemoveDirFromPath(DirName: string; PathType: DWORD): DWORD;
external 'RemoveDirFromPath@{app}\PathMgr.dll stdcall uninstallonly';
// Wrapper for AddDirToPath() DLL function
function AddDirToPath(const DirName: string): DWORD;
var
PathType, AddType: DWORD;
begin
// PathType = 0 - use system Path
// PathType = 1 - use user Path
// AddType = 0 - add to end of Path
// AddType = 1 - add to beginning of Path
if IsAdminInstallMode() then
PathType := 0
else
PathType := 1;
AddType := 0;
result := DLLAddDirToPath(DirName, PathType, AddType);
end;
// Wrapper for RemoveDirFromPath() DLL function
function RemoveDirFromPath(const DirName: string): DWORD;
var
PathType: DWORD;
begin
// PathType = 0 - use system Path
// PathType = 1 - use user Path
if IsAdminInstallMode() then
PathType := 0
else
PathType := 1;
result := DLLRemoveDirFromPath(DirName, PathType);
end;
procedure RegisterPreviousData(PreviousDataKey: Integer);
begin
// Store previous or current task selection as custom user setting
if PathIsModified or WizardIsTaskSelected(MODIFY_PATH_TASK_NAME) then
SetPreviousData(PreviousDataKey, MODIFY_PATH_TASK_NAME, 'true');
end;
function InitializeSetup(): Boolean;
begin
result := true;
// Was task selected during a previous install?
PathIsModified := GetPreviousData(MODIFY_PATH_TASK_NAME, '') = 'true';
end;
function InitializeUninstall(): Boolean;
begin
result := true;
// Was task selected during a previous install?
PathIsModified := GetPreviousData(MODIFY_PATH_TASK_NAME, '') = 'true';
ApplicationUninstalled := false;
end;
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
// Add app directory to Path at post-install step if task selected
if PathIsModified or WizardIsTaskSelected(MODIFY_PATH_TASK_NAME) then
AddDirToPath(ExpandConstant('{app}'));
end;
end;
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
if CurUninstallStep = usUninstall then
begin
// Remove app directory from path during uninstall if task was selected;
// use variable because we can't use WizardIsTaskSelected() at uninstall
if PathIsModified then
RemoveDirFromPath(ExpandConstant('{app}'));
end
else if CurUninstallStep = usPostUninstall then
begin
ApplicationUninstalled := true;
end;
end;
procedure DeinitializeUninstall();
begin
if ApplicationUninstalled then
begin
// Unload and delete PathMgr.dll and remove app dir when uninstalling
UnloadDLL(ExpandConstant('{app}\PathMgr.dll'));
DeleteFile(ExpandConstant('{app}\PathMgr.dll'));
RemoveDir(ExpandConstant('{app}'));
end;
end;