使用GetAdaptersInfo獲取本地IP信息

MSDN中關於GetAdaptersInfo的說明:

 

This function retrieves adapter information for the local computer.

DWORD GetAdaptersInfo(
  PIP_ADAPTER_INFO pAdapterInfo,
  PULONG pOutBufLen
);

Parameters

pAdapterInfo
[out] Pointer to a buffer containing a linked list of IP_ADAPTER_INFO structures.
pOutBufLen
[in, out] Pointer to the size, in bytes, of the buffer indicated by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, this function fills in the buffer with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW.

Return Values

Returns NO_ERROR if successful. If the function fails, it returns an error code. For a complete list of error codes, see Error Values or the SDK header file Winerror.h.

Requirements

OS Versions: Windows CE 3.0 and later.
Header: Iphlpapi.h.
Link Library: Iphlpapi.lib.

 

 

MSDN中關於IP_ADAPTER_INFO的定義:

This structure contains information about a particular network adapter on the local computer.

typedef struct _IP_ADAPTER_INFO {
  struct _IP_ADAPTER_INFO* Next;
  DWORD ComboIndex;
  Char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
  char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
  UINT AddressLength;
  BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
  DWORD Index;
  UINT Type;
  UINT DhcpEnabled;
  PIP_ADDR_STRING CurrentIpAddress;
  IP_ADDR_STRING IpAddressList;
  IP_ADDR_STRING GatewayList;
  IP_ADDR_STRING DhcpServer;
  BOOL HaveWins;
  IP_ADDR_STRING PrimaryWinsServer;
  IP_ADDR_STRING SecondaryWinsServer;
  time_t LeaseObtained;
  time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;

Members

Next
Pointer to the next adapter in the linked list of adapters.
ComboIndex
Reserved.
AdapterName
The name of the adapter.
Description
A description for the adapter.
AddressLength
The length of hardware address for the adapter.
Address
The hardware address for the adapter.
Index
The adapter index.
Type
The adapter type. The following list shows the adapter type values as they are defined in the header file IPIfCons.h:
  • MIB_IF_TYPE_OTHER     1
  • MIB_IF_TYPE_ETHERNET     6
  • MIB_IF_TYPE_TOKENRING     9
  • MIB_IF_TYPE_FDDI     15
  • MIB_IF_TYPE_PPP     23
  • MIB_IF_TYPE_LOOPBACK      24
  • MIB_IF_TYPE_SLIP      28
DhcpEnabled
TRUE if dynamic host configuration protocol (DHCP) is enabled for this adapter.
CurrentIpAddress
The current IP address for this adapter.
IpAddressList
The list of IP addresses associated with this adapter.
GatewayList
The IP address of the default gateway for this adapter.
DhcpServer
The IP address of the DHCP server for this adapter.
HaveWins
TRUE if this adapter uses Windows Internet Name Service (WINS).
PrimaryWinsServer
The IP address of the primary WINS server.
SecondaryWinsServer
The IP address of the secondary WINS server.
LeaseObtained
The time when the current DHCP lease was obtained.
LeaseExpires
The time when the current DHCP lease will expire.

具體用法:

 //獲取IP地址
 PIP_ADAPTER_INFO pAdapterInfo;
 PIP_ADAPTER_INFO pAdapter = NULL;
 DWORD dwRetVal = 0;
 ULONG ulOutBufLen;
 pAdapterInfo=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
 ulOutBufLen = sizeof(IP_ADAPTER_INFO);

 // 第一次調用GetAdapterInfo獲取ulOutBufLen大小
 if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
 {
  free(pAdapterInfo);
  pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
 }

 if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
  pAdapter = pAdapterInfo;
  while (pAdapter)
  {
   TRACE("Adapter Name: /t%s/n", pAdapter->AdapterName);
   TRACE("Adapter Desc: /t%s/n", pAdapter->Description);
   TRACE("MAC Addr: /t%02x-%02x-%02x-%02x-%02x-%02x/n",
    pAdapter->Address,
    pAdapter->Address,
    pAdapter->Address,
    pAdapter->Address,
    pAdapter->Address,
    pAdapter->Address);
   TRACE("IP Address: /t%s/n", pAdapter->IpAddressList.IpAddress.String);
   TRACE("IP Mask: /t%s/n", pAdapter->IpAddressList.IpMask.String);
   TRACE("Gateway: /t%s/n", pAdapter->GatewayList.IpAddress.String);
   pAdapter = pAdapter->Next;
  }
 }
 else
 {
  TRACE("Call to GetAdaptersInfo failed./n");
 }

 運行效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章