www.pudn.com > IP_phone.rar > NetSettingInfo.cpp


/**********************************************************/ 
/*类名:CSystemInfo                                       */ 
/*简述:网络信息检测类                                    */ 
/**********************************************************/ 
// NetSettingInfo.cpp: implementation of the CNetSettingInfo class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "NetSettingInfo.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CNetSettingInfo::CNetSettingInfo() 
{ 
  if (!AfxSocketInit()) 
	{ 
		AfxMessageBox(IDP_SOCKETS_INIT_FAILED); 
	} 
} 
 
CNetSettingInfo::~CNetSettingInfo() 
{ 
 
} 
 
//得到网络配置信息 
LPNETINFO CNetSettingInfo::GetNetInfo() 
{ 
	//取得主机名 
	if(gethostname(m_lpNetInfo.HostName, 128)!=0) 
		AfxMessageBox("gethostname fail!\n"); 
 
	//加载IP_Helper_API 所需的库文件 
    HINSTANCE hInst;//实例句柄 
    hInst=LoadLibrary("iphlpapi.dll"); 
    if(!hInst) 
	{ 
		AfxMessageBox("iphlpapi.dll not supported in this platform!\n"); 
		return false; 
	} 
	else 
	{ 
		//取得函数指针 
		PGAINFO pGAInfo; 
		pGAInfo=(PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo"); 
		PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL; 
		ULONG ulSize=0; 
 
		if (pInfo!=NULL) 
		delete (pInfo); 
		unsigned  long nError; 
		//异常处理 
    	nError=pGAInfo(pInfo,&ulSize); 
	    if (nError==ERROR_NO_DATA) 
		{ 
			AfxMessageBox("No adapter information exists for the local computer"); 
		} 
	    if (nError==ERROR_NOT_SUPPORTED) 
		{ 
			AfxMessageBox("GetAdaptersInfo is not supported by the operating system running on the local computer"); 
		} 
	    /*if (nError==ERROR_BUFFER_OVERFLOW) 
		{ 
			AfxMessageBox("Buffer error,try again!"); 
		}*/ 
		if (nError==ERROR_BUFFER_OVERFLOW||nError==0) 
		{ 
			pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]); 
			pGAInfo(pInfo,&ulSize);	 
 
			//if (pinfo!=NULL){.../pinfo = pinfo->Next;}用于多个网卡 
 
			//网卡名 
			m_lpNetInfo.AdapterName=pInfo->AdapterName; 
			//网卡描述信息   
			m_lpNetInfo.Description=pInfo->Description; 
			//网卡类型 
			m_lpNetInfo.AdapterType.Format("%d",pInfo->Type); 
			//显示物理地址 
			m_lpNetInfo.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]); 
			//显示绑定于这张网卡之上的IP地址 
			PIP_ADDR_STRING pAddTemp=&(pInfo->IpAddressList);        
			while(pAddTemp)//遍历IP列表中的每一个元素 
			{ 
				m_lpNetInfo.IpAddress+=pAddTemp->IpAddress.String; 
				pAddTemp=pAddTemp->Next; 
				if (pAddTemp!=NULL) 
				m_lpNetInfo.IpAddress+="\r\n"; 
			}  
			//子网掩码 
			m_lpNetInfo.SubNet.Format("%s",pInfo->IpAddressList.IpMask.String);  
			//默认网关 
			m_lpNetInfo.GateWay.Format("%s",pInfo->GatewayList.IpAddress.String); 
			//Wins 服务器数据 
			if (pInfo->HaveWins) 
				m_lpNetInfo.WinsServer.Format("%s",pInfo->PrimaryWinsServer.IpAddress.String ); 
			else 
				m_lpNetInfo.WinsServer.Format("%s","N/A" ); 
			//显示DHCP 服务器数据 
			if (pInfo->DhcpEnabled ) 
				m_lpNetInfo.DHCPServer.Format("%s",pInfo->DhcpServer.IpAddress.String ); 
			else 
				m_lpNetInfo.DHCPServer.Format("%s","N/A"); 
			// 
		} 
	// 
	} 
	// 
	return &m_lpNetInfo; 
}