How does Inno Setup modify the Hosts file solution - Big Eye Zai Xu
 How does Inno Setup modify the Hosts file solution

How does Inno Setup modify the Hosts file solution

Author: Da Yanzai~Xu Date: 4 years ago (2020-11-19) Comment: 0

Summary: For those who want to modify the Hosts file, we can not only manually locate the Hosts file and edit it using the editor, but also use batch commands to edit it. Today, Dayan Zai Xu wants to introduce to you that by using the Inno Setup installation package to modify Hosts during the installation of programs, we can shield some applications from accessing some servers for verification. Windows Host File Store

For those who want to modify the Hosts file, we can manually navigate to Hosts In addition to editing files with an editor, we can also edit them with batch commands. today Big Eye Baby Xu What I want to introduce to you is that through the use of Inno Setup The installation package modifies Hosts when installing programs to shield some applications from accessing some servers for verification.

Windows host file storage IP addresses and domain names help direct your computer to sites on the Internet or your local network. It is usually unnecessary to edit the Hosts file when browsing the Web, but it is a basic method to prevent harmful sites, or a tool to bind the Web address with the website under development. Incorrect settings of the Hosts file will cause the website to stop loading. Therefore, if you cannot connect to some sites online, please check the error entries in the file or clear their modifications.

one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
forty-five
forty-six
forty-seven
forty-eight
forty-nine
fifty
fifty-one
fifty-two
fifty-three
fifty-four
fifty-five
fifty-six
fifty-seven
fifty-eight
fifty-nine
sixty
sixty-one
sixty-two
sixty-three
sixty-four
sixty-five
sixty-six
sixty-seven
sixty-eight
sixty-nine
seventy
seventy-one
seventy-two
seventy-three
seventy-four
seventy-five
seventy-six
seventy-seven
seventy-eight
seventy-nine
eighty
eighty-one
#ifdef Unicode
#define A "W"
#else
#define A "A"
#endif

[ code ]
const
myMark = 'Written by gnatix' ;   //As identification

function GetFileAttributes ( lpFileName: String ) : Cardinal ;
external 'GetFileAttributes{#A}@kernel32.dll stdcall' ;

function SetFileAttributes ( lpFileName: String ; dwFileAttributes: Cardinal ) : Boolean ;
external 'SetFileAttributes{#A}@kernel32.dll stdcall' ;

function LineInFile ( sLine, fPath: string ) : Boolean ;
var
aos: TArrayOfString;
n: Integer ;
begin
Result:= false ;
if LoadStringsFromFile ( fPath, aos ) then
for n:= zero to GetArrayLength ( aos ) - one do
  if aos [ n ] = sLine then
    begin
    Result := true ;
    Exit ;
    end ;
end ;

procedure AddHosts ( newItem, comments: string ) ;
var
OldFileAttribute: Cardinal ;
hfPath, newLine: string ;
begin
hfPath := ExpandConstant ( '{sys}\drivers\etc\hosts' ) ;
if not LineInFile ( newItem, hfPath ) then       //Only add items that are not in Hosts
  begin
  OldFileAttribute:= GetFileAttributes ( hfPath ) ;
  SetFileAttributes ( hfPath, FILE_ATTRIBUTE_NORMAL ) ;
  newLine := newItem + ' # ' + myMark;
  If comments > ' ' then
    newLine := newLine + ' / ' + comments;
  SaveStringToFile ( hfPath, # thirteen # ten + newLine, True ) ;
  SetFileAttributes ( hfPath, OldFileAttribute ) ;
  end ;
end ;

procedure RemoveHosts ( sItem: string ) ;
var
OldFileAttribute: Cardinal ;
hfPath, newLine: string ;
stl: TStringList ;
n: Integer ;
begin
hfPath := ExpandConstant ( '{sys}\drivers\etc\hosts' ) ;
newLine := sItem + ' # ' + myMark;
stl:= TStringList . Create ;
stl. LoadFromFile ( hfPath ) ;
for n:= stl. Count - one downto zero do
  if Pos ( newLine, stl. Strings [ n ] ) = one then
    stl. Delete ( n ) ;
OldFileAttribute:= GetFileAttributes ( hfPath ) ;
SetFileAttributes ( hfPath, FILE_ATTRIBUTE_NORMAL ) ;
stl. SaveToFile ( hfPath ) ;
stl. Free ;
SetFileAttributes ( hfPath, OldFileAttribute ) ;
end ;

procedure Initializewizard;
begin
AddHosts ( '0.0.0.0 www.xxx.com' , 'This is a comment' ) ; //Add a new project in Hosts with comments
AddHosts ( '0.0.0.0 www.111.com' , '' ) ;       //Add a new project in Hosts without comments
end ;

procedure DeinitializeUninstall;
begin
RemoveHosts ( '0.0.0.0 www.xxx.com' ) ; //Remove items from Hosts
RemoveHosts ( '0.0.0.0 www.111.com' ) ;       //Remove items from Hosts
end ;

Copy the above code to the Inno Setup script to save it or modify and adjust it according to your needs.

The function of the Windows hosts file is similar to the local copy of the DNS server. Therefore, if you want to customize the domain redirection, block the website or delete the malicious entries set by the malware, it may be useful to know how to edit them. That is, you may encounter permission errors and other problems when making changes to this file in some versions of Windows.

Statement: Big Eye Baby Xu | This article uses the signature - non-commercial use - sharing 4.0 international license agreement in the same way[ CC BY-NC-SA ]Authorize
Article name:《 How does Inno Setup modify the Hosts file solution
Article fixed link: http://www.dayanzai.me/inno-setup-hosts.html
The resources of this website are only for personal learning and exchange. Please delete them within 24 hours after downloading, and they are not allowed to be used for commercial purposes, otherwise the legal issues will be borne by yourself.
Reprint statement
All comments: (0)
^_^No comment yet!

Comment

Back to top