VC知識庫BLOG-寧靜致遠-取得網路卡資訊(轉)

chief1985發表於2008-10-12
導讀:



//使用ATL
#pragma once


#include "atlstr.h"
#include "iphlpapi.h"
#pragma comment(lib,"iphlpapi.lib")



class CDnpNetworkInterface
{
 _MIB_IFTABLE* _pInfoBuffer;



protected:



 bool GetNicInformation(void)
 {
  DWORD ret;
  ULONG dwSize;


  dwSize = 0;
  ret = ::GetIfTable(NULL,&dwSize,TRUE);
  if(ret != NO_ERROR && ret != ERROR_INSUFFICIENT_BUFFER)
   return false;


  if(_pInfoBuffer)
   delete (BYTE*)_pInfoBuffer;


  _pInfoBuffer = (_MIB_IFTABLE*)new BYTE[dwSize];
  if(_pInfoBuffer == NULL)
   return false;


  ZeroMemory(_pInfoBuffer,dwSize);
  ret = ::GetIfTable(_pInfoBuffer,&dwSize,TRUE);
  if(ret != NO_ERROR)
  {
   delete (BYTE*)_pInfoBuffer;
   _pInfoBuffer = NULL;
   return false;
  }


  return true;
 }


 bool IsValidInfo(void)
 {
  if(_pInfoBuffer)
   return true;


  return GetNicInformation();
 }


public:


 CDnpNetworkInterface()
 {
  _pInfoBuffer = NULL;
 }


 ~CDnpNetworkInterface()
 {
  if(_pInfoBuffer)
   delete (BYTE*)_pInfoBuffer;
 }



 
 DWORD GetNicCount(void)
 {
  if(IsValidInfo() == false)
   return 0;


  return _pInfoBuffer->dwNumEntries;
 }



 bool GetNicName(DWORD dwIndex,CAtlString* pstrName)
 {
  if(pstrName == NULL)
   return false;
  *pstrName = _T("");
  if(dwIndex >= GetNicCount())
   return false;


  *pstrName = (char*)_pInfoBuffer->table[dwIndex].bDescr;


  return true;
 }



 bool GetNicMacAddress(DWORD dwIndex,CAtlString* pstrMacAddress,int nFormat=0)
 {
  if(pstrMacAddress == NULL)
   return false;
  *pstrMacAddress = _T("");
  if(dwIndex >= GetNicCount())
   return false;


  switch(nFormat)
  {
  case 1:
   pstrMacAddress->Format(_T("%02X %02X %02X %02X %02X %02X")
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[0]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[1]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[2]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[3]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[4]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[5]);
   break;


  case 2:
   pstrMacAddress->Format(_T("%02X%02X%02X%02X%02X%02X")
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[0]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[1]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[2]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[3]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[4]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[5]);
   break;


  case 0:
  default:
   pstrMacAddress->Format(_T("%02X-%02X-%02X-%02X-%02X-%02X")
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[0]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[1]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[2]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[3]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[4]
     ,_pInfoBuffer->table[dwIndex].bPhysAddr[5]);
   break;
  }


  return true;
 }



 
 DWORD GetNicConnectionSpeed(DWORD dwIndex)
 {
  if(dwIndex >= GetNicCount())
   return 0;


  return _pInfoBuffer->table[dwIndex].dwSpeed;
 }



 bool IsNicWorking(DWORD dwIndex)
 {
  if(dwIndex >= GetNicCount())
   return false;


  return (_pInfoBuffer->table[dwIndex].dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL) ? true : false;
 }


};



//使用方法
void Test(void)
{
 bool  ret;
 DWORD  i;
 DWORD  dwSize;
 DWORD  dwSpeed;
 CAtlString strName;
 CAtlString strMacAddress;
 CAtlString strBuff;
 CAtlString strMessage;
 CDnpNetworkInterface cDevice;


 dwSize = cDevice.GetNicCount();
 for(i = 0; i < dwSize; i++)
 {
  ret = cDevice.GetNicName(i,&strName);
  if(ret == false)
   continue;


  ret = cDevice.GetNicMacAddress(i,&strMacAddress);
  if(ret == false)
   continue;


  dwSpeed = cDevice.GetNicConnectionSpeed(i);


  strBuff.Format(_T("描述:%s/nMAC地址:%s/n連線速度:%dKbps/n"),strName,strMacAddress,dwSpeed/1000);
  strMessage += strBuff;
  if(cDevice.IsNicWorking(i))
   strMessage += _T("工作:正常/n");
  else
   strMessage += _T("工作:停止中/n");


  strMessage += _T("/n");
 }


 ::MessageBox(NULL,strMessage,_T(""),MB_OK);
}


本文轉自
http://blog.vckbase.com/star/archive/2007/08/12/28501.html

相關文章