inno setup

本文介绍了一个用于Windows系统的环境变量修改工具,支持Windows 9x、NT、2000及XP等不同版本的操作系统。该工具能够根据指定的方法和作用范围(当前用户或所有用户)来添加或更新环境变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


[Code]

// Version log:
// 09/22/2005: Initial release (axis23(at)gmail.com)
// Based in 'Native ISX procedures for PATH modification' from Thomas Vedel ('ModifyPath.iss')

const
// Modification method
vmAddOnlyIfVarDoesNotExists = $1; // Add Var only is this Var doesn't exists
vmAddAllways = $2; // Add Var allways

// Scope
vsCurrentUser = 1; // Add Var for current user
vsAllUsers = 2; // Add Var for all users

// Error results
mvOK = 0; // No errors
mvMissingRights = -1; // User has insufficient rights
mvAutoexecNoWriteacc = -2; // Autoexec can not be written (may be readonly)
mvBothMethods = -3; // Error if invoque function with two methods

{ Helper function: Modify Var on Windows 9x }
function ModifyVar9x(VarName, VarValue: string; Method: integer): integer;
var
AutoexecLines: TStringList;
ActualLine: String;
VarLineNos: TStringList;
FirstVarLineNo: Integer;
VarExists: boolean;
LineNo, CharNo, Index: integer;

TempString: String;
TempVarName: String;
TempVarName_comp: String;

begin
// Expect everything to be OK
result := mvOK;

// Create stringslists
AutoexecLines := TStringList.Create;
VarLineNos := TStringList.Create;
TempVarName := 'SET ' + uppercase(VarName) + '=';
TempVarName_comp := 'SET' + uppercase(VarName) + '=';

// Create VarExists
VarExists := false;

// Read existing Var
LoadStringFromFile('c:Autoexec.bat', TempString);
AutoexecLines.Text := TempString;
VarLineNos.Clear;
// Read Autoexec line by line
for LineNo := 0 to AutoexecLines.Count - 1 do begin
ActualLine := AutoexecLines.Strings[LineNo];
// Check if line starts with 'Varname=' after first stripping spaces and other "fill-chars"
if Pos('=', ActualLine) > 0 then
begin
for CharNo := Pos('=', ActualLine)-1 downto 1 do
if (ActualLine[CharNo]=' ') or (ActualLine[CharNo]=#9) then
Delete(ActualLine, CharNo, 1);
if Pos('@', ActualLine) = 1 then
Delete(ActualLine, 1, 1);
if (Pos(TempVarName_Comp, uppercase(ActualLine))=1) then
begin
ActualLine := TempVarName + VarValue;
// Update list of line numbers holding VarName variables
VarLineNos.Add(IntToStr(LineNo));
VarExists :=true;
end;
end;
end;

// Save first line number in Autoexec.bat which modifies Var environment variable
if VarLineNos.Count > 0 then
FirstVarLineNo := StrToInt(VarLineNos.Strings[0])
else
FirstVarLineNo := 0;

// Only change autoexec if method permit this
if ((Method = vmAddAllways) or ((Method = vmAddOnlyIfVarDoesNotExists) and (VarExists=false))) then
begin
// Write Modified Var back to Autoexec.bat
// First delete all existing Var references from Autoexec.bat
Index := VarLineNos.Count-1;
while (Index>=0) do
begin
AutoexecLines.Delete(StrToInt(VarLineNos.Strings[Index]));
Index := Index-1;
end;
// Then insert new Var variable into Autoexec.bat
if(VarExists=false) then
FirstVarLineNo := AutoexecLines.Count;
AutoexecLines.Insert(FirstVarLineNo, TempVarName+VarValue);
// Delete old Autoexec.bat from disk
if not DeleteFile('c:Autoexec.bat') then
result := mvAutoexecNoWriteAcc;
Sleep(500);
// And finally write Autoexec.bat back to disk
if not (result=mvAutoexecNoWriteAcc) then
SaveStringToFile('c:Autoexec.bat', AutoexecLines.Text, false);
end;

// Free stringlists
VarLineNos.Free;
AutoexecLines.Free;
end; // ModifyVar9x


{ Helper function: Modify Var on Windows NT, 2000 and XP }
function ModifyVarNT(VarName, VarValue: string; Method, Scope: integer): integer;
var
RegRootKey: integer;
RegSubKeyName: string;
RegValueName: string;
ResultVar: string;
OK: boolean;
begin
// Expect everything to be OK
result := mvOK;

// Initialize registry key and value names to reflect if changes should be global or local to current user only
case Scope of
vsCurrentUser:
begin
RegRootKey := HKEY_CURRENT_USER;
RegSubKeyName := 'Environment';
RegValueName := VarName;
end;
vsAllUsers:
begin
RegRootKey := HKEY_LOCAL_MACHINE;
RegSubKeyName := 'SYSTEMCurrentControlSetControlSession ManagerEnvironment';
RegValueName := VarName;
end;
end;

// Read current Var value from registry
OK := RegQueryStringValue(RegRootKey, RegSubKeyName, RegValueName, ResultVar);

// Write new Var value to registry
if ((Method = vmAddAllways) or ((Method = vmAddOnlyIfVarDoesNotExists) and (OK=false))) then
begin
if not RegWriteStringValue(RegRootKey, RegSubKeyName, RegValueName, VarValue) then
begin
result := mvMissingRights;
Exit;
end;
end;
end; // ModifyVarNT


{ Main function: Modify Var }
function ModifyVar(VarName, VarValue: string; Method, Scope: integer): integer;
begin
// Check if both add and remove has been specified (= error!)
if (Method and (vmAddOnlyIfVarDoesNotExists and vmAddAllways)) > 0 then
begin
result := mvBothMethods;
Exit;
end;

// Test if Win9x
if InstallOnThisVersion('4,0','0,0') = irInstall then
ModifyVar9x(VarName,VarValue, Method);

// Test if WinNT, 2000 or XP
if InstallOnThisVersion('0,4','0,0') = irInstall then
ModifyVarNT(VarName, VarValue, Method, Scope);
end; // ModifyVar

### Inno Setup 安装程序创建工具概述 Inno Setup 是一个广泛使用的 Windows 安装包制作工具,以其轻量级和丰富的功能受到个人开发者和企业的青睐。它支持脚本自定义、数字签名、权限控制、多语言、任务选项等功能,适用于多种软件打包场景[^2]。 ### Inno Setup 的基本使用步骤 创建安装包通常包含以下基本步骤: 1. **下载并安装 Inno Setup**:从官方网站获取安装包,按照指引完成安装。 2. **使用内置的脚本编辑器创建安装脚本**:脚本中定义安装程序的各个方面,如应用程序文件位置、安装目录、组件选择、快捷方式设置以及注册表修改等。 3. **配置安装程序的界面和行为**:调整安装向导页面的外观、文本和按钮,以满足特定需求。 4. **通过内置编译器编译脚本**:将编写好的脚本编译成可执行的安装包。 5. **测试安装包**:确保生成的安装包能够按照预期工作,没有错误或遗漏。 ### 脚本语言简介 Inno Setup 使用一种脚本语言来定义安装过程中的各种参数和行为。这种语言允许开发者高度定制安装程序的行为,包括但不限于文件的复制、注册表项的创建、安装路径的选择等。通过脚本语言,用户可以实现复杂的安装逻辑,如条件判断、循环等[^1]。 ### 中文语言支持 对于中文用户,可以使用博主“阿松爱睡觉”提供的中文语言包下载地址:http://files.cnblogs.com/files/shiningrise/InnoSetup%E6%B1%89%E5%8C%96%E8%AF%AD%E8%A8%80%E5%8C%85.zip,以便更好地理解和使用 Inno Setup 的各项功能[^3]。 ### 示例脚本 以下是一个简单的 Inno Setup 脚本示例,用于演示如何创建一个基本的应用程序安装包: ```pascal [Setup] AppName=My Application AppVersion=1.0 DefaultDirName={pf}\My Application DefaultGroupName=My Application OutputBaseFilename=setup [Files] Source: "C:\MyApp\*"; DestDir: "{app}"; Flags: ignoreversion [Icons] Name: "{group}\My Application"; Filename: "{app}\MyApp.exe" ``` 此脚本定义了一个名为 "My Application" 的应用程序安装包,版本为 1.0,安装路径默认为 Program Files 文件夹下的 "My Application" 子目录。安装完成后,会在开始菜单创建一个指向应用程序的快捷方式。 ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值