Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C++

Setting DNS using iphelp and register, DhcpNotifyConfigChange and DnsFlushResolverCache

Rate me:
Please Sign up or sign in to vote.
4.64/5 (4 votes)
8 Oct 2007CPOL 58.3K   2.2K   28   6
Setting DNS using iphelp and register, DhcpNotifyConfigChange and DnsFlushResolverCache

Introduction

This is one way of changing DNS in C++ using Iphlpapi.h, register, and two undocumented Windows APIs. It's a simple program, no UI for you, sorry...

  1. Use GetAdaptersInfo to get adapters information.
  2. Then use GetPerAdapterInfo to get information for every ethernet network card. The information contains DNS list.
  3. Use register to set the DNS by open HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\
    Interfaces\\lpszAdapterName
    , the name is got from GetAdaptersInfo.
  4. Use DhcpNotifyConfigChange to notify the IP change.
  5. Use DnsFlushResolverCache to flush the DNS.

Using the Code

  1. Use GetAdaptersInfo:
    C++
    if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == 
    			ERROR_BUFFER_OVERFLOW ) // out of buff
     {
      delete pAdapterInfo;
      pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
      pAdapterInfoBkp = pAdapterInfo;
     }
    
    if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS )
    {
       do{
        if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) // If type is etherent
           {
            //Here to use GetPerAdapterInfo 
        }
        pAdapterInfo = pAdapterInfo->Next;
          }while(pAdapterInfo);
        FlushDNS(); 
    }
  2. Use GetPerAdapterInfo:
    C++
    if( GetPerAdapterInfo(
        pAdapterInfo->Index,
        pPerAdapterInfo,
        &ulPerAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // out of buff
    {
        delete pPerAdapterInfo;
        pPerAdapterInfo = (IP_PER_ADAPTER_INFO*)new char[ulPerAdapterInfoSize];
        pPerAdapterInfoBkp = pPerAdapterInfo;
    }
    
    DWORD dwRet;
    if((dwRet = GetPerAdapterInfo(
        pAdapterInfo->Index,
        pPerAdapterInfo,
        &ulPerAdapterInfoSize)) == ERROR_SUCCESS)
    {
        AdaptInfo tmpadpif;
        pAddrStr = pPerAdapterInfo->DnsServerList.Next;
        strncpy(tmpadpif.AdapterName, pAdapterInfo->AdapterName, 
    				sizeof(pAdapterInfo->AdapterName));
        tmpadpif.NameSever = pPerAdapterInfo->DnsServerList.IpAddress.String;
        cout<<pPerAdapterInfo->DnsServerList.IpAddress.String<<endl;
        char tmp[16];
        strcpy( tmp, "172.16.227.4");
        if ( strcmp(pPerAdapterInfo->DnsServerList.IpAddress.String, tmp) == 0 )
        { 
            return 0;
        }
        while(pAddrStr) 
        { 
            tmpadpif.NameSever += ",";
            tmpadpif.NameSever += pAddrStr->IpAddress.String;
            cout<<pAddrStr->IpAddress.String<<endl;
            pAddrStr = pAddrStr->Next; 
        }
        vecAdpif.push_back( tmpadpif );
        RegSetDNS( tmpadpif.AdapterName, tmp);
        NotifyIPChange(pAdapterInfo->AdapterName);
    }
  3. Open registry to set DNS or IP addr. For this part, please refer to the download code.
  4. We have something fresh here:
    C++
    typedef int (CALLBACK* DNSFLUSHPROC)();
    typedef int (CALLBACK* DHCPNOTIFYPROC)
    	(LPWSTR, LPWSTR, BOOL, DWORD, DWORD, DWORD, int);
  5. After setting DNS in registy, we need to inform the system about the change. Now we use Windows undocumented API: DhcpNotifyConfigChange. It is located in dhcpcsvc.dll.
    C++
    BOOL DhcpNotifyConfigChange(
    LPWSTR lpwszServerName, 	// local machine should be NULL
    LPWSTR lpwszAdapterName, 	// Adapt name
    BOOL bNewIpAddress, 	// TRUE indicates changing ip
    DWORD dwIpIndex, 		// which IP addr, if only 1, it's 0
    DWORD dwIpAddress, 	// IP addr
    DWORD dwSubNetMask, 	// mask
    int nDhcpAction ); // DHCP, 0 for not change, 1 for enable, 2 for disable DHCP

    Please refer to the code.

  6. Flush DNS, use another Windows undocumented API: DnsFlushResolverCache, located in dnsapi.dll.
    C++
    DnsFlushResolverCache();
  7. It's ok now.

Points of Interest

I have not learned how to use WMI in VC++. Sigh, it's not a good way to change Ipaddr and DNS in registy. netsh is not good too. If anyone has a better idea, please share with us.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Harly24-Sep-11 2:19
Harly24-Sep-11 2:19 
QuestionVisual C++ 2010 Express Pin
Member 215751130-Jul-10 11:55
Member 215751130-Jul-10 11:55 
AnswerRe: Visual C++ 2010 Express Pin
arabcoder28-Jan-11 8:05
arabcoder28-Jan-11 8:05 
QuestionHi all.. does the "DhcpNotifyConfigChange" function work for ipv6 also? Pin
G Haranadh27-Jul-10 3:08
G Haranadh27-Jul-10 3:08 
GeneralUm... Pin
Snuff Daddy6-Oct-07 21:07
Snuff Daddy6-Oct-07 21:07 
GeneralRe: Um... Pin
Nescot8-Oct-07 15:54
Nescot8-Oct-07 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.