c++ - flush network setting after modifying the gateway in registry -


i need modify gateway of network setting. see there's defaultgateway , dhcpdefaultgateway params in registry. here's code change these values:

#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)) // adapter         return 1;     if (!setregip(adaptername, "1.1.1.1", "2.2.2.2")) // change ip 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);  }  

but it's not working, guess there must flush after modifying registry value. google found similar problem flushing dns config, simple ipconfig /registerdns. how flush gateway?

here 1 way can accomplish same:

https://social.msdn.microsoft.com/forums/vstudio/en-us/96ece1b7-cc88-4675-b036-90197140c9ab/how-to-change-default-gateway-address?forum=csharpgeneral


Comments