Starting in 1996, Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the Wayback Machine after an embargo period.
Heroes Happen Here
New products, new technologies: Microsoft SQL Server 2008, Microsoft Visual Studio 2008, and Windows Server 2008 create new opportunities to use your existing skills and to grow your business.
» Tips for Growing Your Microsoft Practice
You can grow your practice by increasing business with existing clients or adding more customers, expanding your practice areas or expanding your business into hosted or managed services. Discover which approach to growing your business makes the most sense for you and your customers.
» Drive Your Business with Microsoft Explore the business opportunities available through the Microsoft Partner Program, which is designed to help you generate leads, drive customer demand and sales, increase your profitability, and assess your business performance.
» Partner-to-Partner Networking: A New Paradigm in Relationship Building
Heres a great way to build your business: Team up with partners whose solutions and services work as an extension of your own offerings. View this Webcast to learn how building networks with other partners can help your business grow.
» IT Provider Grows Profits 85% in One Year with Microsoft Technologies
Knight Enterprises Inc., a longtime Novell partner, chose to add Microsoft technologies to its offerings after attending a Microsoft partner event. The company's major accounts soon followed suit, opting for Microsoft solutions over those of the competition. Download this case study to discover how Knight now expects its revenue to double in two years time.
»
This article is based on another article I did, DNSWATCH, at http://www.codeguru.com. You might want to take a look at that before proceeding because this is a newer version with more features, and some new code. The program is based on DNS host name lookups done by programs, mainly browsers. It lists them and allows you to select some and save or append to a temporary file, and optionally update the host file. This allows you to block sites that are just advertisements, or tracking sites and eliminate the overhead of retrieving them. I basically wrote this program to try and watch who was watching me, and block annoying ads. It's also interesting just running it and see how many Web sites are looked up from one Web page.
Editing a system file (hosts) can be dangerous; however, most Windows users don't ever use it, so it's safe doing this. You can block yourself from some Web sites if you select the wrong URL. For example, if you selected "www.ebay.com," saved it, and updated the hosts file, you would not be able to access ebay at all. I added the "edit hosts file" option so you can remove any entries you want to.
I added some command line switches so I could start the program from startup. They are: "-s" to auto start the program, -b to turn off the beeping, and "-t tempfilename" to set the default temp file. After you add the program to the start menu, right-click it and select Properties. In the window will be a line marked Target and an EditBox next to it containing the full path and executable name. Just add the switches after the name. This is the one I use: C:\dev\NewDev\DNSWatch\Debug\DNSWatch.exe -s -b.
I also found a piece of code I have been looking for a long time. ListBoxes always scroll down and new entries can't be seen; the first entries are always seen. There may be other ways to do this, but by adding one line after adding an entry to the listbox it seems to work the opposite, newest at the bottom.
You now can edit the temporary file. I also added some code that invokes the CFileDialog dialog so you can search for a temporary file. I added an On-Top button because that's what I needed once. You now can edit the hosts file. This is not recommended, but it's necessary sometimes (like just checking). You now can update the hosts file with the entries in the temporary file automatically. In WinXP and Win2K, the hosts file resides in \windows\system32\drivers\etc directory. The logic I used for this task is:
Copy the hosts file to a file named hosts.bak and create it or replace it in the etc directory.
Open the temporary file for append. Read in the hosts file and append it to existing entries.
Sort the new file, remove duplicates, and write the results to the hosts file.
Empty the temporary file.
This works and I haven't had any problems yet. Additional backups could be done and the logic could be changed to taste. Here is most of the code I've added. It reads better in the compiler.
Note: You may have to delete and re-add ws2_lib.dll and iphlpapi.lib with the path of your SDK lib directory.
//---------------------------------------------------------int CompHostNames(constvoid *s1, constvoid *s2)
{
return strcmp((constchar *) s1, (constchar *) s2);
}
//--------------------------------------------------------int MergeSortAndDeleteDups()
{
FILE *fh;
int count=0;
char *host_array = (char *)calloc(1000*256,1); //1000 entrieschar ctemp[256], *cptr;
//read in hosts file
fh = fopen (hosts_file_name,"r");
if ( fh == NULL ) return 1; //No default param filewhile(true) {
cptr = fgets((char *)&ctemp,256,fh);
if (cptr == NULL ) break;
strcpy(&host_array[count*256],ctemp);
++count;
if (count >=1000) break;
}
fclose(fh);
//Now read and append the temporary file
fh = fopen (temp_file_name,"r");
if ( fh == NULL ) return 2; //No default param filewhile(true) {
cptr = fgets((char *)&ctemp,256,fh);
if (cptr == NULL ) break;
strcpy(&host_array[count*256],ctemp);
++count;
if (count >=1000) break;
}
fclose(fh);
//We have all the hosts, now sort the array
qsort(host_array,count,256,CompHostNames);
//Now remove duplicates and save in temp file
fh = fopen (temp_file_name,"w");
if ( fh == NULL ) {
free(host_array);
return 3; //???
}
char last_host[256] = "";
for(int i=0; i< count; i++){
strcpy(ctemp, &host_array[i*256]);
if (strcmp(last_host,ctemp) != 0) {
fputs(ctemp,fh);
strcpy(last_host,ctemp);
}
}
fclose(fh);
free(host_array); //no longer needed//Now copy temp to hosts
FILE *fhr = fopen (temp_file_name,"r");
if ( fhr == NULL ) {
return 4; //???
}
FILE *fhw = fopen (hosts_file_name,"w+");
if ( fh == NULL ) {
return 5; //???
}
while(true) {
cptr = fgets((char *)&ctemp,256,fhr);
if (cptr == NULL ) break;
fputs(ctemp,fhw);
}
fclose(fh);
fclose(fhw);
return 0;
}
//---------------------------------------------------------void CDNSWatchDlg::OnUpdateButtonButton()
{
//Save a copy of current host fileint rc = CopyFile(hosts_file_name, hosts_file_name+".bak", FALSE);
temp_file_name = m_save_filename;
//Merge temp and host files, sort,remove duplicates, and update//host file
rc = MergeSortAndDeleteDups();
if ( rc == 0) { ///empty temp file
FILE *fh;
fh = fopen (temp_file_name,"w");
if ( fh == NULL ) return; //??
fseek(fh,0,0);
fclose(fh);
}
m_status = "Hosts File Updated";
UpdateData(FALSE);
}
//---------------------------------------------------------// I simply exec notepad from SystemRoot and edit hosts filevoid CDNSWatchDlg::OnEditHostsButton()
{
char ctemp[256], sysroot[256];
GetEnvironmentVariable("SystemRoot",sysroot,256);
strcpy(ctemp,sysroot);
strcat(ctemp,"\\notepad ");
strcat(ctemp,sysroot);
strcat(ctemp,"\\system32\\drivers\\etc\\hosts");
WinExec(ctemp,SW_SHOWNORMAL);
}
//------------------------------------------------------// I exec notepad from SystemRoot and edit temp filevoid CDNSWatchDlg::OnEditTempFile()
{
char sysroot[256], tstr[256];;
CString Newstr,Srchstr,Srchaddstr="\\",Repstr="\\\\",ctemp;
//this function adds a '\' to each '\' in the temp file display//name
CString Chstr = ConvertAndInsert(Newstr,m_save_filename,
Srchaddstr,Repstr);
//check if temp file doesn't exist, create it
FILE *fhr = fopen (temp_file_name,"a+"); //check if exists,
//create elseif ( fhr == NULL ) {
m_status = "Error opening temp file"; //Error ???
UpdateData(FALSE);
}
fclose(fhr);
GetEnvironmentVariable("SystemRoot",sysroot,256);
strcpy(tstr,sysroot);
strcat(tstr,"\\notepad ");
strcat(tstr,Chstr);
WinExec(tstr,SW_SHOWNORMAL);
}
//------------------------------------------------------//This function takes a string, searches for another string in it,//and inserts a string after the string found. I couldn't seen to//find an easier way for a CString
CString ConvertAndInsert(CString Newstr,CString Srchstr,
CString Srchaddstr,CString Repstr)
{
int ndx = 0;
Newstr = Srchstr;
while(true) {
ndx = Srchstr.Find(Srchaddstr, ndx);
if (ndx == -1 ) break;
++ndx;
Newstr.Insert(ndx,Srchaddstr);
Srchstr = Newstr;
++ndx;
}
return Newstr;
}
//-----------------------------------------------------//This is a function to toggle whether the dialog is always on//top or notvoid CDNSWatchDlg::OnOntopbutton()
{
int rc;
HWND hWandle;
m_on_top_button.SetButtonStyle(BS_DEFPUSHBUTTON);
if ( OnTop == TRUE ) {
OnTop = FALSE;
hWandle = HWND_NOTOPMOST;
m_on_top_button.SetWindowText("Not On Top");
}
else {
OnTop = TRUE;
hWandle = HWND_TOPMOST;
m_on_top_button.SetWindowText("On Top");
}
rc = ::SetWindowPos ( ::GetActiveWindow(), hWandle ,
0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
if ( rc == 1) return;
rc = GetLastError ();
}
//-----------------------------------------------------//This function allows you to search for a temp file using system//dialogvoid CDNSWatchDlg::OnSearchBUTTON()
{
static LPCTSTR kpszFileTypes = _T(" (*.*)||");
CFileDialog fd(TRUE, _T(".acs"), NULL, OFN_HIDEREADONLY,
kpszFileTypes, this);
if (fd.DoModal() == IDCANCEL) return; //nothing selected or//cancelled
m_save_filename = fd.GetPathName(); //get new temp file name
UpdateData(FALSE);
}
Known Quirks
On multi-homed systems where the first interface listed isn't active or is disabled, a bind error will occur.
If no interface is enabled, it will default to the loop back address.
If you use a proxy server for your browser, the hosts file is bypassed and no DNS lookups will show.
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed