📄 netadapterinfo.cpp
字号:
// OneNetAdapterInfo.cpp: implementation of the COneNetAdapterInfo class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SpeedPostEmail.h"
#include "NetAdapterInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define MALLOC( bytes ) ::HeapAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, (bytes) )
#define FREE( ptr ) if( ptr ) ::HeapFree( ::GetProcessHeap(), 0, ptr )
#define REMALLOC( ptr, bytes ) ::HeapReAlloc( ::GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, bytes )
#pragma comment ( lib, "iphlpapi.lib" )
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
COneNetAdapterInfo::COneNetAdapterInfo ( IP_ADAPTER_INFO *pAdptInfo )
{
m_bInitOk = FALSE;
memset ( &m_PhysicalAddress, 0, sizeof(m_PhysicalAddress) );
m_nPhysicalAddressLength = 0;
ASSERT ( pAdptInfo );
memcpy ( &m_AdptInfo, pAdptInfo, sizeof(IP_ADAPTER_INFO) );
if ( !Init() )
{
TRACE ( _T("[%s - %s] initialize failed."), Get_Name(), Get_Desc() );
}
}
COneNetAdapterInfo::~COneNetAdapterInfo()
{
}
//
// 根据传入的 pAdptInfo 信息来获取指定网卡的基本信息
//
BOOL COneNetAdapterInfo::Init ()
{
IP_ADDR_STRING* pNext = NULL;
IP_PER_ADAPTER_INFO* pPerAdapt = NULL;
ULONG ulLen = 0;
DWORD dwErr = ERROR_SUCCESS;
ASSERT ( m_AdptInfo.AddressLength > 0 );
t_IPINFO iphold;
// 将变量清空
m_bInitOk = FALSE;
m_csName.Empty ();
m_csDesc.Empty ();
m_CurIPInfo.csIP.Empty ();
m_CurIPInfo.csSubnet.Empty ();
m_Ary_IP.RemoveAll ();
m_Ary_DNS.RemoveAll ();
m_Ary_Gateway.RemoveAll ();
#ifndef _UNICODE
m_csName = m_AdptInfo.AdapterName;
m_csDesc = m_AdptInfo.Description;
#else
USES_CONVERSION;
m_csName = A2W ( m_AdptInfo.AdapterName );
m_csDesc = A2W ( m_AdptInfo.Description );
#endif
// 获取当前正在使用的IP地址
if ( m_AdptInfo.CurrentIpAddress )
{
m_CurIPInfo.csIP = m_AdptInfo.CurrentIpAddress->IpAddress.String;
m_CurIPInfo.csSubnet = m_AdptInfo.CurrentIpAddress->IpMask.String;
}
else
{
m_CurIPInfo.csIP = _T("0.0.0.0");
m_CurIPInfo.csSubnet = _T("0.0.0.0");
}
// 获取本网卡中所有的IP地址
pNext = &( m_AdptInfo.IpAddressList );
while ( pNext )
{
iphold.csIP = pNext->IpAddress.String;
iphold.csSubnet = pNext->IpMask.String;
m_Ary_IP.Add ( iphold );
pNext = pNext->Next;
}
// 获取本网卡中所有的网关信息
pNext = &( m_AdptInfo.GatewayList );
while ( pNext )
{
m_Ary_Gateway.Add ( pNext->IpAddress.String );
pNext = pNext->Next;
}
// 获取本网卡中所有的 DNS
dwErr = ::GetPerAdapterInfo ( m_AdptInfo.Index, pPerAdapt, &ulLen );
if( dwErr == ERROR_BUFFER_OVERFLOW )
{
pPerAdapt = ( IP_PER_ADAPTER_INFO* ) MALLOC( ulLen );
dwErr = ::GetPerAdapterInfo( m_AdptInfo.Index, pPerAdapt, &ulLen );
// if we succeed than we need to drop into our loop
// and fill the dns array will all available IP
// addresses.
if( dwErr == ERROR_SUCCESS )
{
pNext = &( pPerAdapt->DnsServerList );
while( pNext )
{
m_Ary_DNS.Add( pNext->IpAddress.String );
pNext = pNext->Next;
}
m_bInitOk = TRUE;
}
// this is done outside the dwErr == ERROR_SUCCES just in case. the macro
// uses NULL pointer checking so it is ok if pPerAdapt was never allocated.
FREE( pPerAdapt );
}
return m_bInitOk;
}
//
// 释放或刷新本网卡的IP地址
//
BOOL COneNetAdapterInfo::RenewReleaseIP( Func_OperateIP func )
{
IP_INTERFACE_INFO* pInfo = NULL;
BOOL bDidIt = FALSE;
ULONG ulLen = 0;
int nNumInterfaces = 0;
int nCnt = 0;
DWORD dwErr = ERROR_SUCCESS;
dwErr = ::GetInterfaceInfo ( pInfo, &ulLen );
if( dwErr == ERROR_INSUFFICIENT_BUFFER )
{
pInfo = ( IP_INTERFACE_INFO* ) MALLOC( ulLen );
dwErr = ::GetInterfaceInfo ( pInfo, &ulLen );
if( dwErr != ERROR_SUCCESS )
{
return FALSE;
}
}
// we can assume from here out that we have a valid array
// of IP_INTERFACE_INFO structures due to the error
// checking one above.
nNumInterfaces = ulLen / sizeof( IP_INTERFACE_INFO );
for( nCnt = 0; nCnt < nNumInterfaces; nCnt++ )
{
if( pInfo[ nCnt ].Adapter[ 0 ].Index == m_AdptInfo.Index )
{
dwErr = func( &pInfo[ nCnt ].Adapter[ 0 ] );
// free all used memory since we don't need it any more.
FREE( pInfo );
bDidIt = ( dwErr == NO_ERROR );
if( ! bDidIt ) {
return FALSE;
}
break;
}
}
return bDidIt;
}
////////////////////////////////////////////////////////////
// Desc:
// Releases the addresses held by this adapter.
////////////////////////////////////////////////////////////
BOOL COneNetAdapterInfo::ReleaseIP()
{
return RenewReleaseIP ( ::IpReleaseAddress );
}
////////////////////////////////////////////////////////////
// Desc:
// Renews the address being held by this adapter.
////////////////////////////////////////////////////////////
BOOL COneNetAdapterInfo::RenewIP()
{
return RenewReleaseIP ( ::IpRenewAddress );
}
CString COneNetAdapterInfo::GetAdapterTypeString ()
{
UINT nType = m_AdptInfo.Type;
CString csType = _T("");
switch( nType )
{
case MIB_IF_TYPE_OTHER: csType = _T("Other"); break;
case MIB_IF_TYPE_ETHERNET: csType = _T("Ethernet"); break;
case MIB_IF_TYPE_TOKENRING: csType = _T("Token Ring"); break;
case MIB_IF_TYPE_FDDI: csType = _T("FDDI"); break;
case MIB_IF_TYPE_PPP: csType = _T("PPP"); break;
case MIB_IF_TYPE_LOOPBACK: csType = _T("Loopback"); break;
case MIB_IF_TYPE_SLIP: csType = _T("SLIP"); break;
default: csType = _T("Invalid Adapter Type"); break;
};
return csType;
}
time_t COneNetAdapterInfo::Get_LeaseObtained() const { return m_AdptInfo.LeaseObtained; }
time_t COneNetAdapterInfo::Get_LeaseExpired() const { return m_AdptInfo.LeaseExpires; }
int COneNetAdapterInfo::Get_IPCount() const { return (int)m_Ary_IP.GetSize(); }
int COneNetAdapterInfo::Get_DNSCount() const { return (int)m_Ary_DNS.GetSize(); }
CString COneNetAdapterInfo::Get_CurrentIP() const { return m_CurIPInfo.csIP; }
BOOL COneNetAdapterInfo::Is_DHCP_Used() const { return m_AdptInfo.DhcpEnabled; }
CString COneNetAdapterInfo::Get_DHCPAddr() const { return m_AdptInfo.DhcpServer.IpAddress.String; }
BOOL COneNetAdapterInfo::Is_Wins_Used() const { return m_AdptInfo.HaveWins; }
CString COneNetAdapterInfo::Get_PrimaryWinsServer() const { return m_AdptInfo.PrimaryWinsServer.IpAddress.String; }
CString COneNetAdapterInfo::Get_SecondaryWinsServer() const { return m_AdptInfo.SecondaryWinsServer.IpAddress.String; }
int COneNetAdapterInfo::Get_GatewayCount() const { return m_Ary_Gateway.GetSize(); }
DWORD COneNetAdapterInfo::Get_AdapterIndex() const { return m_AdptInfo.Index; }
UINT COneNetAdapterInfo::Get_AdapterType() const { return m_AdptInfo.Type; }
CString COneNetAdapterInfo::Get_IPAddr ( int nIndex ) const
{
CString csAddr = _T("");
if ( nIndex >= 0 && nIndex < m_Ary_IP.GetSize() )
{
csAddr = m_Ary_IP.GetAt(nIndex).csIP;
}
return csAddr;
}
CString COneNetAdapterInfo::Get_Subnet ( int nIndex ) const
{
CString csAddr = _T("");
if ( nIndex >= 0 && nIndex < m_Ary_IP.GetSize() )
{
csAddr = m_Ary_IP.GetAt(nIndex).csSubnet;
}
return csAddr;
}
CString COneNetAdapterInfo::Get_DNSAddr ( int nIndex ) const
{
CString csAddr = _T("");
if ( nIndex >= 0 && nIndex < m_Ary_DNS.GetSize() )
{
csAddr = m_Ary_DNS.GetAt(nIndex);
}
return csAddr;
}
CString COneNetAdapterInfo::Get_GatewayAddr ( int nIndex ) const
{
CString csAddr = _T("");
if ( nIndex >= 0 && nIndex < m_Ary_Gateway.GetSize() )
{
csAddr = m_Ary_Gateway.GetAt(nIndex);
}
return csAddr;
}
void COneNetAdapterInfo::Set_PhysicalAddress ( int nPhysicalAddressLength, BYTE *pPhysicalAddress )
{
if ( !pPhysicalAddress ) return;
m_nPhysicalAddressLength = __min(nPhysicalAddressLength,sizeof(m_PhysicalAddress));
memcpy ( m_PhysicalAddress, pPhysicalAddress, m_nPhysicalAddressLength );
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNetAdapterInfo::CNetAdapterInfo ()
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -