VC知識庫BLOG-寧靜致遠-取得網路卡資訊(轉)
//使用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
相關文章
- 取得網路卡序列號 (轉)
- 取得oracle資料庫資訊(轉)Oracle資料庫
- 資訊優化知識庫優化
- 用程式取得CPU資訊 (轉)
- WWW全球資訊網知識小結
- 怎樣用程式取得CPU資訊 (轉)
- esxi網路卡資訊搜尋
- 網路管理知識集萃(轉)
- 【YashanDB知識庫】archivelog磁碟滿導致資料庫abnormalHive資料庫ORM
- 利用VC++獲取異構型資料庫庫結構資訊 (轉)C++資料庫
- 【YashanDB知識庫】EXP導致主機卡死問題
- 檢視無線網路卡資訊
- CentOS 6.5 修改網路卡配置資訊CentOS
- VC中文FAQ寶庫之7:獲得各種目錄資訊(轉)
- Linux下檢視網路卡資訊及確定網路卡位置Linux
- 獲取網路卡的相關資訊
- 資料庫知識點(2)——行轉列資料庫
- 資料庫基礎知識總結(轉)資料庫
- 知識雜庫
- PgSql 知識庫SQL
- 用VC實現遠端修改NT帳號 (轉)
- 轉載:翻譯的寧靜工程 (節選)
- 導致物聯網路卡停機的原因
- 運營知識與管理知識(轉)
- 取得ie詳細版本資訊.及其它版本資訊的兩種方法. (轉)
- 新手知識:認識(轉)
- (轉)Ubuntu網路卡基本配置Ubuntu
- 如何全面認識物聯網路卡
- 【YashanDB知識庫】資料庫審計shutdown immediate操作導致資料庫異常退出資料庫
- 小知識系列:查詢資料庫資料的元資訊資料庫
- 網路知識
- 知網知識庫呼叫
- 搭建知識庫xwiki
- 打造“個人知識庫”
- VC的撥號上網程式 (轉)
- RDMA:遠端直接存取資料,國產網路卡
- 讀出網路卡號碼 (轉)
- 【轉】redhat 雙網路卡繫結Redhat