呼,研究这个函数的时候发现我借来那本书上关于这个函数的范例是错的,后来去找MSDN的范例也有个地方感觉有误-_-深深的汗了一下,然后我在MSDN范例的基础上,几乎用上了IP_ADAPTER_INFO结构里面的所有成员,貌似在VC6.0里面没有这个函数,好友lewvan告诉我这个函数要Vs2003以上才有,嗯 必要的头文件和注释都在下面了,直接看吧
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>
#include <IPHlpApi.h>
#pragma comment(lib, "Iphlpapi.lib")
void PrintAdaptersInfo()
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
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 Index: \t %ld \n ",pAdapter->Index); //适配器索引
printf( "Adapter Name: \t %s \n ", pAdapter->AdapterName); //适配器名称
printf( "Adapter Desc: \t %s \n ", pAdapter->Description); //适配器描述
switch (pAdapter->Type) //适配器类型
{
case MIB_IF_TYPE_OTHER:
printf( "Adapter Type: \t Other \n ");
break;
case MIB_IF_TYPE_ETHERNET:
printf( "Adapter Type: \t Ethernet \n ");
break;
case MIB_IF_TYPE_TOKENRING:
printf( "Adapter Type: \t Tokenring \n ");
break;
case MIB_IF_TYPE_FDDI:
printf( "Adapter Type: \t FDDI \n ");
break;
case MIB_IF_TYPE_PPP:
printf( "Adapter Type: \t PPP \n ");
break;
case MIB_IF_TYPE_LOOPBACK:
printf( "Adapter Type: \t LoopBack \n ");
break;
case MIB_IF_TYPE_SLIP:
printf( "Adapter Type: \t Slip \n ");
break;
default:
printf( "Adapter Type: \t Unknow \n ");
}
printf( "MAC Addr \t "); //MAC地址
for (DWORD dw = 0;dw<pAdapter->AddressLength;dw++)
{
if (dw<pAdapter->AddressLength- 1)
{
printf( "%02X-",pAdapter->Address[dw]);
}
else
{
printf( "%02X \n ",pAdapter->Address[dw]);
}
}
//printf("Adapter Addr: \t%ld\n", pAdapter->Address); //MSDN原句,感觉有误,所以就改成上面那样子
printf( "IP Address: \t %s \n ", pAdapter->IpAddressList.IpAddress.String); //IP地址
printf( "IP Mask: \t %s \n ", pAdapter->IpAddressList.IpMask.String); //子网掩码
printf( "Gateway: \t %s \n ", pAdapter->GatewayList.IpAddress.String); //默认网关
if (pAdapter->DhcpEnabled)
{
printf( "DHCP Enabled: Yes \n ");
printf( " \t DHCP Server: \t %s \n ", pAdapter->DhcpServer.IpAddress.String); //DHCP服务器IP
printf( "Lease Obtained: %ld \n ", pAdapter->LeaseObtained); //当前DHCP租用时间
printf( "Lease Expires: %ld \n ", pAdapter->LeaseExpires); //当前DHCP租用期满时间
}
else
printf( "DHCP Enabled: No \n ");
if (pAdapter->HaveWins)
{
printf( "Have Wins: Yes \n ");
printf( " \t Primary Wins Server: \t %s \n ", pAdapter->PrimaryWinsServer.IpAddress.String); //wins服务器主IP
printf( " \t Secondary Wins Server: \t %s \n ", pAdapter->SecondaryWinsServer.IpAddress.String); //wins服务器副IP
}
else
printf( "Have Wins: No \n ");
pAdapter = pAdapter->Next;
}
}
else
{
printf( "Call to GetAdaptersInfo failed. \n ");
}
}
#include <stdio.h>
#include <WinSock2.h>
#include <IPHlpApi.h>
#pragma comment(lib, "Iphlpapi.lib")
void PrintAdaptersInfo()
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
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 Index: \t %ld \n ",pAdapter->Index); //适配器索引
printf( "Adapter Name: \t %s \n ", pAdapter->AdapterName); //适配器名称
printf( "Adapter Desc: \t %s \n ", pAdapter->Description); //适配器描述
switch (pAdapter->Type) //适配器类型
{
case MIB_IF_TYPE_OTHER:
printf( "Adapter Type: \t Other \n ");
break;
case MIB_IF_TYPE_ETHERNET:
printf( "Adapter Type: \t Ethernet \n ");
break;
case MIB_IF_TYPE_TOKENRING:
printf( "Adapter Type: \t Tokenring \n ");
break;
case MIB_IF_TYPE_FDDI:
printf( "Adapter Type: \t FDDI \n ");
break;
case MIB_IF_TYPE_PPP:
printf( "Adapter Type: \t PPP \n ");
break;
case MIB_IF_TYPE_LOOPBACK:
printf( "Adapter Type: \t LoopBack \n ");
break;
case MIB_IF_TYPE_SLIP:
printf( "Adapter Type: \t Slip \n ");
break;
default:
printf( "Adapter Type: \t Unknow \n ");
}
printf( "MAC Addr \t "); //MAC地址
for (DWORD dw = 0;dw<pAdapter->AddressLength;dw++)
{
if (dw<pAdapter->AddressLength- 1)
{
printf( "%02X-",pAdapter->Address[dw]);
}
else
{
printf( "%02X \n ",pAdapter->Address[dw]);
}
}
//printf("Adapter Addr: \t%ld\n", pAdapter->Address); //MSDN原句,感觉有误,所以就改成上面那样子
printf( "IP Address: \t %s \n ", pAdapter->IpAddressList.IpAddress.String); //IP地址
printf( "IP Mask: \t %s \n ", pAdapter->IpAddressList.IpMask.String); //子网掩码
printf( "Gateway: \t %s \n ", pAdapter->GatewayList.IpAddress.String); //默认网关
if (pAdapter->DhcpEnabled)
{
printf( "DHCP Enabled: Yes \n ");
printf( " \t DHCP Server: \t %s \n ", pAdapter->DhcpServer.IpAddress.String); //DHCP服务器IP
printf( "Lease Obtained: %ld \n ", pAdapter->LeaseObtained); //当前DHCP租用时间
printf( "Lease Expires: %ld \n ", pAdapter->LeaseExpires); //当前DHCP租用期满时间
}
else
printf( "DHCP Enabled: No \n ");
if (pAdapter->HaveWins)
{
printf( "Have Wins: Yes \n ");
printf( " \t Primary Wins Server: \t %s \n ", pAdapter->PrimaryWinsServer.IpAddress.String); //wins服务器主IP
printf( " \t Secondary Wins Server: \t %s \n ", pAdapter->SecondaryWinsServer.IpAddress.String); //wins服务器副IP
}
else
printf( "Have Wins: No \n ");
pAdapter = pAdapter->Next;
}
}
else
{
printf( "Call to GetAdaptersInfo failed. \n ");
}
}