October 24, 2016
Hot Topics:

IP Helper API: ARP, Routing, and Network Statistics

  • September 30, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

The next three functions—CreateIpForwardEntry, DeleteIpForwardEntry, and SetIpForwardEntry—are used to manage IP routing table. Their usage is illustrated in the following sample. It changes default gateway value. Please note that calling only SetIpForwardEntry will not change it, but rather add new one. Due to this reason, the code below first removes all suitable records and then creates a new entry with the desired properties:

void CNWParamsDlg::OnDefaultGateway()
   PMIB_IPFORWARDROW pRow              = NULL;
   ULONG dwSize                        = 0;
   BOOL bOrder                         = FALSE;
   DWORD dwStatus                      = 0;
   DWORD NewGateway = 0xAADDBBCC;      // CC.BB.DD.AA

   dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
      pIpForwardTable = (PMIB_IPFORWARDTABLE)new BYTE[dwSize];
      dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize,

   if (dwStatus != NO_ERROR )
      if (pIpForwardTable)
         delete pIpForwardTable;

   for (unsigned long int i=0; i <
        pIpForwardTable->dwNumEntries; i++)
      if (pIpForwardTable->table[i].dwForwardDest == 0)
         // The default gateway has been found
         if (!pRow)
            // Allocate some memory to store the row in - this is
            // easier than filling in the row structure and helps
            // ensure that only the gateway address is changed.
            pRow = (PMIB_IPFORWARDROW)
                   new BYTE[sizeof(MIB_IPFORWARDROW)];
            // Copy the row
            memcpy(pRow, &(pIpForwardTable->table[i]),

         // Delete the old default gateway entry
         dwStatus = DeleteIpForwardEntry(&(pIpForwardTable->

         if (dwStatus != NO_ERROR)
            TRACE(L"Could not delete old gateway\n");

   // Set the nexthop field to our new gateway - all the other
   // properties of the route will be the same as they were
   // previously.
   pRow->dwForwardNextHop = NewGateway;

   // Create a new route entry for the default gateway.
   dwStatus = CreateIpForwardEntry(pRow);

   if (dwStatus == NO_ERROR)
      TRACE(L"Gateway changed successfully\n");
   else if (dwStatus == ERROR_INVALID_PARAMETER)
      TRACE(L"Invalid Parameter\n");

   // Free resources
   if (pIpForwardTable)
      delete pIpForwardTable;
   if (pRow)
      delete pRow;

Default gateway has dwForwardDest field equals to zero, so that's the criteria sample code uses to detect it. Finally, dwForwardNextHop is set to a new value, and all is done!

Several words about GetBestRoute function. Similar to GetBestInterface, this function returns the best route for specified destination IP address. On succesfull return, the last parameter keeps MIB_IPFORWARDROW variable with info regarding found route. The next snippet show this all:

void CNWParamsDlg::OnBestRoute()
   DWORD dwRes =
   TRACE(L"Best Route: %d",BestRoute.dwForwardDest);

   CString sTmp;
   MIB_IFROW mibRow;
   mibRow.dwIndex = BestRoute.dwForwardIfIndex;
   sTmp.Format(L"Idx: %lu Name: %s", BestRoute.dwForwardIfIndex,

It's really simple. You're providing destination address and optionally local one. You get back the best route for it. That's it!

IP Protocols Statistics

If you develop an application which needs to monitor network, say some kind of sniffer or whatever it is, you may want to know some statistical information about different network protocols. IP Helper API has subset of correspondend functions, which deal with ICMP, IP, TCP, and UPD statistics respectively:

  • GetIpStatistics
  • GetIcmpStatistics
  • GetTcpStatistics
  • GetUdpStatistics
  • SetIpStatistics
  • SetTcpEntry
  • SetIpTTL

You may find a detailed description of each one in SDK help; here, I just want to note that they give you a lot of info about the network state and traffic. Your application may use all this to analyze networking processes.


Download the accompanying code's zip file here.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. Now, he works at an international retail software company as a team leader of the Mobile R department, making programmers' lives in the mobile jungles a little bit simpler.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel