Important alert: (current site time 7/15/2013 5:46:55 PM EDT)
 

VB icon

Changing Local DNS IP

Email
Submitted on: 5/27/2003 11:11:40 AM
By: rMatrix 
Level: Intermediate
User Rating: By 2 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 14621
 
     The utility can be used to change the DNS servers of the TCP/IP settings. By default the utility will swap the first two IP address, also you can specify the number as command line argument to set the specified IP as the Primary DNS.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Changing Local DNS IP
// Description:The utility can be used to change the DNS servers of the TCP/IP settings. By default the utility will swap the first two IP address, also you can specify the number as command line argument to set the specified IP as the Primary DNS.
// By: rMatrix
//
// Inputs:Optional, you can specify the number of DNS to set it as Primary DNS.
//
// Side Effects:The utility has been tested on Windows 2000 and Windows XP. It may not work on other platforms.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6311&lngWId=3//for details.//**************************************

#include <windows.h>
#include <iostream.h>
#include <stdio.h>
//HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{F7D9F953-70DC-4CED-8145-A7E93D3A953C}
void swap (char* szFirstIp, char* szSecondIp)
{
 char temp [15];
 lstrcpy (temp, szFirstIp);
 lstrcpy (szFirstIp, szSecondIp);
 lstrcpy (szSecondIp, temp);
}
main (int argc, char* argv[])
{
 long lRet;
 TCHAR szKeyValue [128];
 HKEY hKey;
 DWORD dwLen =128;
 char* szKey ="SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\";	//Interfaces\\{1F4380AD-3A8A-4CBC-A692-42223902A720}
 char* szDnsRegisteredAdapters = new char [256] ; 
 
 lstrcpy (szDnsRegisteredAdapters, szKey);
 lstrcat (szDnsRegisteredAdapters, "DnsRegisteredAdapters");
 char* szInterfaceKey = new char [256];
 lstrcpy (szInterfaceKey, szKey);
 lstrcat (szInterfaceKey, "Interfaces\\");
 char* szInterfaceId = new char [128];
 
 PFILETIME lpftLastWriteTime = 0;
 unsigned long ulSize;
 lRet = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szDnsRegisteredAdapters, 0, KEY_ALL_ACCESS, &hKey);
 if (lRet !=0 )
 {
	cout << "Error in accessing the Registry" << endl;
	return 0;
 }
 lRet = RegEnumKeyEx (hKey,0,szInterfaceId, &ulSize,NULL,NULL,NULL, lpftLastWriteTime);
 if (lRet !=0 )
 {
	cout << "Error in enumerating the Registry Keys" << endl;
	return 0;
 }
 RegCloseKey(hKey);
 lstrcat (szInterfaceKey, szInterfaceId);
 
 lRet = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szInterfaceKey, 0, KEY_ALL_ACCESS, &hKey);
 if (lRet !=0 )
 {
	cout << "Error in reading the Registry Keys" << endl;
	return 0;
 }
 lRet = RegQueryValueEx (hKey, TEXT("NameServer"),NULL, NULL, (LPBYTE)szKeyValue, &dwLen);
 if (lRet !=0 )
 {
	cout << "Error in reading the value of Registry Keys" << endl;
	return 0;
 } 
 
 char szIpArray [4][15];
 char *szStringArray = new char [45];
 
 
 char* token;
 
 token = strtok(szKeyValue,","); 
 
 int iTokenCnt = 0;
 
 while (token != NULL)
 {
	lstrcpy(szIpArray [iTokenCnt],token);
	iTokenCnt++;
	 
	token = strtok(NULL,",");
 }
 
 
 if (argc == 1)
 {
	swap (szIpArray [0], szIpArray[1]);
 }
 else
 {
	int k = atoi (argv[1]);
	swap (szIpArray [0], szIpArray[k]);
 }
 
 for (int i = 0; i < iTokenCnt; i++)
 {
	if (i == 0) 
	 lstrcpy (szStringArray, szIpArray [i]);
	else
	{
	 lstrcat (szStringArray, ",");
	 lstrcat (szStringArray, szIpArray [i]);
	}
 }
 lRet = RegSetValueEx (hKey, TEXT("NameServer"), 0, REG_SZ, (LPBYTE)szStringArray, strlen (szStringArray) +1);
 if (lRet !=0 )
 {
	cout << "Unable to write the modifications in the Registry." << endl;
	return 0;
 }
 RegCloseKey(hKey); 
 return 0;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/27/2003 12:15:24 PMIxac

Exactly what I wanted. Thanks.
(If this comment was disrespectful, please report it.)

 
5/28/2003 2:47:31 AM

Has anyone tested this code on other platforms? (98/NT/ME) I assume MS did not supply a method which works on all windows versions. (That would be to simple I guess :-))

(If this comment was disrespectful, please report it.)

 
1/13/2004 7:43:46 AMSanchit Karve

Ah! A program I just needed. I tested it on Win9x and XP.
XP-> Works Fine
Win9x -> "Cant Access Registry"

Good Code!
(If this comment was disrespectful, please report it.)

 
3/26/2004 4:20:22 AM

what is the c
and how to traning.
(If this comment was disrespectful, please report it.)

 
9/30/2005 10:36:16 AMSpike

This code does not seem to work on the Windows CE plataform. It will fail on the RegEnumKey. I need to research more on this. On the though life of a WinCE programmer :(
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.