现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

VC读取网卡信息

2012-07-08 22:54 工业·编程 ⁄ 共 2080字 ⁄ 字号 暂无评论

    网络编程中经常需要获得类似于路由信息,ip信息之类的东西。下面的这个函数可以实现读取网卡信息:

void MASKinfo()
{
PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0;
DWORD index=0;
CString str;
    pAdapterInfo = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    dwRetVal = GetAdaptersInfo(pAdapterInfo,&ulOutBufLen);
    if (dwRetVal==ERROR_BUFFER_OVERFLOW)
    {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO*)malloc(ulOutBufLen);
        dwRetVal = GetAdaptersInfo(pAdapterInfo,&ulOutBufLen);
    }
    if (dwRetVal==NO_ERROR)
    {
        pAdapter = pAdapterInfo;
        while (pAdapter)
        {
            printf("适配器名称:/t% s/n",pAdapter->AdapterName);
   printf("适配器索引号:/t% s/n",pAdapter->Index);
            printf("适配器描述信息:/t% s/n",pAdapter->Description);
            printf("MAC地址:/t% 02X:% 02X:% 02X:% 02X:% 02X:% 02X/n",pAdapter->Address[0],pAdapter->Address[1],
    pAdapter->Address[2],pAdapter->Address[3],pAdapter->Address[4],pAdapter->Address[5]);
            printf("IP地址:/t% s/n",pAdapter->IpAddressList.IpAddress.String);
   printf("子网掩码:/t% s/n",pAdapter->IpAddressList.IpMask.String);
   printf("网关地址:/t% s/n",pAdapter->GatewayList.IpAddress.String);
   str.Format("% s", pAdapter->IpAddressList.IpAddress.String);//则str里面存储的就是IP地址
   index=pAdapter->Index;
   CString   strindex;   
   if (pAdapter->DhcpEnabled)
            {
                printf("DHCP enabled: yes/n");
                printf("DHCP服务器:/t% s/n",pAdapter->DhcpServer.IpAddress.String);
                printf("租约:% ld/n",pAdapter->LeaseObtained);
            }
            else
            {
                printf("DHCP enabled:no/n");
            }
            if (pAdapter->HaveWins)
            {
                printf("Have Wins:Yes/n");
                printf("Primary Wins Server:/t% s/n",pAdapter->PrimaryWinsServer.IpAddress.String);
                printf("Secondary Server:/t% s/n",pAdapter->SecondaryWinsServer.IpAddress.String);
            }
            else
            {
                printf("Have Wins:No/n");
            }
            pAdapter = pAdapter->Next;
        }
}
    else
    {
        printf("失败/n");
    }
}

注意使用上面这个函数的时候要添加头文件

#include   "Iphlpapi.h"

给我留言

留言无头像?