el 02-27-2011 11:21 PM
#include <winsock2.h> #include <ws2tcpip.h> #include <iphlpapi.h> #include <stdio.h> #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) int 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) { printf("Not enough space, re-allocate...\n"); FREE(pIPAddrTable); pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(dwSize); if (pIPAddrTable == NULL) { printf("Memory re-allocation failed for GetIpAddrTable()\n"); exit(1); } else printf("Memory re-allocation for GetIpAddrTable() is OK!\n"); } else printf("Buffer is sufficient...\n"); } // 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); } else printf("GetIpAddrTable() should be fine!\n"); printf("\n\tNumber of 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"); } // Clean up/reset if (pIPAddrTable) { FREE(pIPAddrTable); pIPAddrTable = NULL; } return 0; }
Hola
Tengo un problema al tratar de compliar este codigo, el compliador me arroja este error
el 03-29-2011 06:17 PM
Hola Antonio;
El problema que encontré con el código es que la variable local IPAddr está definida con un tipo in_addr es decir una dirección de Internet IPv4 que a su vez es una structura con varios caracetéres. Estas esctructuras están definidas únicamente en el Windows SDK de Windows Vista y posterior, por lo que si estás trabajando con un sistema operativo previo es posible que no encuentre esta definición en el header que incluyes.
Lo que te recomendaría sería agregar la definición de la estructura en el inicio del programa y probar desde ahí;
La definición que encontré en el sitio de Microsoft sería algo así:
typedef struct in_addr {
union {
struct {
u_char s_b1,s_b2,s_b3,s_b4;
} S_un_b;
struct {
u_short s_w1,s_w2;
} S_un_w;
u_long S_addr;
} S_un;
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;
El mínimo cliente soportado es Windows 2000 Professional y el servidor Windows 2000 Server
Si estás trabajando con una versión Win 2000, 2003 server, XP entonces deberías agregar la librería Inaddr.h y Winsock2.h
Espero esta información te sea de utilidad
Éxito!!!
Marisol