Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This code sample finds a route in the table and changes its metric to NewMetric. The metric change must be viable otherwise TCP/IP will not accept it.

Copy Code
   PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
   ULONG dwSize = 0;
   BOOL bOrder = FALSE;
   DWORD dwStatus = 0;
   DWORD Dest = 0xDDCCBBAA; // this is in host order Ip Address
AA.BB.CC.DD is 0xDDCCBBAA
   DWORD NewMetric = 3;

   // Find out how big our buffer needs to be
   dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize,
bOrder);
   if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {

	// Allocate the memory for the table
	if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize)))
{
		 OutputMessage(TEXT("Malloc failed, Out of Memory!\r\n"));
		 exit(1);
}

	// Now get the table
	dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize,
bOrder);
   }

   if (dwStatus != ERROR_SUCCESS) {
	OutputMessage(TEXT("GetIpForwardTable Failed, ERROR %d\r\n"),
dwStatus);
	if (pIpForwardTable) 
		 free(pIpForwardTable);
	exit(1);
   }
   
   // Search for the row in the table that is wanted.
   for (unsigned long int i=0; i <
pIpForwardTable->dwNumEntries; i++) {
	if (pIpForwardTable->table[i].dwForwardDest == 0x0000389D)
{
		 
		 pIpForwardTable->table[i].dwForwardMetric1 = 3;

		 // Delete the old default gateway entry
		 dwStatus =
SetIpForwardEntry(&(pIpForwardTable->table[i]));

		 if (dwStatus != ERROR_SUCCESS) {
			OutputMessage(TEXT("Could not change route metric\n"));
			exit(1);
		 }
}
   }		 

   if (dwStatus == NO_ERROR)
	OutputMessage(TEXT("Metric changed successfully\n"));
   else if (dwStatus == ERROR_INVALID_PARAMETER)
	OutputMessage(TEXT("Invalid Parameter\n"));
   else 
	DisplayErrorMessage(dwStatus);

   // Free resources
   if (pIpForwardTable) 
	free(pIpForwardTable);

See Also