vc下不重啓改ip、網關、dns

原文地址:http://hi.baidu.com/9908006/blog/item/bed174998952a8186f068c3e.html

看雪上某人發表的,貌似其它都可以改,就是dns有些問題,研究後發現,原來是類型有問題,改了後正常運行了

  1. //VC-ConsoleWithApi 
  2. #include <windows.h> 
  3. #include <string> 
  4.  
  5. #include <iostream> 
  6. #pragma comment(lib,"Ws2_32.lib") 
  7. using namespace std; 
  8. void GetLanAdapterName( char* szLanAdapterName ); 
  9. BOOL RegIP( LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate,  LPCTSTR pDNSServer1,  LPCTSTR pDNSServer2 ); 
  10. BOOL NotifyIPChange( LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask ); 
  11.  
  12. int main() 
  13.  
  14.    char AdapterName[ MAX_PATH ] =""
  15.  
  16.     GetLanAdapterName( AdapterName ); 
  17.  
  18.     RegIP( AdapterName, "172.20.10.252","255.255.255.0""172.20.10.1""211.136.17.107""211.136.18.171" ); 
  19.     
  20.     NotifyIPChange( AdapterName, 0, "172.20.10.252",  "172.20.10.1" ); 
  21.     
  22.  
  23.    return 0; 
  24.  
  25.  
  26.  
  27. //讀取註冊表取得適配器名稱  
  28. void GetLanAdapterName(char* szLanAdapterName) 
  29. {     
  30.    HKEY hKey, hSubKey, hNdiIntKey; 
  31.     if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &hKey ) != ERROR_SUCCESS ) 
  32.         return
  33.     DWORD dwIndex = 0; 
  34.     DWORD dwBufSize = 256; 
  35.     DWORD dwDataType; 
  36.     char szSubKey[256]; 
  37.      unsigned char szData[256]; 
  38.     while( ::RegEnumKeyEx( hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL ) == ERROR_SUCCESS ) 
  39.      {         
  40.       if( ::RegOpenKeyEx( hKey, szSubKey, 0, KEY_READ, &hSubKey ) == ERROR_SUCCESS ) 
  41.          {              
  42.          if( ::RegOpenKeyEx( hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey ) == ERROR_SUCCESS )  
  43.           {               
  44.              dwBufSize = 256; 
  45.                 if( ::RegQueryValueEx( hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS ) 
  46.                  { 
  47.                     if( strcmp( (char*)szData, "ethernet" ) == 0 ) // 判斷是不是以太網卡  
  48.                 {              
  49.                    dwBufSize = 256; 
  50.                         if( ::RegQueryValueEx( hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS ) 
  51.                          {     
  52.                      // szData 中便是適配器詳細描述  
  53.                       dwBufSize = 256; 
  54.                             if( ::RegQueryValueEx( hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize ) == ERROR_SUCCESS )  
  55.                       {   
  56.                         //szData中便是適配器名稱   
  57.                          strcpy( szLanAdapterName, (const char *)szData ); 
  58.                         break
  59.                              } 
  60.                          } 
  61.                      }  
  62.                 } 
  63.                  ::RegCloseKey( hNdiIntKey ); 
  64.              }   
  65.           ::RegCloseKey(hSubKey); 
  66.          }   
  67.        dwBufSize = 256; 
  68.      } 
  69.    /* end of while */     
  70.     
  71.     ::RegCloseKey(hKey); 
  72.  
  73.  
  74. BOOL RegIP( LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate,  LPCTSTR pDNSServer1,  LPCTSTR pDNSServer2 ) 
  75.    HKEY hKey; 
  76.     string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"
  77.     strKeyName += lpszAdapterName; 
  78.    if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_WRITE, &hKey ) != ERROR_SUCCESS ) 
  79.       return FALSE; 
  80.  
  81.     string DNSServer = pDNSServer1; 
  82.     DNSServer += ","
  83.     DNSServer += pDNSServer2; 
  84.  
  85.    char mszIPAddress[ 100 ]; 
  86.    char mszNetMask[ 100 ]; 
  87.    char mszNetGate[ 100 ]; 
  88.    char mszDNSServer[ 100 ]; 
  89.     
  90.     
  91.     strncpy( mszIPAddress, pIPAddress, 98 ); 
  92.     strncpy( mszNetMask, pNetMask, 98 ); 
  93.     strncpy( mszNetGate, pNetGate, 98 ); 
  94.     strncpy( mszDNSServer, DNSServer.c_str(), 98 ); 
  95.  
  96.    int nIP, nMask, nGate, nDNSServer; 
  97.  
  98.     nIP = strlen( mszIPAddress ); 
  99.     nMask = strlen( mszNetMask ); 
  100.     nGate = strlen( mszNetGate ); 
  101.     nDNSServer = strlen( mszDNSServer ); 
  102.  
  103.     *( mszIPAddress + nIP + 1 ) = 0x00; // REG_MULTI_SZ數據需要在後面再加個0 
  104.     nIP += 2; 
  105.  
  106.     *( mszNetMask + nMask + 1 ) = 0x00; 
  107.     nMask += 2; 
  108.  
  109.     *( mszNetGate + nGate + 1 ) = 0x00; 
  110.     nGate += 2; 
  111.  
  112.     *( mszDNSServer + nDNSServer + 1 ) = 0x00; 
  113.     nGate += 2; 
  114.  
  115.  
  116.     ::RegSetValueEx( hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP ); 
  117.     ::RegSetValueEx( hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask ); 
  118.     ::RegSetValueEx( hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate ); 
  119.     ::RegSetValueEx( hKey, "NameServer", 0, REG_SZ, (unsigned char*)mszDNSServer, nDNSServer ); 
  120.  
  121.     ::RegCloseKey(hKey); 
  122.  
  123.    return TRUE; 
  124.  
  125.  
  126.  
  127. //未公開函數DhcpNotifyConfigChange位於 dhcpcsvc.dll中.  
  128. //原型聲明 
  129. typedef BOOL ( CALLBACK* DHCPNOTIFYPROC) ( 
  130.       LPWSTR lpwszServerName,      // 本地機器爲NULL 
  131.       LPWSTR lpwszAdapterName,   // 適配器名稱 
  132.       BOOL bNewIpAddress,         // TRUE表示更改IP 
  133.       DWORD dwIpIndex,         // 指明第幾個IP地址,如果只有該接口只有一個IP地址則爲0 
  134.       DWORD dwIpAddress,         // IP地址 
  135.       DWORD dwSubNetMask,         // 子網掩碼 
  136.       int nDhcpAction            // 對DHCP的操作 0:不修改, 1:啓用 DHCP,2:禁用 DHCP 
  137. );  
  138.  
  139.  
  140. BOOL NotifyIPChange( LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask ) 
  141.    BOOL bResult = FALSE; 
  142.    HINSTANCE hDhcpDll; 
  143.     DHCPNOTIFYPROC pDhcpNotifyProc; 
  144.    WCHAR wcAdapterName[256]; 
  145.  
  146.     MultiByteToWideChar( CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256 ); 
  147.  
  148.    if( ( hDhcpDll = ::LoadLibrary("dhcpcsvc.dll") ) == NULL ) 
  149.       return FALSE; 
  150.  
  151.    if( ( pDhcpNotifyProc = (DHCPNOTIFYPROC)::GetProcAddress( hDhcpDll, "DhcpNotifyConfigChange" ) ) != NULL ) 
  152.       if(   (pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0 ) == ERROR_SUCCESS ) 
  153.           bResult = TRUE; 
  154.  
  155.     ::FreeLibrary(hDhcpDll); 
  156.    return bResult; 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章