11#define AppDll " CalibreImport.dll"
22#define ConfigFile " CalibreImport.config"
33#define ReleaseFilesPath " ..\ReleaseFiles\"
4+ #define AppName " CalibreImport"
45
56[Setup]
6- AppName = YourAppName
7+ AppName = {#AppName}
78AppVersion = 1.0
8- DefaultDirName = {pf} \CalibreImport
9- DefaultGroupName = Calibre Import Shell Extension
10- UninstallDisplayIcon = {app} \YourApp.exe
9+ DefaultDirName = {pf} \{#AppName}
10+ DefaultGroupName = {#AppName}
11+ UninstallDisplayIcon = {app} \{#AppDll}
1112OutputDir = {#ReleaseFilesPath}
1213OutputBaseFilename = CalibreImportSetup
1314Compression = lzma
1415SolidCompression = yes
16+ PrivilegesRequired = admin
1517
1618[Files]
17- Source : " {#ReleaseFilesPath}{#AppDll}" ; DestDir : " {app} " ; Flags : ignoreversion
18- Source : " {#ReleaseFilesPath}{#ConfigFile}" ; DestDir : " {userappdata}\YourAppName" ; Flags : ignoreversion
19-
20- [Icons]
21- Name : " {group} \YourAppName" ; Filename : " {app} \YourApp.exe"
19+ ; These will only be used in non-portable mode
20+ Source : " {#ReleaseFilesPath}{#AppDll}" ; DestDir : " {app} " ; Flags : ignoreversion ; Check : ShouldInstallToProgramFiles
21+ Source : " {#ReleaseFilesPath}{#ConfigFile}" ; DestDir : " {userappdata}\{#AppName}" ; Flags : ignoreversion ; Check : ShouldInstallToAppData
2222
2323[Code]
2424const
25- AppDll = ' {#AppDll}' ; // Use preprocessor variable
26- ConfigFile = ' {#ConfigFile}' ; // Use preprocessor variable
27- ReleaseFilesPath = ' {#ReleaseFilesPath}' ; // Use preprocessor variable
25+ AppDll = ' {#AppDll}' ;
26+ ConfigFile = ' {#ConfigFile}' ;
27+ ReleaseFilesPath = ' {#ReleaseFilesPath}' ;
28+ AppName = ' {#AppName}' ;
2829
2930var
3031 PortableMode: Boolean;
4748 end ;
4849end ;
4950
51+ function ShouldInstallToProgramFiles : Boolean;
52+ begin
53+ Result := not PortableMode;
54+ end ;
55+
56+ function ShouldInstallToAppData : Boolean;
57+ begin
58+ Result := not PortableMode;
59+ end ;
60+
61+ procedure UpdatePortableMode (Sender: TObject);
62+ begin
63+ PortableMode := PortableModeCheck.Checked;
64+ end ;
65+
5066procedure InitializeWizard ;
5167begin
5268 { Check for .NET Framework 4.8 }
@@ -59,18 +75,17 @@ begin
5975 { Initialize variables }
6076 PortableMode := False;
6177 RestartExplorer := False;
62-
63- { Initialize InstallPath with a default directory }
64- InstallPath := ExpandConstant(' {sd}' ); // Default to the system drive (e.g., C:\)
78+ InstallPath := ExpandConstant(' {sd}' );
6579
6680 { Create a checkbox for portable mode }
6781 PortableModeCheck := TNewCheckBox.Create(WizardForm);
6882 PortableModeCheck.Parent := WizardForm.SelectDirPage;
69- PortableModeCheck.Left := WizardForm.DirEdit.Left; // Use DirEdit instead of SelectDirLabel
83+ PortableModeCheck.Left := WizardForm.DirEdit.Left;
7084 PortableModeCheck.Top := WizardForm.DirEdit.Top + WizardForm.DirEdit.Height + 10 ;
71- PortableModeCheck.Width := WizardForm.DirEdit.Width; // Use DirEdit instead of SelectDirLabel
85+ PortableModeCheck.Width := WizardForm.DirEdit.Width;
7286 PortableModeCheck.Caption := ' Install in portable mode' ;
7387 PortableModeCheck.Checked := PortableMode;
88+ PortableModeCheck.OnClick := @UpdatePortableMode;
7489
7590 { Create a checkbox for restarting Explorer }
7691 RestartExplorerCheck := TNewCheckBox.Create(WizardForm);
@@ -85,34 +100,60 @@ end;
85100procedure CurStepChanged (CurStep: TSetupStep);
86101var
87102 SourceAppDll, SourceConfigFile: String;
88- ResultCode: Integer; // Declare ResultCode variable
103+ ResultCode: Integer;
104+ RegAsmPath: String;
89105begin
90106 if CurStep = ssPostInstall then
91107 begin
108+ { Update checkbox states }
109+ PortableMode := PortableModeCheck.Checked;
110+ RestartExplorer := RestartExplorerCheck.Checked;
111+
92112 { Resolve the full paths to the source files }
93113 SourceAppDll := ExpandConstant(' {src}\' + ReleaseFilesPath + AppDll);
94114 SourceConfigFile := ExpandConstant(' {src}\' + ReleaseFilesPath + ConfigFile);
95115
96116 if PortableMode then
97117 begin
118+ { Create directory if it doesn't exist }
119+ if not DirExists(InstallPath) then
120+ ForceDirectories(InstallPath);
121+
98122 { Copy files to the selected folder }
99- FileCopy(SourceAppDll, InstallPath + ' \' + AppDll, False);
100- FileCopy(SourceConfigFile, InstallPath + ' \' + ConfigFile, False);
123+ if not FileCopy(SourceAppDll, InstallPath + ' \' + AppDll, False) then
124+ MsgBox(' Failed to copy DLL to portable location' , mbError, MB_OK);
125+ if not FileCopy(SourceConfigFile, InstallPath + ' \' + ConfigFile, False) then
126+ MsgBox(' Failed to copy config to portable location' , mbError, MB_OK);
127+
128+ { Register DLL from portable location }
129+ RegAsmPath := ExpandConstant(' {win}\Microsoft.NET\Framework\v4.0.30319\regasm.exe' );
130+ if not FileExists(RegAsmPath) then
131+ RegAsmPath := ExpandConstant(' {win}\Microsoft.NET\Framework64\v4.0.30319\regasm.exe' );
132+
133+ if not Exec(RegAsmPath, ' "' + InstallPath + ' \' + AppDll + ' " /codebase' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) then
134+ MsgBox(' Failed to register DLL: ' + SysErrorMessage(ResultCode), mbError, MB_OK)
135+ else if ResultCode <> 0 then
136+ MsgBox(' DLL registration failed with code: ' + IntToStr(ResultCode), mbError, MB_OK);
101137 end
102138 else
103139 begin
104- { Copy files to Program Files and AppData }
105- FileCopy(SourceAppDll, ExpandConstant(' {pf}\YourAppName\' + AppDll), False);
106- FileCopy(SourceConfigFile, ExpandConstant(' {userappdata}\YourAppName\' + ConfigFile), False);
140+ { For non-portable mode, files are already installed by the [Files] section }
141+ { Register DLL from installation directory }
142+ RegAsmPath := ExpandConstant(' {win}\Microsoft.NET\Framework\v4.0.30319\regasm.exe' );
143+ if not FileExists(RegAsmPath) then
144+ RegAsmPath := ExpandConstant(' {win}\Microsoft.NET\Framework64\v4.0.30319\regasm.exe' );
145+
146+ if not Exec(RegAsmPath, ExpandConstant(' "{app}\' + AppDll + ' " /codebase' ), ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) then
147+ MsgBox(' Failed to register DLL: ' + SysErrorMessage(ResultCode), mbError, MB_OK)
148+ else if ResultCode <> 0 then
149+ MsgBox(' DLL registration failed with code: ' + IntToStr(ResultCode), mbError, MB_OK);
107150 end ;
108151
109- { Register the DLL using regasm }
110- Exec(ExpandConstant(' {win}\Microsoft.NET\Framework\v4.0.30319\regasm.exe' ), ExpandConstant(' "{app}\' + AppDll + ' " /codebase' ), ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode);
111-
112152 if RestartExplorer then
113153 begin
114154 { Restart Windows Explorer }
115- Exec(' taskkill.exe' , ' /f /im explorer.exe' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode);
155+ if not Exec(' taskkill.exe' , ' /f /im explorer.exe' , ' ' , SW_HIDE, ewWaitUntilTerminated, ResultCode) then
156+ MsgBox(' Failed to kill explorer: ' + SysErrorMessage(ResultCode), mbError, MB_OK);
116157 Exec(' explorer.exe' , ' ' , ' ' , SW_HIDE, ewNoWait, ResultCode);
117158 end ;
118159 end ;
0 commit comments