Skip to content

Commit a9f752f

Browse files
committed
Merge pull request #353 from CreationAugust/master
Windows console hosts tool Important update
2 parents 081d6ef + a13e8c5 commit a9f752f

File tree

4 files changed

+69
-53
lines changed

4 files changed

+69
-53
lines changed

hosts_tools/HostsToolforWindows/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Download Executable File
22

3-
Download address:[hosts_tool.exe](https://git.io/vV2YO)
3+
Download address:[hosts_tool.exe](https://git.io/vVKmr)
44

55
# Hosts Tool
66

@@ -22,7 +22,7 @@ main program file: `hosts_tool.exe`
2222

2323
安装服务后 日志文件会保存在`c:\Hosts_Tool_log.log`下 您可以通过查看日志观察服务的工作状态
2424

25-
卸载服务请使用原来的`hosts_tool.exe`文件 请不要在命令行中直接执行`hosts_tool -fu`
25+
卸载服务请使用原来的`hosts_tool.exe`文件 请不要在命令行中直接执行`hosts_tool -fu`(如执行 需要手动删除`%SystemRoot%`目录下的`hoststools.exe`)
2626

2727
请间隔一段时间后清理`%SystemRoot%\system32\drivers\etc\`文件夹 (因为可能堆满了备份的文件)
2828

@@ -51,7 +51,7 @@ windres hosts_tool.rc -o hosts_toolr.o
5151
g++ -o hosts_tool.exe hosts_tool.cpp hosts_toolr.o -lwininet -O2 -s
5252
```
5353

54-
`hosts_tool.exe.manifest`:
54+
File `hosts_tool.exe.manifest`:
5555

5656
```xml
5757
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

hosts_tools/HostsToolforWindows/hosts_tool.cpp

+38-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ There seems something wrong in download file, we will retry after 5 seconds.\n")
4747
#define DownLocated _T(".\\hosts")
4848
#define ChangeCTLR _T(".\\hostsq")
4949
#define BAD_EXIT \
50-
_tprintf(_T("Bad Parameters.\n")),\
50+
_tprintf(_T("Bad Parameters.\nUsing [program-name] -? to show how to use.\n")),\
5151
abort();
5252
const size_t localbufsize=1024;
5353

@@ -98,7 +98,7 @@ int __fastcall __Check_Parameters(int,TCHAR const**);
9898
void WINAPI Service_Main(DWORD, LPTSTR *);
9999
void WINAPI Service_Control(DWORD);
100100
DWORD CALLBACK Main_Thread(LPVOID);
101-
void Func_Service_Install(const TCHAR *);
101+
void Func_Service_Install();
102102
void Func_Service_UnInstall();
103103
void NormalEntry();
104104
bool Func_CheckDiff(const TCHAR*,const TCHAR*) throw(expection);
@@ -112,15 +112,24 @@ TCHAR DEFBUF(buf1,localbufsize),DEFBUF(buf2,localbufsize),
112112
DEFBUF(buf3,localbufsize),DEFBUF(szline,localbufsize);
113113

114114
enum _Parameters{
115-
EXEC_START_NORMAL =1,
116-
// EXEC_START_RUNAS =2,
117-
EXEC_START_SERVICE =4,
118-
EXEC_START_INSTALL_SERVICE =8,
119-
EXEC_START_UNINSTALL_SERVICE =16,
120-
EXEC_START_HELP =32,
121-
EXEC_DEBUG_RESET =64,
122-
SHOW_LICENSE =128,
123-
EXEC_BAD_PARAMETERS =65536
115+
EXEC_START_NORMAL =1<<0x00,
116+
// EXEC_START_RUNAS =1<<0x01,
117+
EXEC_START_SERVICE =1<<0x02,
118+
EXEC_START_INSTALL_SERVICE =1<<0x03,
119+
EXEC_START_UNINSTALL_SERVICE =1<<0x04,
120+
EXEC_START_HELP =1<<0x05,
121+
EXEC_DEBUG_RESET =1<<0x06,
122+
SHOW_LICENSE =1<<0x07,
123+
PARAMETERS_RESERVED1 =1<<0x08,
124+
PARAMETERS_RESERVED2 =1<<0x09,
125+
PARAMETERS_RESERVED3 =1<<0x0a,
126+
PARAMETERS_RESERVED4 =1<<0x0b,
127+
PARAMETERS_RESERVED5 =1<<0x0c,
128+
PARAMETERS_RESERVED6 =1<<0x0d,
129+
PARAMETERS_RESERVED7 =1<<0x0e,
130+
PARAMETERS_RESERVED8 =1<<0x0f,
131+
PARAMETERS_RESERVED9 =1<<0x10,
132+
EXEC_BAD_PARAMETERS =1073741824
124133
};
125134

126135

@@ -167,7 +176,7 @@ int _tmain(int argc,TCHAR const ** argv){
167176
switch (__Check_Parameters(argc,argv)){
168177
/* CASE(EXEC_START_NORMAL,Shell("-rrun"));
169178
CASE(EXEC_START_RUNAS|EXEC_START_NORMAL,NormalEntry());
170-
CASE(EXEC_START_RUNAS|EXEC_START_INSTALL_SERVICE,Func_Service_Install(argv[0]));
179+
CASE(EXEC_START_RUNAS|EXEC_START_INSTALL_SERVICE,Func_Service_Install());
171180
CASE(EXEC_START_RUNAS|EXEC_START_UNINSTALL_SERVICE,Func_Service_UnInstall());
172181
CASE(EXEC_START_INSTALL_SERVICE,Shell("-rinstall"));
173182
CASE(EXEC_START_UNINSTALL_SERVICE,Shell("-runinstall"));
@@ -177,7 +186,7 @@ int _tmain(int argc,TCHAR const ** argv){
177186
_tprintf(_T("Bad Parameters."));
178187
abort();*/
179188
CASE(EXEC_START_NORMAL,NormalEntry());
180-
CASE(EXEC_START_INSTALL_SERVICE,Func_Service_Install(argv[0]));
189+
CASE(EXEC_START_INSTALL_SERVICE,Func_Service_Install());
181190
CASE(EXEC_START_UNINSTALL_SERVICE,Func_Service_UnInstall());
182191
CASE(EXEC_START_SERVICE,StartServiceCtrlDispatcher(STE));
183192
CASE(EXEC_START_HELP,__show_str(SHOW_HELP,Sname));
@@ -260,16 +269,22 @@ void Func_Service_UnInstall(){
260269
try{
261270
if (!GetEnvironmentVariable(_T("SystemRoot"),buf2,BUFSIZ))
262271
THROWERR(_T("GetEnvironmentVariable() Error in UnInstall Service."));
263-
_stprintf(buf1,_T("%s\\hoststools.exe"),buf2);
272+
_stprintf(buf1,_T("%s\\hoststools.exe"),buf2);/*
273+
if (!GetModuleFileName(NULL,szline,sizeof(szline)/sizeof(TCHAR)));
274+
THROWERR(_T("GetModuleFileName() Error in Uninstall Service."));
275+
if (!_tcscmp(buf1,szline)) THROWERR(_T("Please"))*/
264276
if (!(shMang=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS)))
265277
THROWERR(_T("OpenSCManager() Error in Uninstall service."));
266278
if (!(shSvc=OpenService(shMang,Sname,SERVICE_ALL_ACCESS)))
267279
THROWERR(_T("OpenService() Error in Uninstall service."));
268280
if (!ControlService(shSvc,SERVICE_CONTROL_STOP,&ss))
269281
THROWERR(_T("ControlService() Error in Uninstall service."));
270-
Sleep(1000);//Wait for service stop
271-
if (!DeleteService(shSvc))
272-
THROWERR(_T("DeleteService() Error in UnInstall service."));
282+
Sleep(2000);//Wait for service stop
283+
if (!DeleteService(shSvc)){
284+
_tprintf(_T("Executable File located:%s\n"),buf1);
285+
THROWERR(_T("DeleteService() Error in UnInstall service.\n\
286+
You may should delete it manually."));
287+
}
273288
if (!DeleteFile(buf1))
274289
THROWERR(_T("DeleteFile() Error in Uninstall service."));
275290
}
@@ -315,7 +330,7 @@ Please contact the application's support team for more information.\n"),
315330
}
316331

317332

318-
void Func_Service_Install(const TCHAR * st){
333+
void Func_Service_Install(){
319334
SC_HANDLE shMang=NULL,shSvc=NULL;
320335
_tprintf(_T(" LICENSE:MIT LICENSE\n Copyright (C) 2016 @Too-Naive\n\n"));
321336
_tprintf(_T(" Bug report:sweheartiii[at]hotmail.com \n\t Or open new issue\n\
@@ -325,8 +340,10 @@ void Func_Service_Install(const TCHAR * st){
325340
THROWERR(_T("GetEnvironmentVariable() Error in Install Service."));
326341
_stprintf(buf1,_T("%s\\hoststools.exe"),buf3);
327342
_stprintf(buf2,_T("%s\\hoststools.exe -svc"),buf3);
343+
if (GetModuleFileName(NULL,szline,sizeof(szline)/sizeof(TCHAR)))
344+
THROWERR(_T("GetModuleFileName() Error in Install Service."));
328345
_tprintf(_T(" Step1:Copy file.\n"));
329-
if (!CopyFile(st,buf1,FALSE))
346+
if (!CopyFile(szline,buf1,FALSE))
330347
THROWERR(_T("CopyFile() Error in Install Service.(Is service has been installed?)"));
331348
_tprintf(_T(" Step2:Connect to SCM.\n"));
332349
if (!(shMang=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS)))
@@ -417,8 +434,8 @@ DWORD __stdcall HostThread(LPVOID){
417434
Func_FastPMNSS(_T(" Or open new issue.(https://github.com/racaljk/hosts)\n"));
418435
Func_FastPMNTS(_T("Start replace hosts file.\n"));
419436
try {
420-
for (int _count=0;!Func_Download(hostsfile,DownLocated);_count++,Sleep(30000))
421-
if (_count>2) for (_count=0;!Func_Download(hostsfile,DownLocated);_count++,Sleep(30000))
437+
for (int _count=0;!Func_Download(hostsfile,DownLocated);_count++,Sleep(10000))
438+
if (_count>2) for (_count=0;!Func_Download(hostsfile,DownLocated);_count++,Sleep(10000))
422439
if (_count>2) THROWERR(_T("DownLoad hosts file Error!"));
423440
if (!((fp=_tfopen(DownLocated,_T("r"))) && (_=_tfopen(ChangeCTLR,_T("w")))))
424441
THROWERR(_T("Open file Error!"));
@@ -495,4 +512,3 @@ void WINAPI Service_Control(DWORD dwControl){
495512
}
496513
return ;
497514
}
498-
+27-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
#include <tchar.h>
2-
3-
#pragma once
4-
5-
TCHAR szMitLicenseRaw[]=_T("\n\
6-
The MIT License(MIT)(redefined£©\n\
7-
\n\
8-
Copyright(c) 2016 Too-Naive E-mail:[email protected]\n\
9-
\n\
10-
Permission is hereby granted, free of charge, to any person obtaining a copy\n\
11-
of this software and associated documentation files(the \"Software\"), to deal\n\
12-
in the Software without restriction, including without limitation the rights\n\
13-
to use, copy, modify, merge, publish, distribute, and to permit persons to \n\
14-
whom the Software is furnished to do so, BUT DO NOT SUBLICENSE, AND / OR SELL\n\
15-
OF THE SOFTWARE,subject to the following conditions :\n\
16-
\n\
17-
The above copyright notice and this permission notice shall be included in\n\
18-
all copies or substantial portions of the Software.\n\
19-
\n\
20-
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\
21-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\
22-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\
23-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\
24-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\
25-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\
26-
SOFTWARE.\n\
27-
");
1+
#include <tchar.h>
2+
3+
#pragma once
4+
5+
TCHAR szMitLicenseRaw[]=_T("\n\
6+
The MIT License(MIT)(redefined)\n\
7+
\n\
8+
Copyright(c) 2016 Too-Naive E-mail:[email protected]\n\
9+
\n\
10+
Permission is hereby granted, free of charge, to any person obtaining a copy\n\
11+
of this software and associated documentation files(the \"Software\"), to deal\n\
12+
in the Software without restriction, including without limitation the rights\n\
13+
to use, copy, modify, merge, publish, distribute, and to permit persons to \n\
14+
whom the Software is furnished to do so, BUT DO NOT SUBLICENSE, AND / OR SELL\n\
15+
OF THE SOFTWARE,subject to the following conditions :\n\
16+
\n\
17+
The above copyright notice and this permission notice shall be included in\n\
18+
all copies or substantial portions of the Software.\n\
19+
\n\
20+
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\
26+
SOFTWARE.\n\
27+
");

hosts_tools/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
- HostToolforMac: Mac 环境下的 hosts 自动部署更新应用(适用于 OS X 10.10 或以上),基于 Swift 开发。由 @ZzzM 贡献。
2323

24-
- HostsToolforWindows: Windows console环境下的 hosts 自动更新应用(作为系统服务),使用 **C++**构建。由 @Too-Naive 贡献。
24+
- HostsToolforWindows: Windows console环境下的 hosts 自动更新应用(作为系统服务),使用 **C++** 构建。由 @Too-Naive 贡献。
2525

2626
- [`script_tool_for_windows.bat`](http://keving.pythonanywhere.com/hosts_scripts/script_tool_for_windows.bat): Windows 环境下的 hosts 一键部署脚本,由 @redapple0204@lizongzeshunshun 贡献。以及 @lideming 提供的重要更新。
2727

0 commit comments

Comments
 (0)