@@ -47,7 +47,7 @@ There seems something wrong in download file, we will retry after 5 seconds.\n")
47
47
#define DownLocated _T (" .\\ hosts" )
48
48
#define ChangeCTLR _T (" .\\ hostsq" )
49
49
#define BAD_EXIT \
50
- _tprintf (_T(" Bad Parameters.\n " )),\
50
+ _tprintf (_T(" Bad Parameters.\n Using [program-name] -? to show how to use. \ n" )),\
51
51
abort();
52
52
const size_t localbufsize=1024 ;
53
53
@@ -98,7 +98,7 @@ int __fastcall __Check_Parameters(int,TCHAR const**);
98
98
void WINAPI Service_Main (DWORD, LPTSTR *);
99
99
void WINAPI Service_Control (DWORD);
100
100
DWORD CALLBACK Main_Thread (LPVOID);
101
- void Func_Service_Install (const TCHAR * );
101
+ void Func_Service_Install ();
102
102
void Func_Service_UnInstall ();
103
103
void NormalEntry ();
104
104
bool Func_CheckDiff (const TCHAR*,const TCHAR*) throw(expection);
@@ -112,15 +112,24 @@ TCHAR DEFBUF(buf1,localbufsize),DEFBUF(buf2,localbufsize),
112
112
DEFBUF(buf3,localbufsize),DEFBUF(szline,localbufsize);
113
113
114
114
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
124
133
};
125
134
126
135
@@ -167,7 +176,7 @@ int _tmain(int argc,TCHAR const ** argv){
167
176
switch (__Check_Parameters (argc,argv)){
168
177
/* CASE(EXEC_START_NORMAL,Shell("-rrun"));
169
178
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());
171
180
CASE(EXEC_START_RUNAS|EXEC_START_UNINSTALL_SERVICE,Func_Service_UnInstall());
172
181
CASE(EXEC_START_INSTALL_SERVICE,Shell("-rinstall"));
173
182
CASE(EXEC_START_UNINSTALL_SERVICE,Shell("-runinstall"));
@@ -177,7 +186,7 @@ int _tmain(int argc,TCHAR const ** argv){
177
186
_tprintf(_T("Bad Parameters."));
178
187
abort();*/
179
188
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 ());
181
190
CASE (EXEC_START_UNINSTALL_SERVICE,Func_Service_UnInstall ());
182
191
CASE (EXEC_START_SERVICE,StartServiceCtrlDispatcher (STE));
183
192
CASE (EXEC_START_HELP,__show_str (SHOW_HELP,Sname));
@@ -260,16 +269,22 @@ void Func_Service_UnInstall(){
260
269
try {
261
270
if (!GetEnvironmentVariable (_T (" SystemRoot" ),buf2,BUFSIZ))
262
271
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"))*/
264
276
if (!(shMang=OpenSCManager (NULL ,NULL ,SC_MANAGER_ALL_ACCESS)))
265
277
THROWERR (_T (" OpenSCManager() Error in Uninstall service." ));
266
278
if (!(shSvc=OpenService (shMang,Sname,SERVICE_ALL_ACCESS)))
267
279
THROWERR (_T (" OpenService() Error in Uninstall service." ));
268
280
if (!ControlService (shSvc,SERVICE_CONTROL_STOP,&ss))
269
281
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
+ }
273
288
if (!DeleteFile (buf1))
274
289
THROWERR (_T (" DeleteFile() Error in Uninstall service." ));
275
290
}
@@ -315,7 +330,7 @@ Please contact the application's support team for more information.\n"),
315
330
}
316
331
317
332
318
- void Func_Service_Install (const TCHAR * st ){
333
+ void Func_Service_Install (){
319
334
SC_HANDLE shMang=NULL ,shSvc=NULL ;
320
335
_tprintf (_T (" LICENSE:MIT LICENSE\n Copyright (C) 2016 @Too-Naive\n\n " ));
321
336
_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){
325
340
THROWERR (_T (" GetEnvironmentVariable() Error in Install Service." ));
326
341
_stprintf (buf1,_T (" %s\\ hoststools.exe" ),buf3);
327
342
_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." ));
328
345
_tprintf (_T (" Step1:Copy file.\n " ));
329
- if (!CopyFile (st ,buf1,FALSE ))
346
+ if (!CopyFile (szline ,buf1,FALSE ))
330
347
THROWERR (_T (" CopyFile() Error in Install Service.(Is service has been installed?)" ));
331
348
_tprintf (_T (" Step2:Connect to SCM.\n " ));
332
349
if (!(shMang=OpenSCManager (NULL ,NULL ,SC_MANAGER_ALL_ACCESS)))
@@ -417,8 +434,8 @@ DWORD __stdcall HostThread(LPVOID){
417
434
Func_FastPMNSS (_T (" Or open new issue.(https://github.com/racaljk/hosts)\n " ));
418
435
Func_FastPMNTS (_T (" Start replace hosts file.\n " ));
419
436
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 ))
422
439
if (_count>2 ) THROWERR (_T (" DownLoad hosts file Error!" ));
423
440
if (!((fp=_tfopen (DownLocated,_T (" r" ))) && (_=_tfopen (ChangeCTLR,_T (" w" )))))
424
441
THROWERR (_T (" Open file Error!" ));
@@ -495,4 +512,3 @@ void WINAPI Service_Control(DWORD dwControl){
495
512
}
496
513
return ;
497
514
}
498
-
0 commit comments