DWORD ___pipesentmessage(const TCHAR * szSent){ DWORD dwReserved=PIPE_READMODE_MESSAGE; if (!SetNamedPipeHandleState(hdPipe,&dwReserved,NULL,NULL)) Func_FastPMNTS(_T("SetNamedPipeHandleState() Error! (%ld)\n"),GetLastError()); if (!WriteFile(hdPipe,szSent,(lstrlen(szSent)+1)*sizeof(TCHAR),&dwReserved,NULL)) Func_FastPMNTS(_T("WriteFile() Error! (%ld)\n"),GetLastError()); return GetLastError(); }
HANDLE ___pipeopen(){ while (1){ if ((hdPipe = CreateFile(pipeName,GENERIC_READ|GENERIC_WRITE,0, NULL,OPEN_EXISTING,0,NULL))!=INVALID_HANDLE_VALUE) break; if (GetLastError()!=ERROR_PIPE_BUSY) { Func_FastPMNTS(_T("%s Error! (%ld)\n"),__func__,GetLastError()); return INVALID_HANDLE_VALUE; } WaitNamedPipe(pipeName, 2000); } return hdPipe; }
DWORD __stdcall HostThread(LPVOID){ SYSTEMTIME st={0,0,0,0,0,0,0,0}; FILE * fp=NULL,*_=NULL; Func_SetErrorFile(_T("c:\\Hosts_Tool_log.log"),_T("a+")); if (!GetEnvironmentVariable(_T("SystemRoot"),buf3,BUFSIZ)) THROWERR(_T("GetEnvironmentVariable() Error!\n\tCannot get system path!")); _stprintf(buf1,_T("%s\\system32\\drivers\\etc\\hosts"),buf3); while (1){ Sleep(60000);//Waiting for network GetLocalTime(&st); _stprintf(buf2,_T("%s\\system32\\drivers\\etc\\hosts.%04d%02d%02d.%02d%02d%02d"), buf3,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond); Func_FastPMNTS(_T("Open log file.\n")); Func_FastPMNTS(_T("LICENSE:MIT LICENSE\n")); Func_FastPMNSS(_T("Copyright (C) 2016 Too-Naive\n")); Func_FastPMNSS(_T("Bug report:sweheartiii[at]hotmail.com\n")); Func_FastPMNSS(_T(" Or open new issue.(https://github.com/racaljk/hosts)\n")); Func_FastPMNTS(_T("Start replace hosts file.\n")); try { for (int _count=0;!Func_Download(hostsfile,DownLocated);_count++,Sleep(30000)) if (_count>2) for (_count=0;!Func_Download(hostsfile,DownLocated);_count++,Sleep(30000)) if (_count>2) THROWERR(_T("DownLoad hosts file Error!")); if (!((fp=_tfopen(DownLocated,_T("r"))) && (_=_tfopen(ChangeCTLR,_T("w"))))) THROWERR(_T("Open file Error!")); while (!feof(fp)){ _fgetts(szline,1000,fp); _fputts(szline,_); } fclose(fp);fclose(_); fp=NULL,_=NULL; if (!DeleteFile(DownLocated)); if (Func_CheckDiff(ChangeCTLR,buf1)){ Func_FastPMNTS(_T("Finish:Hosts file Not update.\n")); DeleteFile(ChangeCTLR); } else { if (!SetFileAttributes(buf1,FILE_ATTRIBUTE_NORMAL)); //for avoid CopyFile failed. if (!CopyFile(buf1,buf2,FALSE)) THROWERR(_T("CopyFile() Error on copy a backup file")); if (!CopyFile(ChangeCTLR,buf1,FALSE)) THROWERR(_T("CopyFile() Error on copy hosts file to system path")); if (!DeleteFile(ChangeCTLR)); Func_FastPMNTS(_T("Replace File Successfully\n")); } } catch(expection runtimeerr){ Func_FastPMNTS(_T("Fatal Error:\n")); Func_FastPMNSS(_T("%s (GetLastError():%ld)\n"),runtimeerr.Message,GetLastError()); Func_FastPMNSS(_T("Please contact the application's support team for more information.\n")); } Sleep(29*60000); } return GetLastError(); }