Funzione IpReleaseAddress (iphlpapi.h)

La funzione IpReleaseAddress rilascia in precedenza un indirizzo IPv4 ottenuto tramite il protocollo DHCP (Dynamic Host Configuration Protocol).

Sintassi

IPHLPAPI_DLL_LINKAGE DWORD IpReleaseAddress(
  [in] PIP_ADAPTER_INDEX_MAP AdapterInfo
);

Parametri

[in] AdapterInfo

Puntatore a una struttura IP_ADAPTER_INDEX_MAP che specifica l'adapter associato all'indirizzo IPv4 da rilasciare.

Valore restituito

Se la funzione ha esito positivo, il valore restituito è NO_ERROR.

Se la funzione ha esito negativo, usare FormatMessage per ottenere la stringa di messaggio per l'errore restituito.

Codice restituito Descrizione
ERROR_INVALID_PARAMETER
Uno dei parametri non è valido. Questo errore viene restituito se il parametro AdapterInfo è NULL o se il membro Name della struttura PIP_ADAPTER_INDEX_MAP puntato dal parametro AdapterInfo non è valido.
ERROR_PROC_NOT_FOUND
Si è verificata un'eccezione durante la richiesta a DHCP per il rilascio dell'indirizzo IPv4.
Altri
Usare FormatMessage per ottenere la stringa di messaggio per l'errore restituito.

Commenti

La funzione IpReleaseAddress è specifica per IPv4 e rilascia solo un indirizzo IPv4 ottenuto in precedenza tramite il protocollo DHCP (Dynamic Host Configuration Protocol). Il membro Name della struttura IP_ADAPTER_INDEX_MAP puntato dal parametro AdapterInfo è l'unico membro utilizzato per determinare l'indirizzo DHCP da rilasciare.

Una matrice di strutture IP_ADAPTER_INDEX_MAP viene restituita nella struttura IP_INTERFACE_INFO dalla funzione GetInterfaceInfo . La struttura IP_INTERFACE_INFO restituita da GetInterfaceInfo contiene almeno una struttura IP_ADAPTER_INDEX_MAP anche se il membro NumAdaptersdella strutturaIP_INTERFACE_INFO indica che non sono abilitate schede di rete con IPv4. Quando il membro NumAdapters della struttura IP_INTERFACE_INFO restituito da GetInterfaceInfo è zero, il valore dei membri della singola struttura IP_ADAPTER_INDEX_MAP restituita nella struttura IP_INTERFACE_INFO non è definita.

Se il membro Name della struttura IP_ADAPTER_INDEX_MAP puntato dal parametro AdapterInfo è NULL, la funzione IpReleaseAddress restituisce ERROR_INVALID_PARAMETER.

Non sono disponibili funzioni per rilasciare o rinnovare un indirizzo IPv6. Questa operazione può essere eseguita solo eseguendo il comando Ipconfig:

ipconfig /release6

ipconfig /renew6

Esempio

Nell'esempio seguente viene recuperato l'elenco delle schede di rete con IPv4 abilitato nel sistema locale, quindi rilascia e rinnova l'indirizzo IPv4 per la prima scheda nell'elenco.

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "iphlpapi.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

// Before calling IpReleaseAddress and IpRenewAddress we use
// GetInterfaceInfo to retrieve a handle to the adapter

void __cdecl main()
{
    ULONG ulOutBufLen = 0;
    DWORD dwRetVal = 0;
    PIP_INTERFACE_INFO pInfo;

    pInfo = (IP_INTERFACE_INFO *) MALLOC(sizeof(IP_INTERFACE_INFO));

    // Make an initial call to GetInterfaceInfo to get
    // the necessary size into the ulOutBufLen variable
    if (GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER) {
        FREE(pInfo);
        pInfo = (IP_INTERFACE_INFO *) MALLOC (ulOutBufLen);
    }

    // Make a second call to GetInterfaceInfo to get the
    // actual data we want
    if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR ) {
        printf("\tAdapter Name: %ws\n", pInfo->Adapter[0].Name);
        printf("\tAdapter Index: %ld\n", pInfo->Adapter[0].Index);
        printf("\tNum Adapters: %ld\n", pInfo->NumAdapters);
    }
    else if (dwRetVal == ERROR_NO_DATA) {
        printf("There are no network adapters with IPv4 enabled on the local system\n");
        return;
    }
    else {
        LPVOID lpMsgBuf;
        printf("GetInterfaceInfo failed.\n");
            
        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 );
        return;
    }

// Call IpReleaseAddress and IpRenewAddress to release and renew
// the IP address on the first network adapter returned 
// by the call to GetInterfaceInfo.
    if ((dwRetVal = IpReleaseAddress(&pInfo->Adapter[0])) == NO_ERROR) {
        printf("IP release succeeded.\n");
    }
    else {
        printf("IP release failed: %ld\n", dwRetVal);
    }

    if ((dwRetVal = IpRenewAddress(&pInfo->Adapter[0])) == NO_ERROR) {
        printf("IP renew succeeded.\n");
    }
    else {
        printf("IP renew failed: %ld\n", dwRetVal);
    }

    // Free memory for IP_INTERFACE_INFO 
    if (pInfo != NULL) {
        FREE(pInfo);
    }
    return;
}

Requisiti

Requisito Valore
Client minimo supportato Windows 2000 Professional [solo app desktop]
Server minimo supportato Windows 2000 Server [solo app desktop]
Piattaforma di destinazione Windows
Intestazione iphlpapi.h
Libreria Iphlpapi.lib
DLL Iphlpapi.dll

Vedi anche

GetInterfaceInfo

Informazioni di riferimento sulla funzione helper IP

Pagina iniziale dell'helper IP

IP_ADAPTER_INDEX_MAP

IP_INTERFACE_INFO

IpRenewAddress