inno setup可以在安装时写注册表,在卸载时删除注册表。
重复安装检测可以通过检查注册表是否存在特定的键。
代码示例如下:
function CheckIsInstalled(): Boolean;
var UnInstallExe, strCmdUnInstall, errorStr: String;
var ErrorCode: Integer;
var Subkeys: TArrayOfString;
var Subkey: string;
var I: Integer;
var uninstallKey: String;
begin
//CheckIsInstalledFromRegistry可以遍历注册表查找key
if CheckIsInstalledFromRegistry(uninstallKey) then
begin
if MsgBox('hello already installed in this system, click "ok" to uninstall it and continue install, click "cancel" to exit!', mbConfirmation, MB_OKCANCEL) = IDOK then begin
UnInstallExe := GetRegistryString('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\'+uninstallKey, uninstallKey);
if FileExists(UnInstallExe) then
begin
strCmdUnInstall := Format('/c %s', [UnInstallExe]);
//卸载之前安装的dbAgent
ShellExec('open', ExpandConstant('{cmd}'), strCmdUnInstall, '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
end
else
begin
errorStr := Format('Uninstall program %s not exist,install program will continue install.', [UnInstallExe]);
MsgBox(errorStr, mbError, MB_OK);
Exit;
end;
end
else
begin
//退出安装
MyExitProcess(0, ' "cancel" is clicked, exit');
end;
end;
end;
其中CheckIsInstalledFromRegistry的实现参考https://blog.csdn.net/dongsongz/article/details/116329544
CheckIsInstalledFromRegistry主要根据特定字符串在HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall下搜索匹配的key,找到了则取出其值(值写入的是卸载程序的绝对路径),这样就可以调用卸载程序来卸载已经安装的版本。
安装完成后写注册表通过调用RegWriteStringValue完成,卸载时删除注册表通过调用RegDeleteKeyIncludingSubkeys完成,详细实现见完整脚本。
完整脚本下载:
https://download.csdn.net/download/dongsongz/18299822