Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
Add new feature
Browse files Browse the repository at this point in the history
* Add hardreset to command line.
* Add a extra option to choose.exe
* Using Marco to replace executable file name.
TODO:
Using g++ compiler may cause a lot of compile error(compiler version 4.7.1)
  • Loading branch information
Too-Naive committed Aug 30, 2016
1 parent 85c0837 commit 4bedbaa
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 74 deletions.
2 changes: 1 addition & 1 deletion g++make.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
windres tool.rc -o tmp.o
g++ -o tool.exe tool.cpp tmp.o -s -lwininet -O2 -DHOSTS_HARD_RESET
g++ -o tool.exe tool.cpp tmp.o -s -lwininet -O2
del /q tmp.o
pause
41 changes: 24 additions & 17 deletions object-runwithcode/choose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#define WIN32_LEAN_AND_MEAN

#define ExecutableFileName _T("tool.exe")

TCHAR const *szParameters[]={
_T("svc"), //for backward compatibility
_T("fi"), //1
_T("fu"), //2
_T("\x02\x03"), //3
_T("hardreset"), //3
_T("-debug-reset"), //4
_T("\x02\x03"), //5
_T("?"), //6
Expand All @@ -35,13 +37,14 @@ TCHAR const * showNotice="\
2. Install automanic update service\n\
3. Uninstall automanic update service\n\
4. Reset Hosts File\n\
5. Show help message\n\
6. Open debug mode to listen service\n\
7. Stop Service\n\
8. Start Service\n\
9. Open \".ini\" file\n\
5. Reset Hosts File(Set to Default Hosts File)\n\
6. Show help message\n\
7. Open debug mode to listen service\n\
8. Stop Service\n\
9. Start Service\n\
10. Open \".ini\" file\n\
";
const int max_input_int=9;
const int max_input_int=10;


TCHAR _[20]=_T("");
Expand All @@ -53,20 +56,24 @@ int _tmain(int,TCHAR const **){
_tprintf(_T("Program mode list:\n%s\n"),showNotice);
_tprintf(_T("Please enter the number what you want to run:"));
_tscanf(_T("%d"),&inputn);
// printf("%d\n",max_input_int);
if (inputn>max_input_int || inputn<1) abort();
switch (inputn){
case 1:ShellExecute(NULL,_T("open"),_T("tool.exe"),NULL,NULL,SW_SHOWNORMAL);
case 1:ShellExecute(NULL,_T("open"),ExecutableFileName,NULL,NULL,SW_SHOWNORMAL);
exit(0);
case 2:str=_T("fi");break;
case 3:str=_T("fu");break;
case 4:str=_T("r");break;
case 5:str=_T("?");break;
case 6:str=szParameters[10];break;
case 7:str=szParameters[7];break;
case 8:str=szParameters[8];break;
case 9:ShellExecute(NULL,_T("open"),_T("notepad"),_T(" %windir%\\hstool.ini"),NULL,SW_SHOWDEFAULT);
case 4:
case 5:str=_T("r");break;
case 6:str=_T("?");break;
case 7:str=szParameters[10];break;
case 8:str=szParameters[7];break;
case 9:str=szParameters[8];break;
case 10:ShellExecute(NULL,_T("open"),_T("notepad"),_T(" %windir%\\hstool.ini"),NULL,SW_SHOWDEFAULT);
exit(0);
}
_stprintf(_,_T("-%s"),str);
ShellExecute(NULL,_T("open"),_T("tool.exe"),_,NULL,SW_SHOWNORMAL);
}
if (inputn==5)
_stprintf(_,_T("-%s %s"),str,szParameters[3]);
else _stprintf(_,_T("-%s"),str);
ShellExecute(NULL,_T("open"),ExecutableFileName,_,NULL,SW_SHOWNORMAL);
}
113 changes: 57 additions & 56 deletions tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@
#define objectwebsite _T("https:\x2f\x2fgithub.com/HostsTools/Windows")
//end.

#define ConsoleTitle _T("racaljk-host tool v2.1.12 Build time:Aug. 30th, '16")
#define ConsoleTitle _T("racaljk-host tool v2.1.13 Build time:Aug. 30th, '16")

#define CASE(x,y) case x : y; break;
#define DEBUGCASE(x) CASE(x,___debug_point_reset(x))
#define pWait _T("\n \
There seems something wrong in download file, we will retry after 5 seconds.\n")

Expand Down Expand Up @@ -109,11 +110,7 @@ struct expection{
}
};

#define SHOW_HELP _T("\
------------------------------------------------------------\n\
Hosts Tool for Windows Console by: Too-Naive\n\
Copyright (C) 2016 @Too-Naive License:General Public License\n\
------------------------------------------------------------\n\n\
#define SHOW_HELP _T("%s\n\
Usage: tool [-? | -r | -fi | -fu | --debug-pipe]\n\n\
Options:\n\
-? : Show this help message.\n\
Expand All @@ -136,6 +133,12 @@ Example:\n\
* *\n\
* Powered by: @Too-Naive *\n\
**********************************************")

#define copyrightshow _T("\
------------------------------------------------------------\n\
Hosts Tool for Windows Console by: Too-Naive\n\
Copyright (C) 2016 @Too-Naive License:General Public License\n\
------------------------------------------------------------\n")

//Global variable
SERVICE_STATUS_HANDLE ssh;
Expand All @@ -155,7 +158,7 @@ void Func_Service_Install(bool);
void Func_Service_UnInstall(bool);
DWORD __stdcall NormalEntry(LPVOID);
void ___debug_point_reset(int);
inline void __show_str(TCHAR const *,TCHAR const *);
inline void __show_str(TCHAR const *,TCHAR const *,TCHAR const *);
void Func_ResetFile();
void ___Func_pipeCallBack(TCHAR const*);
void Func_CallCopyHostsFile();
Expand Down Expand Up @@ -203,7 +206,7 @@ TCHAR const *szParameters[]={
_T("svc"), //for backward compatibility
_T("fi"), //1
_T("fu"), //2
_T("\x02\x03"), //3
_T("hardreset"), //3
_T("-debug-reset"), //4
_T("\x02\x03"), //5
_T("?"), //6
Expand All @@ -223,10 +226,12 @@ int __fastcall __Check_Parameters(int argc,TCHAR const **argv){
size_t i=0;
for (;_tcscmp(&(argv[1][1]),szParameters[i]) &&
i<sizeof(szParameters)/sizeof(szParameters[0]);i++);
if (!(i==0 || i==1 ||i==9)
if (!(i==0 || i==1 || i==9 || i==11)
&& argc>2) BAD_EXIT;
if (argc==3 && !_tcscmp(argv[2],szParameters[11])) request_client=1;
else if (argc==3 && _tcscmp(argv[2],szParameters[11])) BAD_EXIT;
if (argc==3 &&
(!_tcscmp(argv[2],szParameters[11]) || !_tcscmp(argv[2],szParameters[3])))
request_client=1;
else if (argc==3 && !request_client) BAD_EXIT;
switch (i){
case 0: bReserved=true;
return EXEC_START_SERVICE;
Expand All @@ -253,12 +258,16 @@ int _tmain(int argc,TCHAR const ** argv){
CASE(EXEC_START_INSTALL_SERVICE,Func_Service_Install(true));
CASE(EXEC_START_UNINSTALL_SERVICE,Func_Service_UnInstall(true));
CASE(EXEC_START_SERVICE,StartServiceCtrlDispatcher(STE));
CASE(EXEC_START_HELP,__show_str(SHOW_HELP,Sname));
CASE(EXEC_DEBUG_RESET,___debug_point_reset(EXEC_DEBUG_RESET));
CASE(EXEC_START_HELP,__show_str(SHOW_HELP,copyrightshow,Sname));
// CASE(SHOW_LICENSE,__show_str(szgpl_Raw,_pNULL_));
CASE(DEBUG_SERVICE_STOP,___debug_point_reset(DEBUG_SERVICE_STOP));
CASE(DEBUG_SERVICE_START,___debug_point_reset(DEBUG_SERVICE_START));
CASE(DEBUG_SERVICE_REINSTALL,___debug_point_reset(DEBUG_SERVICE_REINSTALL));
DEBUGCASE(EXEC_DEBUG_RESET);
DEBUGCASE(DEBUG_SERVICE_STOP);
DEBUGCASE(DEBUG_SERVICE_START);
DEBUGCASE(DEBUG_SERVICE_REINSTALL);
// CASE(EXEC_DEBUG_RESET,___debug_point_reset(EXEC_DEBUG_RESET));
// CASE(DEBUG_SERVICE_STOP,___debug_point_reset(DEBUG_SERVICE_STOP));
// CASE(DEBUG_SERVICE_START,___debug_point_reset(DEBUG_SERVICE_START));
// CASE(DEBUG_SERVICE_REINSTALL,___debug_point_reset(DEBUG_SERVICE_REINSTALL));
CASE(OPEN_LISTEN,___debug_point_reset(OPEN_LISTEN));
CASE(RESET_FILE,Func_ResetFile());
default:break;
Expand Down Expand Up @@ -304,21 +313,15 @@ namespace __Dpipe{
ss.dwWin32ExitCode=ERROR_SERVICE_SPECIFIC_ERROR;
ss.dwCurrentState=SERVICE_STOPPED;
SetServiceStatus(ssh,&ss);
ExitProcess(1);
ExitProcess(1);
return GetLastError();
}
}

void Func_ResetFile(){
SYSTEMTIME st={0,0,0,0,0,0,0,0};
#ifdef HOSTS_HARD_RESET
bool chk=false;
#endif
_tprintf(_T("\
------------------------------------------------------------\n\
Hosts Tool for Windows Console by: Too-Naive\n\
Copyright (C) 2016 @Too-Naive License:General Public License\n\
------------------------------------------------------------\n"));
_tprintf(copyrightshow);
if (!GetSystemDirectory(buf3,localbufsize))
_tprintf(_T(" GetSystemDirectory() Error!(GetLastError():%ld)\n\
\tCannot get system path!"),GetLastError()),abort();
Expand All @@ -330,37 +333,36 @@ Copyright (C) 2016 @Too-Naive License:General Public License\n\
else
_tprintf(_T("Unable to backup file.(%ld)\n"),GetLastError()),abort();
//reset file start
FILE *fp=_tfopen(buf1,
#ifdef HOSTS_HARD_RESET
_T("w")
#else
_T("r")
#endif
);
FILE *fp=_tfopen(buf1,request_client?_T("w"):_T("r"));
if (!fp) {//,abort();
#ifdef HOSTS_HARD_RESET
_tprintf(_T("Cannot open file!\nUsing plan B.\n"));
fp=_tfopen(ReservedFile,_T("w"));
fclose(fp);
chk=true;
#else
_tprintf(_T("Cannot open file!\n"));
abort();
#endif
}
#ifdef HOSTS_HARD_RESET
if (chk) {
if (!CopyFile(ReservedFile,buf1,FALSE)){
_stprintf(szline,
_T("CopyFile() Failed!(%ld)\nPlease copy the \"%s\" file to\
\"%%systemroot%%\\drivers\\etc\\hosts\" Manually."),
GetLastError(),ReservedFile);
MessageBox(NULL,_T("Fatal Error"),szline,MB_SETFOREGROUND|MB_ICONSTOP);
if (request_client){
_tprintf(_T("Cannot open file!\nUsing plan B.\n"));
fp=_tfopen(ReservedFile,_T("w"));
fclose(fp);
fp=_pNULL_;
chk=true;
}else{
_tprintf(_T("Cannot open file!\n"));
abort();
}
}
else _ftprintf(fp,_T("%s"),szDefatult_hostsfile);
#else
if (request_client){
if (chk) {
if (!CopyFile(ReservedFile,buf1,FALSE)){
_stprintf(szline,
_T("CopyFile() Failed!(%ld)\nPlease copy the \"%s\" file to\
\"%%systemroot%%\\drivers\\etc\\hosts\" Manually."),
GetLastError(),ReservedFile);
MessageBox(NULL,_T("Fatal Error"),szline,MB_SETFOREGROUND|MB_ICONSTOP);
abort();
}
}
else _ftprintf(fp,_T("%s"),szDefatult_hostsfile);
fclose(fp);
_tprintf(_T(" Reset file successfully.\n"));
callsystempause;
ExitProcess(0);
}
if (!GetEnvironmentVariable(_T("TEMP"),szline,localbufsize))
_tprintf(_T("Can't get TEMP path, using current directory.(%ld)\n"),
GetLastError()),
Expand All @@ -382,8 +384,6 @@ Copyright (C) 2016 @Too-Naive License:General Public License\n\
_tprintf(_T("CopyFile() Failed.(%ld)\nPlease copy the \"%s\" file to \
\"%%systemroot%%\\drivers\\etc\\hosts\" Manually."),GetLastError(),ReservedFile),
abort();
#endif
fclose(fp);
//reset file end
DeleteFile(ReservedFile);
_tprintf(_T(" Reset file successfully.\n"));
Expand All @@ -406,9 +406,10 @@ void __abrt(int){
exit(0);
}

inline void __show_str(TCHAR const* st,TCHAR const * _ingore){
if (!_ingore) _tprintf(_T("%s"),st);
else _tprintf(st,_ingore);
inline void __show_str(TCHAR const* st,TCHAR const * str2,TCHAR const * str3){
/* if (!_ingore) _tprintf(_T("%s"),st);
else _tprintf(st,_ingore);*/
_tprintf(st,str2,str3);
callsystempause;
return ;
}
Expand Down

0 comments on commit 4bedbaa

Please sign in to comment.