For those who want to modify the Hosts file, in addition to manually locating the Hosts file and editing it with an editor, we can also edit it through batch commands. What I want to introduce to you today is to use the Inno Setup installation package to modify the Hosts when installing the program to achieve the effect of blocking certain applications from accessing certain servers for verification.
The Windows Hosts file stores IP addresses and domain names to help direct computers to sites on the Internet or local network. Editing the Hosts file is generally not required to browse the Web, but it is an essential method of blocking harmful sites and a tool for binding Web addresses to sites under development. Incorrect Hosts file settings can cause websites to stop loading, so if you are unable to connect to certain sites online, check the file for incorrect entries or clear its modifications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#ifdef Unicode #define A "W" #else #define A "A" #endif
[code] const myMark = '由 gnatix 编写'; // 作为标识
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:= 0 to GetArrayLength(aos)-1 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 // 仅添加 Hosts 中还没有的项目 begin OldFileAttribute:= GetFileAttributes(hfPath); SetFileAttributes(hfPath, FILE_ATTRIBUTE_NORMAL); newLine := newItem + ' # ' + myMark; If comments > ' ' then newLine := newLine + ' / ' + comments; SaveStringToFile(hfPath, #13#10 + 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-1 downto 0 do if Pos(newLine, stl.Strings[n]) = 1 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 item in Hosts with comment AddHosts('0.0.0.0 www.111.com', '' ); // Add new items to 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 Hostsend ; |
Copy the above code to the Inno Setup script and save it, or modify it according to your needs.
The Windows hosts file functions like a local copy of a DNS server, so knowing how to edit it might come in handy if you want to do custom domain redirections, block websites, or remove malicious entries set by malware. That said, you may encounter permission errors and other issues when making changes to this file in some versions of Windows.