Вы находитесь на странице: 1из 3

//************************************** // // Name: Changing Local DNS IP // Description:The utility can be used t // o change the DNS servers of the TCP/IP

s // ettings. By default the utility will swa // p the first two IP address, also you can // specify the number as command line argum // ent to set the specified IP as the Prima // ry DNS. // By: rMatrix // // Inputs:Optional, you can specify the // number of DNS to set it as Primary DNS. // // Side Effects:The utility has been tes // ted on Windows 2000 and Windows XP. It m // ay not work on other platforms. // //This code is copyrighted and has// limited warranties.Please see http:// // www.Planet-Source-Code.com/vb/scripts/Sh // owCode.asp?txtCodeId=6311&lngWId=3//for details.//************************************** // #include <windows.h> #include <iostream.h> #include <stdio.h> //HKEY_LOCAL_MACHINE\SYSTEM\CurrentContr // olSet\Services\Tcpip\Parameters\Interfac // es\{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) { } else { int k = atoi (argv[1]); swap (szIpArray [0], szIpArray[k]); } for (int i = 0; i < iTokenCnt; i++) {

swap (szIpArray [0], szIpArray[1]);

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 ) { << endl; } cout << "Unable to write the modifications in the Registry." return 0;

RegCloseKey(hKey); return 0;

Вам также может понравиться