获取网络适配器

条件:

获取网络适配器需要WinpCap开发平台。平台下载地址:http://www.winpcap.org

本人用的开发平台:WpdPack_4_1_2

文件预览如下图:

Include里的文件如下:

Lib里的文件如下:

使用开发工具:vc++ 6.0

写程序之前,需要对vc++ 6.0做一些配置。以下是详细的配置过程:

1,打开 工程->>设置->>C/C++,将 WPCAP,HAVE_REMOTE 添加到预处理程序定义后,点确定。如图:

  

2,打开 工具->>选项->>目录->>选择 include files   -->>将WpdPack4_1_2工具包里include文件夹添加进去。如图:

  

  用同样的方法,将WpdPack4_1_2工具包里Lib文件夹添加进去。如图:

  

开发环境配置好后,就可以写代码了。

请新建一个控制台工程。我这里的工程名为 获取网络适配器

获取网络适配器.cpp 的内容如下:

#include "stdafx.h"


#define LINE_LEN 16

int main(int argc, char **argv)
{   
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];
    
    /* 获取本地机器设备列表 */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs_ex: %s
", errbuf);
        exit(1);
    }
    
    /* 打印列表 */
    for(d= alldevs; d != NULL; d= d->next)
    {
        printf("%d. %s
", ++i, d->name);
        if (d->description)
            printf("  描述:%s

", d->description);
        else
            printf("  没有可用的描述!

");
    }
    
    if (i == 0)
    {
        printf("
No interfaces found! Make sure WinPcap is installed.
");
        return 0;
    }

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

    return 0;
}

stdafx.h的内容如下:

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__9DD59F57_836B_4B5A_B16B_499430950854__INCLUDED_)
#define AFX_STDAFX_H__9DD59F57_836B_4B5A_B16B_499430950854__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>
// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__9DD59F57_836B_4B5A_B16B_499430950854__INCLUDED_)

工程截图:

运行截图:

本人希望更多的人受益。此文可以随便转载

原文地址:https://www.cnblogs.com/dzqdzq/p/3287458.html