山寨:不是最好的,是最适合我们的!欢迎体验山寨 中文版MSDN

Blog @ Blog

当华美的叶片落尽,生命的脉络才历历可见。 -- 聂鲁达

常用链接

统计

积分与排名

BBS

Blog

Web

最新评论

熟悉WinpCap(一):获取设备列表

写在前面:
         参考http://www.coffeecat.net.cn/winpcap,最近在接触和熟悉WinpCap,并参考原来的代码,改写为VC6.0 环境的对话框工程,方便自己日后的使用,在此分享出来,希望能帮助一些需要熟悉WinpCap的朋友!
         通常,编写基于WinPcap应用程序的第一件事情,就是获得已连接的网络适配器列表。libpcap和WinPcap都提供了 pcap_findalldevs_ex() 函数来实现这个功能: 这个函数返回一个 pcap_if 结构的链表, 每个这样的结构都包含了一个适配器的详细信息。值得注意的是,数据域 namedescription 表示一个适配器名称和一个可以让人们理解的描述。
void CFindAllDevsDlg::OnBtnFindDevs() 
{
    
// TODO: Add your control notification handler code here
    int i=0;
    
char errBuf[PCAP_ERRBUF_SIZE];
    CString strErr;
    CString strDev;

    
/*获取本机设备列表*/
    
if(pcap_findalldevs(&AllDevs,errBuf) == -1)
    
{
        strErr.Format(
"Error in pcap_findalldevs_ex: %s",errBuf);
        AfxMessageBox(strErr);
        
return;
    }


    
/*打印列表*/
    
for(Dev=AllDevs; Dev!=NULL; Dev = Dev->next)
    
{
        
if(Dev->description)
        
{
            
//strDev.Format("%d.%s: %s",++i,Dev->name,Dev->description);
            strDev.Format("%d",++i);
            m_ctrlList.InsertItem((i
-1),strDev);
            strDev.Format(
"%s",Dev->name);
            m_ctrlList.SetItemText((i
-1),1,strDev);
            strDev.Format(
"%s",Dev->description);
            m_ctrlList.SetItemText((i
-1),2,strDev);
        }

            
        
else
        
{
            strDev.Format(
"%d.%s: %s",++i,Dev->name,"No description available");
            AfxMessageBox(strErr);
        }

            
    }

    
if(i == 0)
    
{
        strErr 
= "No interfaces found! Make sure WinPcap is installed";
        AfxMessageBox(strErr);
    }

    
/*不再需要设备列表了,释放它*/
     pcap_freealldevs(AllDevs);

}

有关这段代码的一些说明

首先, pcap_findalldevs() ,和其他libpcap函数一样,有一个 errbuf 参数。一旦发生错误,这个参数将会被libpcap写入字符串类型的错误信息。

第二要记住,不是所有的操作系统都支持libpcap提供的网络程序接口,因此,如果我们想编写一个可移植的应用程序,我们就必须考虑在什么情况下, description 是 null。本程序中,我们遇到这种情况时,会打印提示语句"No description available"。

最后要记住,当我们完成了设备列表的使用,我们要调用 pcap_freealldevs() 函数将其占用的内存资源释放。


差别描述:源代码使用pcap_findalldevs_ex()获取设备,但是在目前我配置的VC环境,提示找不到此函数,所以使用pcap_findalldevs()来实现。可以下载工程代码参考:/Files/Lee7/FindAllDevs.rar

posted on 2008-05-17 12:25 isabc 阅读(3221) 评论(8)  编辑 收藏 引用

评论

# re: 熟悉WinpCap(一):获取设备列表 2008-05-17 12:40 Gohan

以前用winpcap写过测网速的程序,很方便的库  回复  更多评论   

# re: 熟悉WinpCap(一):获取设备列表 2008-05-17 17:39 ViskerWong

这个WinPcap已经是一些网络应用程序的基础了,了解它有用  回复  更多评论   

# re: 熟悉WinpCap(一):获取设备列表 2009-03-31 09:08 虚拟主机

高手啊  回复  更多评论   

# re: 熟悉WinpCap(一):获取设备列表 2009-05-06 15:39 guest

以前用来写网络攻击程序的,小巧,灵活  回复  更多评论   

# re: 熟悉WinpCap(一):获取设备列表 2010-08-16 14:27 GwenJefferson

I took my first <a href="http://bestfinance-blog.com">loans</a> when I was not very old and this helped my relatives a lot. However, I require the sba loan once more time.   回复  更多评论   

# re: 熟悉WinpCap(一):获取设备列表 2012-07-02 08:33 essays writers

Don't know where to begin your academic assignment writing? Don't trouble, just because you got a possibility to buy research papers written (essayslab.com) well and focus on your personal deals.   回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


广告信息(免费广告联系)

中文版MSDN:
欢迎体验