• Inno Setup打包程序添加hosts解析


    在一些特殊情况下,需要指定某个域名的IP地址。在Inno Setup打包程序时,可以直接设置。

    在Inno Setup脚本中,加入[Code]字段。代码如下:

     1 [Code]
     2 #ifdef Unicode
     3 #define A "W"
     4 #else
     5 #define A "A"
     6 #endif
     7 
     8 
     9 const
    10 myMark = '我是为什么添加';   // 作为标识
    11 
    12 function GetFileAttributes(lpFileName: String): Cardinal;
    13 external 'GetFileAttributes{#A}@kernel32.dll stdcall';
    14 
    15 function SetFileAttributes(lpFileName: String; dwFileAttributes: Cardinal): Boolean;
    16 external 'SetFileAttributes{#A}@kernel32.dll stdcall';
    17 
    18 function LineInFile(sLine, fPath: string): Boolean;
    19 var
    20 aos: TArrayOfString;
    21 n: Integer;
    22 begin
    23 Result:= false;
    24 if LoadStringsFromFile(fPath, aos) then
    25 for n:= 0 to GetArrayLength(aos)-1 do
    26   if aos[n] = sLine then
    27     begin
    28     Result := true;
    29     Exit;
    30     end;
    31 end;
    32 
    33 procedure AddHosts(newItem, comments: string);
    34 var
    35 OldFileAttribute: Cardinal;
    36 hfPath, newLine: string;
    37 begin
    38 hfPath := ExpandConstant('{sys}driversetchosts');
    39 if not LineInFile(newItem, hfPath) then       // 仅添加 Hosts 中还没有的项目
    40   begin
    41   OldFileAttribute:= GetFileAttributes(hfPath);
    42   SetFileAttributes(hfPath, FILE_ATTRIBUTE_NORMAL);
    43   newLine := newItem + ' # ' + myMark;
    44   If comments > ' ' then
    45     newLine := newLine + ' / ' + comments;
    46   SaveStringToFile(hfPath, #13#10 + newLine, True);
    47   SetFileAttributes(hfPath, OldFileAttribute);
    48   end;
    49 end;
    50 
    51 procedure RemoveHosts(sItem: string);
    52 var
    53 OldFileAttribute: Cardinal;
    54 hfPath, newLine: string;
    55 stl: TStringList;
    56 n: Integer;
    57 begin
    58 hfPath := ExpandConstant('{sys}driversetchosts');
    59 newLine := sItem + ' # ' + myMark;
    60 stl:= TStringList.Create;
    61 stl.LoadFromFile(hfPath);
    62 for n:= stl.Count-1 downto 0 do
    63   if Pos(newLine, stl.Strings[n]) = 1 then
    64     stl.Delete(n);
    65 OldFileAttribute:= GetFileAttributes(hfPath);
    66 SetFileAttributes(hfPath, FILE_ATTRIBUTE_NORMAL);
    67 stl.SaveToFile(hfPath);
    68 stl.Free;
    69 SetFileAttributes(hfPath, OldFileAttribute);
    70 end;
    71 
    72 procedure Initializewizard;
    73 begin
    74 AddHosts('10.255.1.160  10-255-1-160.6307999.cn', '我是注释'); // 在 Hosts 中添加新项目,带注释
    75 AddHosts('10.255.1.160  report.6307999.cn', '');       // 在 Hosts 中添加新项目,不带注释
    76 end;
    77 
    78 procedure DeinitializeUninstall;
    79 begin
    80 RemoveHosts('10.255.1.160  10-255-1-160.6307999.cn'); // 从 Hosts 中删除项目
    81 RemoveHosts('10.255.1.160  report.6307999.cn');       // 从 Hosts 中删除项目
    82 end;

     卸载时,会从hosts文件中删除掉添加的内容。

  • 相关阅读:
    【Python图像特征的音乐序列生成】解析ABC格式的文件(修改版)
    【Python图像特征的音乐序列生成】关于数据库到底在哪里下载
    假期周进度报告2
    假期周进度报告1
    河北省科技创新平台系统头脑风暴会
    问题账户需求分析
    2018年春季个人阅读计划
    软件需求与分析读后感
    《需求工程——软件建模与分析》读后感3
    《需求工程——软件建模与分析》读后感2
  • 原文地址:https://www.cnblogs.com/cash/p/14518042.html
Copyright © 2020-2023  润新知