第二篇 获取本机网络配置信息及ARP包的发送
1、获取本机网络配置信息
/*--------------------------------------------------------
利用IP帮助函数获取本机网络适配器名称、
MAC地址、IP地址、子网掩码及广播地址等
---------------------------------------------------------*/
代码如下:(说明:俺没查到为什么不能获得子网掩码,总是提示:IpMask' : undeclared identifier)
PIP_ADAPTER_INFO ad_info;
DWORD uLen=0; //DWORD和ULONG意义相同啊,与unsigned long区别,俺不知道啊 //但曾经吃过亏啊
PIP_ADDR_STRING pAddrStr;
//int nitem;
/*-----------------------------------------------------------------
在网上搜到的结构体的一定,俺的MSDN上查不到啊
struct _IP_ADAPTER_INFO 中结构体的定义
typedef struct _IP_ADDR_STRING {
struct _IP_ADDR_STRING* Next;
IP_ADDRESS_STRING IpAddress;
IP_MASK_STRING IpMask;
DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;
typedef struct {
char String[4 * 4];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;
--------------------------------------------------------------------*/
/*--------------------------------------------------
参数说明:m_adapt_info为列表控件对象
---------------------------------------------------*/
//一定为ad_info分配空间哦!
ad_info=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
if(GetAdaptersInfo(ad_info,&uLen)==ERROR_BUFFER_OVERFLOW)
{
TRACE("%ld\n",uLen);
ad_info=(PIP_ADAPTER_INFO)malloc(uLen);
}
if(GetAdaptersInfo(ad_info,&uLen)==ERROR_SUCCESS)
{
while(ad_info)
{
int nitem;
switch (ad_info->Type)
{
case MIB_IF_TYPE_ETHERNET:
nitem=m_adapt_info.InsertItem(0,"Ethernet adapter");
break;
case MIB_IF_TYPE_TOKENRING:
nitem=m_adapt_info.InsertItem(0,"Token Ring adapter");
break;
case MIB_IF_TYPE_FDDI:
nitem=m_adapt_info.InsertItem(0,"FDDI adapter");
break;
case MIB_IF_TYPE_PPP:
nitem=m_adapt_info.InsertItem(0,"PPP adapter");
break;
case MIB_IF_TYPE_LOOPBACK:
nitem=m_adapt_info.InsertItem(0,"Loopback adapter");
break;
case MIB_IF_TYPE_SLIP:
nitem=m_adapt_info.InsertItem(0,"Slip adapter");
break;
case MIB_IF_TYPE_OTHER:
default:
nitem=m_adapt_info.InsertItem(0,"Other adapter ");
}
m_adapt_info.SetItemText(nitem,1,ad_info->AdapterName);//网卡名称
CString string;
string.Format("%2x-%2x-%2x-%2x-%2x-%2x",ad_info->Address[0],ad_info->Address[1],
ad_info->Address[2],ad_info->Address[3],ad_info->Address[4],ad_info->Address[5]);
m_adapt_info.SetItemText(nitem,2,string); //网卡MAC地址
m_adapt_info.SetItemText(nitem,3,ad_info->DhcpEnabled ? "yes" : "no"); //DHCP是否可 //用
pAddrStr=&(ad_info->IpAddressList);
CString str1,str2;
while(pAddrStr)
{
str1+=" ";
str2+=" ";
str1+=pAddrStr->IpAddress.String;
str2+=pAddrStr>IpMask.String; //有该句总是提示:IpMask' : undeclared identifier
//不知道为啥呀
pAddrStr=pAddrStr->Next;
}
m_adapt_info.SetItemText(nitem,4,str1); //网卡对应的IP地址
m_adapt_info.SetItemText(nitem,5,str2); //对应的子网掩码
CString str3=ad_info->GatewayList.IpAddress.String;
pAddrStr = ad_info->GatewayList.Next;
while(pAddrStr)
{
str3+=" ";
str3+=pAddrStr->IpAddress.String;
pAddrStr=pAddrStr->Next;
}
m_adapt_info.SetItemText(nitem,6,str3); //默认网关
m_adapt_info.SetItemText(nitem,7,ad_info->DhcpServer.IpAddress.String); //DHCP服务 //器IP地址
ad_info=ad_info->Next ;
}
free(ad_info);
}
1、获取本机网络配置信息
/*--------------------------------------------------------
利用IP帮助函数获取本机网络适配器名称、
MAC地址、IP地址、子网掩码及广播地址等
---------------------------------------------------------*/
代码如下:(说明:俺没查到为什么不能获得子网掩码,总是提示:IpMask' : undeclared identifier)
PIP_ADAPTER_INFO ad_info;
DWORD uLen=0; //DWORD和ULONG意义相同啊,与unsigned long区别,俺不知道啊 //但曾经吃过亏啊
PIP_ADDR_STRING pAddrStr;
//int nitem;
/*-----------------------------------------------------------------
在网上搜到的结构体的一定,俺的MSDN上查不到啊
struct _IP_ADAPTER_INFO 中结构体的定义
typedef struct _IP_ADDR_STRING {
struct _IP_ADDR_STRING* Next;
IP_ADDRESS_STRING IpAddress;
IP_MASK_STRING IpMask;
DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;
typedef struct {
char String[4 * 4];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;
--------------------------------------------------------------------*/
/*--------------------------------------------------
参数说明:m_adapt_info为列表控件对象
---------------------------------------------------*/
//一定为ad_info分配空间哦!
ad_info=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
if(GetAdaptersInfo(ad_info,&uLen)==ERROR_BUFFER_OVERFLOW)
{
TRACE("%ld\n",uLen);
ad_info=(PIP_ADAPTER_INFO)malloc(uLen);
}
if(GetAdaptersInfo(ad_info,&uLen)==ERROR_SUCCESS)
{
while(ad_info)
{
int nitem;
switch (ad_info->Type)
{
case MIB_IF_TYPE_ETHERNET:
nitem=m_adapt_info.InsertItem(0,"Ethernet adapter");
break;
case MIB_IF_TYPE_TOKENRING:
nitem=m_adapt_info.InsertItem(0,"Token Ring adapter");
break;
case MIB_IF_TYPE_FDDI:
nitem=m_adapt_info.InsertItem(0,"FDDI adapter");
break;
case MIB_IF_TYPE_PPP:
nitem=m_adapt_info.InsertItem(0,"PPP adapter");
break;
case MIB_IF_TYPE_LOOPBACK:
nitem=m_adapt_info.InsertItem(0,"Loopback adapter");
break;
case MIB_IF_TYPE_SLIP:
nitem=m_adapt_info.InsertItem(0,"Slip adapter");
break;
case MIB_IF_TYPE_OTHER:
default:
nitem=m_adapt_info.InsertItem(0,"Other adapter ");
}
m_adapt_info.SetItemText(nitem,1,ad_info->AdapterName);//网卡名称
CString string;
string.Format("%2x-%2x-%2x-%2x-%2x-%2x",ad_info->Address[0],ad_info->Address[1],
ad_info->Address[2],ad_info->Address[3],ad_info->Address[4],ad_info->Address[5]);
m_adapt_info.SetItemText(nitem,2,string); //网卡MAC地址
m_adapt_info.SetItemText(nitem,3,ad_info->DhcpEnabled ? "yes" : "no"); //DHCP是否可 //用
pAddrStr=&(ad_info->IpAddressList);
CString str1,str2;
while(pAddrStr)
{
str1+=" ";
str2+=" ";
str1+=pAddrStr->IpAddress.String;
str2+=pAddrStr>IpMask.String; //有该句总是提示:IpMask' : undeclared identifier
//不知道为啥呀
pAddrStr=pAddrStr->Next;
}
m_adapt_info.SetItemText(nitem,4,str1); //网卡对应的IP地址
m_adapt_info.SetItemText(nitem,5,str2); //对应的子网掩码
CString str3=ad_info->GatewayList.IpAddress.String;
pAddrStr = ad_info->GatewayList.Next;
while(pAddrStr)
{
str3+=" ";
str3+=pAddrStr->IpAddress.String;
pAddrStr=pAddrStr->Next;
}
m_adapt_info.SetItemText(nitem,6,str3); //默认网关
m_adapt_info.SetItemText(nitem,7,ad_info->DhcpServer.IpAddress.String); //DHCP服务 //器IP地址
ad_info=ad_info->Next ;
}
free(ad_info);
}