18,356
社区成员
发帖
与我相关
我的任务
分享
CComboBox NIC_List; //为CComboBox 声明一个对象(头文件中声明)
pcap_if_t *alldevs; // 保存网卡信息(一个结构体)
pcap_if_t *temp; // 在输出信息的地方做为中间变量,递增
pcap_t *p_pcap; //pcap_t结构体,保存一个已打开接口的详细信息,该结构体的结构不对用户公开,只是通过一个句柄来传递关于接口的参数。
char error_buf[PCAP_ERRBUF_SIZE];
// 获取网络设备列表
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, error_buf) == -1)
{
str.Format(_T("pcap_findalldevs运行错误: %s\n"), error_buf);
MessageBox( str,_T("错误!"),MB_ICONERROR);
return FALSE;
}
// 输出设备列表
for(temp=alldevs; temp; temp=temp->next)
{
if(temp->description)
str.Format(_T("%s"), temp->description);
else if(temp->name)
str.Format(_T("%s"),temp->name);
else
str.Format(_T("无可用网络设备信息"));
i++;
NIC_List.AddString(str);
}
if(i==0)
{
MessageBox(_T("找不到接口! 请确认您的Winpcap驱动是否安装."),_T("错误!"),MB_ICONERROR);
return
}
NIC_List.SetCurSel(0);
int i;
for(temp=alldevs, i=0; i< NIC_select ;temp=temp->next, i++)
{
i=i;
}
if ( (p_pcap= pcap_open(temp->name, // 设备名
100, // 获取包的字节数(仅获取包的前100 bytes)
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous 模式,即适配器(网卡)为混杂模式
1000, // read timeout
NULL, // authentication on the remote machine
error_buf // error buffer
) ) == NULL)
{
MessageBox(_T("不能打开网络适配器,WinPcap不支持此设备!"),_T("错误!"),MB_ICONERROR);
return;
}