当前位置: 首页 > 学习 > 电脑学习 > 程序设计 > C++ > 网络通讯 > 正文

获取网卡的相关信息

http://www.zk168.com.cn  招考学习网 2006-4-11 5:59:47
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
作者姓名 安静
文章原始出处 北京163社区 C版


////////////////////////////////////////////////////////////////
//运行本程序需要安装Microsoft Platform SDK .这个可以去MS下载
// http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
//作者:安静
//mail: aweiken@163.com
//10:56 2002-1-31 start
//14:44 2002-2-2  fix some bug.
//////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include <iostream>
#include <string.h>
#include "Iphlpapi.h"

using namespace std;


PIP_ADAPTER_INFO pinfo=NULL;
unsigned long len=0;

CString macaddress;
CString    description;
CString    type;
CString    subnet;
CString    IpAddress;
CString    gateway;
CString    PrimaryWinsServer;
CString    dhcp;

void GetInfo();
void ParseData();
void OutPut(CString str);

void main()
{
    
    
    cout<<"------------------------网卡信息检测---------------------\n";
    GetInfo();
    
}

void GetInfo()
{
    if (pinfo!=NULL)
        delete (pinfo);
    unsigned  long nError;
    nError    =    GetAdaptersInfo(pinfo,&len);  //1??üoˉêy
    if (nError==0)
    {
        ParseData();            
    }
    if (nError==ERROR_NO_DATA)
    {
        OutPut("没有网络设备信息");
    }
    if (nError==ERROR_NOT_SUPPORTED)
    {
        OutPut("GetAdaptersInfo不支持本系统");
    }
    if (nError==ERROR_BUFFER_OVERFLOW)
    {
        pinfo= (PIP_ADAPTER_INFO)malloc(len);
        nError    =    GetAdaptersInfo(pinfo,&len);
        if (nError==0)
        {
            ParseData();            
        }
    }
    
    return;
}
void ParseData()
{

    if (pinfo!=NULL)
    {
        macaddress.Format("%02X:%02X:%02X:%02X:%02X:%02X",pinfo->Address[0],pinfo->Address[1],pinfo->Address[2],pinfo->Address[3],pinfo->Address[4],pinfo->Address[5]);
        description    =    pinfo->Description;
        type.Format("%d",pinfo->Type);
        
         PIP_ADDR_STRING pAddressList = &(pinfo->IpAddressList);
         IpAddress    ="";
          do
          {
              IpAddress    +=    pAddressList->IpAddress.String;
             pAddressList = pAddressList->Next;
             if (pAddressList != NULL)
                IpAddress    +="\r\n";
          } while (pAddressList != NULL);

        subnet.Format("%s",pinfo->IpAddressList.IpMask.String);
        gateway.Format("%s",pinfo->GatewayList.IpAddress.String);
        if (pinfo->HaveWins)
            PrimaryWinsServer.Format("%s",pinfo->PrimaryWinsServer.IpAddress.String );
        else
            PrimaryWinsServer.Format("%s","N/A" );
        if (pinfo->DhcpEnabled )
            dhcp.Format("%s",pinfo->DhcpServer.IpAddress.String );
        else
            dhcp.Format("%s","N/A");
        pinfo = pinfo->Next;
    }
    OutPut("网络设备为:\t"+description);    
    OutPut("Mac地址为:\t"+macaddress);
    OutPut("网卡类型:\t"+type);
    OutPut("IP地址:\t"+IpAddress);
    OutPut("子网掩码:\t"+subnet);
    OutPut("网关:\t"+gateway);
    OutPut("主Wins服务器:\t"+PrimaryWinsServer);
    OutPut("dhcp服务器:\t"+dhcp);




}

void OutPut(CString str)
{
    cout<<str.GetBuffer(str.GetLength()) <<endl;
}

-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>