欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VC++获取本机网卡及网络流量的方法

程序员文章站 2022-07-09 22:51:49
使用GetAdaptersInfo 和 GetIfTable获取本机网卡信息以及流量等信息。 #include "stdafx.h" #include...

使用GetAdaptersInfo 和 GetIfTable获取本机网卡信息以及流量等信息。

#include "stdafx.h"
#include   
#include   
#include   

using namespace std;
#pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库  


int main(int argc, char* argv[])
{
    PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();//PIP_ADAPTER_INFO结构体指针存储本机网卡信息  
    unsigned long stSize = sizeof(IP_ADAPTER_INFO);         //得到结构体大小,用于GetAdaptersInfo参数  
    int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);     //调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量  

    int netCardNum = 0;//记录网卡数量   
    int IPnumPerNetCard = 0; //记录每张网卡上的IP地址数量  

    if (ERROR_BUFFER_OVERFLOW == nRel)
    {
        //如果函数返回的是ERROR_BUFFER_OVERFLOW  
        //则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小  
        //这也是说明为什么stSize既是一个输入量也是一个输出量                 
        delete pIpAdapterInfo;//释放原来的内存空间       
        pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];//重新申请内存空间用来存储所有网卡信息          
        nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);    //再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量  
    }
    if (ERROR_SUCCESS == nRel)
    {
        //输出网卡信息  
        //可能有多网卡,因此通过循环去判断  
        while (pIpAdapterInfo)
        {
            cout << "网卡数量:" << ++netCardNum << endl;
            cout << "网卡名称:" << pIpAdapterInfo->AdapterName << endl;
            cout << "网卡描述:" << pIpAdapterInfo->Description << endl;
            switch (pIpAdapterInfo->Type)
            {
            case MIB_IF_TYPE_OTHER:
                cout << "网卡类型:" << "OTHER" << endl;
                break;
            case MIB_IF_TYPE_ETHERNET:
                cout << "网卡类型:" << "ETHERNET" << endl;
                break;
            case MIB_IF_TYPE_TOKENRING:
                cout << "网卡类型:" << "TOKENRING" << endl;
                break;
            case MIB_IF_TYPE_FDDI:
                cout << "网卡类型:" << "FDDI" << endl;
                break;
            case MIB_IF_TYPE_PPP:
                printf("PP\n");
                cout << "网卡类型:" << "PPP" << endl;
                break;
            case MIB_IF_TYPE_LOOPBACK:
                cout << "网卡类型:" << "LOOPBACK" << endl;
                break;
            case MIB_IF_TYPE_SLIP:
                cout << "网卡类型:" << "SLIP" << endl;
                break;
            default:
                break;
            }
            cout << "网卡MAC地址:";
            for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++)
                if (i < pIpAdapterInfo->AddressLength - 1)
                {
                    printf("%02X-", pIpAdapterInfo->Address[i]);
                }
                else
                {
                    printf("%02X\n", pIpAdapterInfo->Address[i]);
                }
            cout << "网卡IP地址如下:" << endl;
            //可能网卡有多IP,因此通过循环去判断  
            IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
            do
            {
                cout << "该网卡上的IP数量:" << ++IPnumPerNetCard << endl;
                cout << "IP 地址:" << pIpAddrString->IpAddress.String << endl;
                cout << "子网地址:" << pIpAddrString->IpMask.String << endl;
                cout << "网关地址:" << pIpAdapterInfo->GatewayList.IpAddress.String << endl;
                pIpAddrString = pIpAddrString->Next;
            } while (pIpAddrString);
            pIpAdapterInfo = pIpAdapterInfo->Next;
            cout << "--------------------------------------------------------------------" << endl;
        }
    }
    //释放内存空间  
    if (pIpAdapterInfo)
    {
        delete pIpAdapterInfo;
    }

    cout << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;

    MIB_IFTABLE   *pIfTable = NULL;
    ULONG          dwSize = 0;
    DWORD          dwRet;
    dwRet = GetIfTable(pIfTable, &dwSize, TRUE);
    if (dwRet == ERROR_INSUFFICIENT_BUFFER)
    {
        pIfTable = (MIB_IFTABLE *) new char[dwSize];
        if (pIfTable != NULL)
        {
            dwRet = GetIfTable(pIfTable, &dwSize, TRUE);
            if (dwRet == NO_ERROR)
            {
                printf("dwNumEntries = %u\n", pIfTable->dwNumEntries);
                for (int i = 0; i < pIfTable->dwNumEntries; i++)
                {
                    //MIB_IF_TYPE_ETHERNET ->以太网
                    if ((pIfTable->table[i]).dwType == MIB_IF_TYPE_ETHERNET && (pIfTable->table[i]).dwAdminStatus == 1 
                         && ((pIfTable->table[i].dwOperStatus) == MIB_IF_OPER_STATUS_OPERATIONAL))
                    {
                        printf("table[%1d].dwIndex = %u\n", i, (pIfTable->table[i]).dwIndex);
                        printf("\t dwType  = %u\n", (pIfTable->table[i]).dwType);
                        printf("\t dwSpeed = %u\n", (pIfTable->table[i]).dwSpeed);
                        printf("\t dwInByte = %u\n", (pIfTable->table[i]).dwInOctets);
                        printf("\t dwOutByte = %u\n", (pIfTable->table[i]).dwOutOctets);
                        printf("\t Descript = %s\n", (pIfTable->table[i]).bDescr); // friendly name

                        wcout << (pIfTable->table[i]).wszName << endl; // \DEVICE\TCPIP_{90B6A657-5D57-499F-ACC8-0F931B6B1ADC}, it contains netcard's name {xxxx-xxx-xxx}
                    }
                }
            }
            else
            {
                printf("Some error occured!\n");
            }
        }
        else
        {
            printf("Memory allocate failue\n");
        }
    }
    else
    {
        printf("Some error occured!\n");
    }

    system("pause");
    return 0;
}