Iphlpapi.h;ipHlpApi.lib;IPEXPORT.H;IPRTRMIB.H;IPTYPES.H
IP Helper 是一套用于管理本地网络设置的API。使用这一套API可以方便的改变计算机的网络设置或者提取有关的信息。而且它还提供了一种消息机制,能够在本地计算 机的网络设置发生改变时通知应用程序。实际上,它不仅仅能够提取本机的网络设置信息,还能够获得网络上其它计算机的IP使用情况和MAC地址。 -IP Helper is a set of API for the management of the local network settings. Using this set of APIs can easily change the computer' s network settings or extract the relevant information. It also provides a messaging mechanism to notify the application when the change in the local computer' s network settings. In fact, it is not only able to extract the information of the network settings, and also be able to get the IP use other computers on the network and MAC address.
本案例用到的资源,源码:http://download.csdn.net/detail/you_xian/6443819
#include<winsock2.h>
#include<Iphlpapi.h>
#include<stdio.h>
#pragma comment(lib,"Iphlpapi.lib")
int main()
{
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)
{
printf("Adapter Name: \t%s\n", pAdapter->AdapterName);
printf("Adapter Desc: \t%s\n", pAdapter->Description);
printf("MAC Addr: \t%02x-%02x-%02x-%02x-%02x-%02x\n",
pAdapter->Address[0],
pAdapter->Address[1],
pAdapter->Address[2],
pAdapter->Address[3],
pAdapter->Address[4],
pAdapter->Address[5]);
printf("IP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
printf("IP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
printf("Gateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
pAdapter = pAdapter->Next;
}
}
else
{
printf("Call to GetAdaptersInfo failed.\n");
}
}