永发信息网

WinPcap获取网络接口列表的问题

答案:1  悬赏:30  手机版
解决时间 2021-02-26 20:05
  • 提问者网友:川水往事
  • 2021-02-26 01:26
我用WinPcap的pcap_findalldevs()函数获得网络接口列表

结果返回了三个接口,其中第三个才是我的网卡

但我的机只有一个网卡,请问是怎么回事?

怎么知道当前计算机使用的是哪个接口呢?
------------------------------------
在C++版块提了一个相同的问题,但到现在还没人回答,所以在VC版块重复一遍^_^

http://zhidao.baidu.com/question/45975962.html
最佳答案
  • 五星知识达人网友:等灯
  • 2021-02-26 03:02
WinPcap的pcap_findalldevs()函数我个人感觉不稳定,我比较喜欢用IP Help API获得网卡名称再用Pcap操作

假设有结构体
typedef struct NicItem
{
int nItem;
char name[100];
char description[100];
}NICTIEM;
在来个CArray,有2张网卡以上就加入CArray
CArray <NICITEM,NICITEM&> arrNicItem;

typedef DWORd(CALLBACK *PGAINFO)(PIP_ADAPTER_INFO,PULONG);
PGAINFO pGAInfo;
//加载IP Helper API 所需的库文件
HINSTANCE hInst;//实例句柄
hInst=LoadLibrary("iphlpapi.dll");
if(!hInst)
AfxMessageBox("iphlpapi.dll not supported in this platform!\n");
pGAInfo=(PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo");
////------------------------------------》获得网卡数据
PIP_ADAPTER_INFO pInfo=NULL, pInfoTemp=NULL;
ULONG ulSize=0;
pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小
pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
if( pGAInfo(pInfo,&ulSize) != ERROR_SUCCESS)
return;

DWORD dwLocalIP = 0;
for( ; pInfo; pInfo=pInfo->Next)
{
Device->AddString(pInfo->Description);

NICITEM nic;
nic.nItem = pInfo->Index;
strcpy_s(nic.name, "\\Device\\NPF_"); //加上前缀以被PCAP识别
strcat_s(nic.name, pInfo->AdapterName);
strcpy_s(nic.description, pInfo->Description);

arrNicItem.Add(nic);
}
delete pInfoTemp;

然后用pcap做你想做的事就行了
比如

pcap_t *m_fp;
char errbuf[PCAP_ERRBUF_SIZE] = "";
//打开网卡
if(!(m_fp = pcap_open_live(arrNicItem.GetAt().name,// name of the device
256, // portion of the packet to capture, max 65536
0, // promiscuous mode closed
10, // read timeout
errbuf))) // error buffer
{
...
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯