Funzione GetIpAddrTable (iphlpapi.h)

La funzione GetIpAddrTable recupera la tabella di mapping degli indirizzi da interfaccia a-IPv4.

Sintassi

IPHLPAPI_DLL_LINKAGE DWORD GetIpAddrTable(
  [out]     PMIB_IPADDRTABLE pIpAddrTable,
  [in, out] PULONG           pdwSize,
  [in]      BOOL             bOrder
);

Parametri

[out] pIpAddrTable

Puntatore a un buffer che riceve la tabella di mapping degli indirizzi da interfaccia a-IPv4 come struttura MIB_IPADDRTABLE .

[in, out] pdwSize

In input specifica le dimensioni in byte del buffer a cui punta il parametro pIpAddrTable .

In output, se il buffer non è abbastanza grande per contenere la tabella di mapping restituita, la funzione imposta questo parametro uguale alle dimensioni del buffer necessarie in byte.

[in] bOrder

Se questo parametro è TRUE, la tabella di mapping restituita viene ordinata in ordine crescente in base all'indirizzo IPv4. L'ordinamento viene eseguito nell'ordine di byte di rete. Ad esempio, 10.0.0.255 viene immediatamente prima della 10.0.1.0.

Valore restituito

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

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

Codice restituito Descrizione
ERROR_INSUFFICIENT_BUFFER
Il buffer a cui punta il parametro pIpAddrTable non è sufficiente. Le dimensioni necessarie vengono restituite nella variabile DWORD a cui fa riferimento il parametro pdwSize .
ERROR_INVALID_PARAMETER
Il parametro pdwSize è NULL o GetIpAddrTable non è in grado di scrivere nella memoria a cui punta il parametro pdwSize .
ERROR_NOT_SUPPORTED
Questa funzione non è supportata nel sistema operativo in uso nel sistema locale.
Altri
Usare FormatMessage per ottenere la stringa di messaggio per l'errore restituito.

Commenti

La funzione GetIpAddrTable recupera la tabella di mapping degli indirizzi da interfaccia a-IPv4 in un computer locale e restituisce queste informazioni in una struttura MIB_IPADDRTABLE .

Gli indirizzi IPv4 restituiti dalla funzione GetIpAddrTable sono interessati dallo stato delle interfacce di rete in un computer locale. La reimpostazione manuale di una scheda di interfaccia di rete (NIC) e alcuni eventi PnP possono causare la rimozione o la modifica di un indirizzo IP.

In Windows Server 2003 e Windows XP, gli indirizzi IPv4 restituiti dalla funzione GetIpAddrTable sono interessati anche se la funzionalità di rilevamento multimediale dello stack TCP/IP in un computer locale è stata disabilitata chiamando la funzione DisableMediaSense . Quando il rilevamento multimediale è stato disabilitato, la funzione GetIpAddrTable può restituire indirizzi IPv4 associati alle interfacce disconnesse. Questi indirizzi Ipv4 per le interfacce disconnesse non sono validi per l'uso.

In Windows Server 2008 e Windows Vista gli indirizzi IPv4 restituiti dalla funzione GetIpAddrTable non sono interessati dalla funzionalità di rilevamento multimediale dello stack TCP/IP in un computer locale. La funzione GetIpAddrTable restituisce solo indirizzi IPv4 validi.

La funzione GetAdaptersAddresses disponibile in Windows XP può essere usata per recuperare sia indirizzi IPv6 che IPv4 e informazioni sull'interfaccia.

La struttura MIB_IPADDRTABLE restituita dalla funzione GetIpAddrTable può contenere spaziatura interna per l'allineamento tra il membro dwNumEntries e la prima voce della matrice MIB_IPADDRROW nel membro della tabella . La spaziatura interna per l'allineamento può essere presente anche tra le voci della matrice MIB_IPADDRROW nel membro della tabella . Qualsiasi accesso a una voce di matrice MIB_IPADDRROW deve presupporre che la spaziatura interna possa esistere.

Nel Microsoft Windows Software Development Kit (SDK) rilasciato per Windows Vista e versioni successive, l'organizzazione dei file di intestazione è stata modificata e la MIB_IPADDRROW è definita nel file di intestazione Ipmib.h non nel file di intestazione Iprtrmib.h. Si noti che il file di intestazione Ipmib.h viene incluso automaticamente in Iprtrmib.h che viene incluso automaticamente nel file di intestazione Iphlpapi.h . I file di intestazione Ipmib.h e Iprtrmib.h non devono mai essere usati direttamente.

Esempio

Nell'esempio seguente viene recuperata la tabella degli indirizzi IP, quindi vengono stampati alcuni membri delle voci dell'indirizzo IP nella tabella.

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

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

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

/* Note: could also use malloc() and free() */

int __cdecl main()
{

    int i;
    
    /* Variables used by GetIpAddrTable */
    PMIB_IPADDRTABLE pIPAddrTable;
    DWORD dwSize = 0;
    DWORD dwRetVal = 0;
    IN_ADDR IPAddr;

    /* Variables used to return error message */
    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));

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

        }
        if (pIPAddrTable == NULL) {
            printf("Memory allocation failed for GetIpAddrTable\n");
            exit(1);
        }
    }
    // Make a second call to GetIpAddrTable to get the
    // actual data we want
    if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) { 
        printf("GetIpAddrTable failed with error %d\n", dwRetVal);
        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);
        }
        exit(1);
    }

    printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries);
    for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) {
        printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex);
        IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr;
        printf("\tIP Address[%d]:     \t%s\n", i, inet_ntoa(IPAddr) );
        IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask;
        printf("\tSubnet Mask[%d]:    \t%s\n", i, inet_ntoa(IPAddr) );
        IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr;
        printf("\tBroadCast[%d]:      \t%s (%ld%)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr);
        printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize);
        printf("\tType and State[%d]:", i);
        if (pIPAddrTable->table[i].wType & MIB_IPADDR_PRIMARY)
            printf("\tPrimary IP Address");
        if (pIPAddrTable->table[i].wType & MIB_IPADDR_DYNAMIC)
            printf("\tDynamic IP Address");
        if (pIPAddrTable->table[i].wType & MIB_IPADDR_DISCONNECTED)
            printf("\tAddress is on disconnected interface");
        if (pIPAddrTable->table[i].wType & MIB_IPADDR_DELETED)
            printf("\tAddress is being deleted");
        if (pIPAddrTable->table[i].wType & MIB_IPADDR_TRANSIENT)
            printf("\tTransient address");
        printf("\n");
    }

    if (pIPAddrTable) {
        FREE(pIPAddrTable);
        pIPAddrTable = NULL;
    }

    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

DisableMediaSense

GetAdaptersAddresses

Informazioni di riferimento sulla funzione helper IP

IP_ADAPTER_ADDRESSES

MIB_IPADDRROW

MIB_IPADDRTABLE

RestoreMediaSense