获得网络接口信息和网卡信息

来源:互联网 发布:网络代理 免费 编辑:程序博客网 时间:2024/05/19 17:47

更多精彩内容,请见:http://www.16boke.com

网络适配器一般指网卡。

网络接口指的网络设备的各种接口,我们现今正在使用的网络接口都为以太网接口。它遵循IEEE802.3标准。
通过GetIfTable2函数可以获取网络接口信息。
通过GetAdaptersInfo可以获取网卡信息。
本文只打印了一部分信息,可以根据IP_ADAPTER_INFO结构体和MIB_IF_ROW2结构体获取自己需要的数据。
网卡信息经ipconfig和注册表可以验证位于:HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters\Adapters\
网络接口信息不知道怎么验证是否正确。有知道的,希望不吝赐教,非常感谢!

全部代码如下:
#include <stdio.h>#include <winsock2.h>#include <ws2tcpip.h>#include <iphlpapi.h>#include <locale.h>#pragma comment(lib, "iphlpapi.lib")static int nIndex = 0;//打印网卡信息void printAdapterInfo(PIP_ADAPTER_INFO pIpAdapterInfo){    char temp[MAX_PATH] = {};    nIndex = 0;    while (pIpAdapterInfo)//可能有多网卡    {        nIndex++;        sprintf(temp,"%d", nIndex);        OutputDebugStringA(temp);        OutputDebugString(L"\t网卡索引:");        sprintf(temp,"%d", pIpAdapterInfo->Index);        OutputDebugStringA(temp);                OutputDebugString(L"\n");        OutputDebugString(L"\t网卡名称:");        OutputDebugStringA(pIpAdapterInfo->AdapterName);        OutputDebugString(L"\n\t网卡描述:");        OutputDebugStringA(pIpAdapterInfo->Description);        OutputDebugString(L"\n\t网卡MAC地址:");                for (UINT i =0; i < pIpAdapterInfo->AddressLength; i++)        {            if (i==pIpAdapterInfo->AddressLength-1)                sprintf(temp,"%02x\n", pIpAdapterInfo->Address[i]);            else                sprintf(temp,"%02x-", pIpAdapterInfo->Address[i]);            OutputDebugStringA(temp);        }        OutputDebugString(L"\t网卡IP地址如下:");        //可能网卡有多IP        IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);        do        {            OutputDebugString(L"\t");            OutputDebugStringA(pIpAddrString->IpAddress.String);            pIpAddrString = pIpAddrString->Next;        } while (pIpAddrString);        OutputDebugString(L"\n\n");        pIpAdapterInfo = pIpAdapterInfo->Next;            }    return;}//打印网络接口信息void printNetworkInterface(PMIB_IF_ROW2 pIfRow){    int iRet = 0;    WCHAR GuidString[40] = { 0 };    WCHAR temp[MAX_PATH] = {0};    unsigned int j;    nIndex++;    wsprintf(temp,L"%d", nIndex);    OutputDebugString(temp);    OutputDebugString(L"  接口索引:\t ");    wsprintf(temp,L"%lu\n", pIfRow->InterfaceIndex);    OutputDebugString(temp);    iRet = StringFromGUID2(pIfRow->InterfaceGuid, (LPOLESTR) & GuidString, 39);    if (iRet == 0)        OutputDebugString(L"得到接口GUID失败\n");    else     {        OutputDebugString(L"\t接口GUID:   ");        OutputDebugString(GuidString);    }    OutputDebugString(L"\n\t别名:\t\t ");    OutputDebugString(pIfRow->Alias);    OutputDebugString(L"\n\t描述:\t\t ");    OutputDebugString(pIfRow->Description);    OutputDebugString(L"\n\tMAC地址:\t    ");    if (pIfRow->PhysicalAddressLength == 0)        OutputDebugString(L"\n");    for (j = 0; j < (int) pIfRow->PhysicalAddressLength; j++) {        if (j == (pIfRow->PhysicalAddressLength - 1))            wsprintf(temp,L"%.2X\n", (int) pIfRow->PhysicalAddress[j]);        else            wsprintf(temp,L"%.2X-", (int) pIfRow->PhysicalAddress[j]);        OutputDebugString(temp);    }    }int main(){    PMIB_IF_TABLE2  m_pIfTable; //网络接口项    PMIB_IF_ROW2  m_pIfRow;     //网络接口信息    IP_ADAPTER_INFO  *info_top; //PIP_ADAPTER_INFO结构体指针存储本机网卡信息    ULONG info_size;    UINT ret;    setlocale(LC_CTYPE, ".936");    if(GetIfTable2(&m_pIfTable) == NO_ERROR)     {        OutputDebugString(L"------------开始------------\n");        for (int i = 0; i < (int) m_pIfTable->NumEntries; i++)         {                OutputDebugString(L"----------------------------------\n");            m_pIfRow =  &m_pIfTable->Table[i];            printNetworkInterface(m_pIfRow);                    }               }    else    {        OutputDebugString(L"GetIfTable2(&m_pIfTable) == NO_ERROR:else:");    }    info_top = new IP_ADAPTER_INFO();    info_size = sizeof(IP_ADAPTER_INFO);    ret = GetAdaptersInfo(info_top, &info_size);    if (ERROR_BUFFER_OVERFLOW == ret)    {        //GetAdaptersInfo参数传递的内存空间不够,info_size,表示需要的空间大小        //释放原来的内存空间,重新申请内存空间        if(info_top)        {            delete info_top;            info_top = NULL;        }        info_top = (PIP_ADAPTER_INFO)new BYTE[info_size];        ret = GetAdaptersInfo(info_top, &info_size);      }    printAdapterInfo(info_top);    if(info_top)    {        delete info_top;        info_top = NULL;    }    getchar();    return 0;}

由于本人网络知识有限,有错误之处,欢迎指正,谢谢!

更多精彩内容,请见:http://www.16boke.com

0 0
原创粉丝点击