Freigeben über


DeleteIPAddress (Compact 2013)

3/26/2014

Use this function to delete an IP address that was previously added by using AddIPAddress.

Syntax

DWORD DeleteIPAddress(
  ULONG NTEContext
);

Parameters

  • NTEContext
    [in] The Net Table Entry (NTE) context for the IP address. This context is returned by a previous call to AddIPAddress.

Return Value

The function returns NO_ERROR (zero) if the function is successful.

If the function fails, the return value is one of the following error codes.

Return code

Description

ERROR_ACCESS_DENIED

Access is denied.

ERROR_INVALID_PARAMETER

An input parameter is invalid, no action was taken.

ERROR_NOT_SUPPORTED

The IPv4 transport is not configured on the local device.

Other

Use FormatMessage to obtain the message string for the returned error.

Example Code

The following example retrieves the IP address table, then adds the IP address 192.168.0.27 to the first adapter. The IP address that was added is then deleted.

// Before calling AddIPAddress we use GetIpAddrTable to get
// an adapter to which we can add the IP.
PMIB_IPADDRTABLE pIPAddrTable;
DWORD dwSize = 0;

pIPAddrTable = (MIB_IPADDRTABLE*) malloc( sizeof( MIB_IPADDRTABLE) );

// Make an initial call to GetIpAddrTable to get the
// necessary size into the dwSize variable
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
  GlobalFree( pIPAddrTable );
  pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );
}

// Make a second call to GetIpAddrTable to get the
// actual data we want
if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) == NO_ERROR ) { 
  printf("\tAddress: %ld\n", pIPAddrTable->table[0].dwAddr);
  printf("\tMask:    %ld\n", pIPAddrTable->table[0].dwMask);
  printf("\tIndex:   %ld\n", pIPAddrTable->table[0].dwIndex);
  printf("\tBCast:   %ld\n", pIPAddrTable->table[0].dwBCastAddr);
  printf("\tReasm:   %ld\n", pIPAddrTable->table[0].dwReasmSize);
}
else {
  printf("Call to GetIpAddrTable failed.\n");
}

// IP and mask we will be adding
UINT iaIPAddress;
UINT imIPMask;

iaIPAddress = inet_addr("192.168.0.27");
imIPMask = inet_addr("255.255.255.0");

// Variables where handles to the added IP will be returned
ULONG NTEContext = 0;
ULONG NTEInstance = 0;

if ( (dwRetVal = AddIPAddress(iaIPAddress, 
  imIPMask, 
  pIPAddrTable->table[0].dwIndex, 
  &NTEContext, 
  &NTEInstance) ) == NO_ERROR) {
  printf("\tIP address added.\n");
}

else {
  printf("Error adding IP address.\n");

  LPVOID lpMsgBuf;
  if (FormatMessage( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    dwRetVal,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL )) {
    printf("\tError: %s", lpMsgBuf);
  }
  LocalFree( lpMsgBuf );
}

// Delete the IP we just added using the NTEContext
// variable where the handle was returned
if ((dwRetVal = DeleteIPAddress(NTEContext)) == NO_ERROR) {
  printf("\tIP Address Deleted.\n");
}
else {
  printf("\tCall to DeleteIPAddress failed.\n");
}

Requirements

Header

iphlpapi.h

Library

Iphlpapi.lib

See Also

Reference

IP Helper Functions
AddIPAddress

Other Resources

IP Helper