猫博

猫也博客-c++ kernel ddk ifs
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如何用IPHelp取得网卡的详细信息

Posted on 2004-03-02 13:03  catlog  阅读(2791)  评论(0编辑  收藏  举报

IPHelp在Win98以后就被Windows支持,
是MS在WinSock上的再一层封装。
请下面的代码:

得到网卡列表:
//-----------------------------------------------------------------------
// GetAdapterLists
//-----------------------------------------------------------------------
BOOL CNetcfgDlg::GetAdapterLists(CComboBox &cmdAdapter)
{
 bool bRet = false;
 DWORD Err;
 PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
    DWORD AdapterInfoSize;
 CString csTmp;
 UINT nIndex = 0;
 
 cmdAdapter.Clear();
    //
    // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure.
    // Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.
    //
    AdapterInfoSize = 0;
    if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
    {
        if (Err != ERROR_BUFFER_OVERFLOW)
        {           
            return false;
        }
    }

    // Allocate memory from sizing information
    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
    {       
        return false;
    }

    // Get actual adapter information
    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
    {       
        return false;
    }

    pAdapt = pAdapterInfo;
  
 //m_AdapterList.push_back(*pAdapt);
    while (pAdapt)
    {               
  csTmp.Format("%d\t%s",nIndex,pAdapt->Description);
  cmdAdapter.AddString(csTmp);
  //m_AdapterList.push_back(*pAdapt);
  cmdAdapter.SetItemDataPtr(nIndex,pAdapt);
  nIndex++;

  pAdapt = pAdapt->Next;
    }
 
}

显示单个网卡的信息:

 csTmp.Format("适配器GUID:%s\n",pAdapt->AdapterName);
 csAdpInfo += csTmp;

 csTmp.Format("名称描述:%s\n",pAdapt->Description);
 csAdpInfo += csTmp;

 char buf[2];
 CString csBuf = "";
 for (UINT i=0; i<pAdapt->AddressLength; i++)
 {
  if (i == (pAdapt->AddressLength - 1))
   sprintf(buf,"%.2X",(int)pAdapt->Address[i]);
  else
   sprintf(buf,"%.2X-",(int)pAdapt->Address[i]);

  csBuf += buf;
 }    
 csTmp.Format("网卡物理地址(MAC):%s\n",csBuf);
 csAdpInfo += csTmp;

 csTmp.Format("DHCP: %s\n", (pAdapt->DhcpEnabled ? "允许" : "不允许")); 
 csAdpInfo += csTmp;

 pAddrStr = &(pAdapt->IpAddressList);
 while(pAddrStr)
 {
  csTmp.Format("IP地址:%s\n",pAddrStr->IpAddress.String);
  csAdpInfo += csTmp;
  csTmp.Format("子网掩码:%s\n",pAddrStr->IpMask.String);
  csAdpInfo += csTmp;  
  pAddrStr = pAddrStr->Next;
 }

 
 csTmp.Format("默认网关:%s\n",pAdapt->GatewayList.IpAddress.String);
 csAdpInfo += csTmp;
 
 csTmp.Format("DHCP服务器 :%s\n",pAdapt->DhcpServer.IpAddress.String);
 csAdpInfo += csTmp;
 
 csTmp.Format("主WINS服务器:%s\n",pAdapt->PrimaryWinsServer.IpAddress.String);
 csAdpInfo += csTmp;

 csTmp.Format("从WINS服务器:%s\n",pAdapt->SecondaryWinsServer.IpAddress.String);
 csAdpInfo += csTmp;
 
 SetDlgItemText(IDC_ADPINFO,csAdpInfo);