2016-01-29 20 views
0

我需要修改我的网络设置的网关。我看到有DefaultGatewayDhcpDefaultGateway PARAMS在注册表中。这里是我的代码来改变这些值:在修改注册表中的网关后刷新网络设置

#include <windows.h> 
#include <string> 

#include <iostream> 
#pragma comment(lib,"Ws2_32.lib") 
using namespace std; 
void GetLanAdapterName(char* szLanAdapterName); 
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask); 

char szGate[100] = { 0 }; 
DWORD dwGateType; 
char szDhcpGate[100] = { 0 }; 
DWORD dwDhcpGateType; 


BOOL GetRegIP(LPCTSTR lpszAdapterName) 
{ 
    HKEY hKey; 
    string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; 
    strKeyName += lpszAdapterName; 
    if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) 
     return FALSE; 

    ULONG x = sizeof(szGate); 
    auto ret = ::RegQueryValueEx(hKey, "DefaultGateway", 0, &dwGateType, (unsigned char*)szGate, &x); 
    x = sizeof(szDhcpGate); 
    ret = ::RegQueryValueEx(hKey, "DhcpDefaultGateway", 0, &dwDhcpGateType, (unsigned char*)szDhcpGate, &x); 

    ::RegCloseKey(hKey); 

    return TRUE; 
} 
BOOL SetRegIP(LPCTSTR lpszAdapterName, const char* szNewGateway, const char* szNewDhcpGateway) 
{ 
    HKEY hKey; 
    string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; 
    strKeyName += lpszAdapterName; 
    if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) 
     return FALSE; 
    int x; 
    x = strlen(szNewGateway); 
    if (x) 
     ::RegSetValueEx(hKey, "DefaultGateway", 0, dwGateType, (unsigned char*)szNewGateway, x); 
    x = strlen(szNewDhcpGateway); 
    if (x) 
     ::RegSetValueEx(hKey, "DhcpDefaultGateway", 0, dwDhcpGateType, (unsigned char*)szNewDhcpGateway, x); 

    ::RegCloseKey(hKey); 

    return TRUE; 
} 

int main() 
{ 
    char AdapterName[ MAX_PATH ] = ""; 

    GetLanAdapterName(AdapterName); 

    if (!GetRegIP(AdapterName)) // get adapter 
     return 1; 
    if (!SetRegIP(AdapterName, "1.1.1.1", "2.2.2.2")) // change it to some ip that will never work 
     return 1; 
    //if (!SetRegIP(AdapterName, szGate, szDhcpGate)) 
     //return 1; 


    return 0; 
} 

void GetLanAdapterName(char* szLanAdapterName) 
{  
    HKEY hKey, hSubKey, hNdiIntKey; 
    if(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &hKey) != ERROR_SUCCESS) 
     return; 
    DWORD dwIndex = 0; 
    DWORD dwBufSize = 256; 
    DWORD dwDataType; 
    char szSubKey[256]; 
    unsigned char szData[256]; 
    while(::RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) 
    {   
     if(::RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS) 
     {    
     if(::RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS) 
      {    
      dwBufSize = 256; 
       if(::RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) 
       { 
        if(strcmp((char*)szData, "ethernet") == 0) // 
       {    
        dwBufSize = 256; 
         if(::RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) 
         {  
        // 
         dwBufSize = 256; 
          if(::RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) 
         { 

         strcpy(szLanAdapterName, (const char *)szData); 
         break; 
          } 
         } 
        } 
       } 
       ::RegCloseKey(hNdiIntKey); 
      } 
      ::RegCloseKey(hSubKey); 
     } 
     dwBufSize = 256; 
    } 
    /* end of while */  

    ::RegCloseKey(hKey); 
} 

但它不工作,我想修改自己的注册表值之后,必须有一个齐平。从谷歌我发现了一些类似的问题,冲洗DNS配置,简单的ipconfig /registerdns。但是如何刷新网关?

回答