|
原帖地址: http://bbs.hanzify.org/read-htm-tid-97864.html想请教老虎版主,为什么不能再卸载时删除写入 hosts 的内容?自己找不到问题出在哪儿? - #ifdef Unicode
- #define A "W"
- #else
- #define A "A"
- #endif
- #define AppID "My Program"
- [Setup]
- AppName=My Program
- AppVersion=1.5
- DefaultDirName={pf}\My Program
- DefaultGroupName=My Program
- UninstallDisplayIcon={app}\MyProg.exe
- [Tasks]
- Name: "PATCHhosts"; Description: "屏蔽hosts"; GroupDescription:"其它任务"; Flags: checkedonce
- [code]
- var
- //当没有选择语言时,禁用下一步
- OldEvent_ComponentsListClickCheck: TNotifyEvent;
- //屏蔽 hosts 段开始
- const
- myMark = '546242502'; // 作为标识,用来标识你修改的内容
- 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(sItem, fPath: string): Boolean;
- var
- aos: TArrayOfString;
- i: Integer;
- begin
- Result:= false;
- if LoadStringsFromFile(fPath, aos) then
- for i:= 0 to GetArrayLength(aos)-1 do
- if Pos(sItem, TrimLeft(aos[i])) = 1 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;
- s: AnsiString;
- stl: TStringList;
- i: LongInt;
- begin
- hfPath := ExpandConstant('{sys}\drivers\etc\hosts');
- OldFileAttribute := GetFileAttributes(hfPath);
- SetFileAttributes(hfPath, FILE_ATTRIBUTE_NORMAL);
- newLine := sItem + ' # ' + myMark;
- stl := TStringList.Create;
- stl.LoadFromFile(hfPath);
- for i:= stl.Count-1 downto 0 do
- if Pos(newLine, TrimLeft(stl.Strings[i])) = 1 then
- stl.Delete(i);
- s := stl.Text;
- SetLength(s, Length(s)-Length(#13#10));
- SaveStringToFile(hfPath, s, false);
- stl.Free;
- SetFileAttributes(hfPath, OldFileAttribute);
- end;
- function WasTaskSelected(sTask, AppID: String): boolean;
- var
- sSelectedTasks: String;
- begin
- Result := false;
- sSelectedTasks := '';
- if RegQueryStringValue(HKLM, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\'+AppID+'_is1',
- 'Inno Setup: Selected Tasks', sSelectedTasks) then
- begin
- if Pos(',', sSelectedTasks) = 0 then
- Result := sSelectedTasks = sTask
- else
- begin
- if Pos(sTask+',', sSelectedTasks) = 1 then
- Result := true;
- if Pos(','+sTask+',', sSelectedTasks) > 0 then
- Result := true;
- if Pos(','+sTask, sSelectedTasks) = Length(sSelectedTasks) - Length(sTask) then
- Result := true;
- end;
- end;
- end;
- //屏蔽 hosts 段结束
- procedure CurStepChanged(CurStep: TSetupStep );
- begin
- //屏蔽 hosts 代码
- if CurStep = ssPostInstall then // 安装文件前检查
- if IsTaskSelected('PATCHhosts') then // 是否选择了相应的任务
- begin
- AddHosts('127.0.0.1 support.wondershare.net', ''); // 在 Hosts 中添加新项目,带注释
- AddHosts('127.0.0.1 www.wondershare.net', ''); // 在 Hosts 中添加新项目,不带注释
- AddHosts('127.0.0.1 support.wondershare.com', '');
- AddHosts('127.0.0.1 www.wondershare.com', '');
- AddHosts('127.0.0.1 cbs.wondershare.com', '');
- end;
- end;
- procedure CurUninstallStepChanged1(CurUninstallStep: TUninstallStep);
- begin
- //屏蔽 hosts 代码
- if (CurUninstallStep = usPostUninstall) and (WasTaskSelected('PATCHhosts', '{#AppID}')) then // 卸载文件前检查
- RemoveHosts('127.0.0.1 support.wondershare.net'); // 从 Hosts 中删除项目
- RemoveHosts('127.0.0.1 www.wondershare.net'); // 从 Hosts 中删除项目
- RemoveHosts('127.0.0.1 support.wondershare.com'); // 从 Hosts 中删除项目
- RemoveHosts('127.0.0.1 www.wondershare.com'); // 从 Hosts 中删除项目
- RemoveHosts('127.0.0.1 cbs.wondershare.com'); // 从 Hosts 中删除项目
- end;
- procedure CurUninstallStepChanged2(CurUninstallStep: TUninstallStep);
- begin
- //卸载时提醒用户是否清理残留
- if CurUninstallStep = usDone then
- if MsgBox(CustomMessage('cmcleanhint'), mbConfirmation, MB_YESNO) = IDYES then
- //删除文件夹及其中所有文件
- DelTree(ExpandConstant('{app}'), True, True, True);
- end;
|