참조 : https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Private Declare Function GetIpAddrTable_API Lib "IpHlpApi" Alias "GetIpAddrTable" (pIPAddrTable As Any, pdwSize As Long, ByVal bOrder As Long) As Long 'IP 주소 가져 오기 ' Returns an array with the local IP addresses (as strings). ' Author: Christian d'Heureuse, www.source-code.biz Public Function GetIpAddrTable() As String On Error Resume Next Dim Buf(0 To 511) As Byte Dim BufSize As Long: BufSize = UBound(Buf) + 1 Dim rc As Long rc = GetIpAddrTable_API(Buf(0), BufSize, 1) If rc <> 0 Then Err.Raise vbObjectError, , "GetIpAddrTable failed with return value " & rc Dim NrOfEntries As Integer: NrOfEntries = Buf(1) * 256 + Buf(0) If NrOfEntries = 0 Then GetIpAddrTable = Array(): Exit Function ReDim IpAddrs(0 To NrOfEntries - 1) As String Dim i As Integer For i = 0 To NrOfEntries - 1 Dim j As Integer, s As String: s = "" For j = 0 To 3: s = s & IIf(j > 0, ".", "") & Buf(4 + i * 24 + j): Next IpAddrs(i) = s Next GetIpAddrTable = IpAddrs(UBound(IpAddrs)) End Function |
c++ 에서
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#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); } |