Funzione DeleteIPAddress (iphlpapi.h)

La funzione DeleteIPAddress elimina un indirizzo IP aggiunto in precedenza usando AddIPAddress.

Sintassi

IPHLPAPI_DLL_LINKAGE DWORD DeleteIPAddress(
  [in] ULONG NTEContext
);

Parametri

[in] NTEContext

Contesto NTE (Net Table Entry) per l'indirizzo IP. Questo contesto è stato restituito dalla chiamata precedente a AddIPAddress.

Valore restituito

La funzione restituisce NO_ERROR (zero) se la funzione ha esito positivo.

Se la funzione ha esito negativo, il valore restituito è uno dei codici di errore seguenti.

Codice restituito Descrizione
ERROR_ACCESS_DENIED
Accesso negato. Questo errore viene restituito in Windows Vista e Windows Server 2008 in diverse condizioni che includono quanto segue: l'utente non dispone dei privilegi amministrativi necessari nel computer locale o l'applicazione non è in esecuzione in una shell avanzata come amministratore predefinito (amministratore RunAs).
ERROR_INVALID_PARAMETER
Un parametro di input non è valido, non è stata eseguita alcuna azione.
ERROR_NOT_SUPPORTED
Il trasporto IPv4 non è configurato nel computer locale.
Altri
Usare FormatMessage per ottenere la stringa di messaggio per l'errore restituito.

Commenti

In Windows Vista e versioni successive, la funzione DeleteIPAddress può essere chiamata solo da un utente connesso come membro del gruppo Administrators. Se DeleteIPAddress viene chiamato da un utente che non è membro del gruppo Administrators, la chiamata alla funzione avrà esito negativo e ERROR_ACCESS_DENIED viene restituita. Questa funzione può anche non riuscire a causa del controllo account utente (UAC) in Windows Vista e versioni successive. Se un'applicazione contenente questa funzione viene eseguita da un utente connesso come membro del gruppo Administrators diverso dall'amministratore predefinito, questa chiamata avrà esito negativo a meno che l'applicazione non sia stata contrassegnata nel file manifesto con un set requestedExecutionLevel per richiedereAdministrator. Se l'applicazione in Windows Vista e versioni successive manca di questo file manifesto, un utente ha eseguito l'accesso come membro del gruppo Administrators diverso dall'amministratore predefinito deve quindi eseguire l'applicazione in una shell avanzata come amministratore predefinito (amministratore RunAs) per la riuscita di questa funzione.

Nota In Windows NT 4.0 e Windows 2000 e versioni successive, questa funzione esegue un'operazione con privilegi. Per eseguire correttamente questa funzione, il chiamante deve essere connesso come membro del gruppo Administrators o del gruppo NetworkConfigurationOperators.
 

Esempio

Nell'esempio seguente viene recuperata la tabella degli indirizzi IP, quindi viene aggiunto l'indirizzo IP 192.168.0.27 alla prima scheda. L'indirizzo IP aggiunto viene quindi eliminato.

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

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

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

int main()
{
    // Declare and initialize variables
    PMIB_IPADDRTABLE pIPAddrTable;
    DWORD dwSize = 0;
    DWORD dwRetVal;

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

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

    LPVOID lpMsgBuf;


    // Before calling AddIPAddress we use GetIpAddrTable to get
    // an adapter to which we can add the IP.
    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

    iaIPAddress = inet_addr("192.168.0.27");
    imIPMask = inet_addr("255.255.255.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");

        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");
    }

    exit(0);

Requisiti

   
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

AddIPAddress

Informazioni di riferimento sulla funzione helper IP

Pagina iniziale dell'helper IP