资源简介
在MFC环境下获取本机网络信息,本地IP 外网IP 子虚掩码 mac地址
代码片段和文件信息
#include “StdAfx.h“
#include
#include “NetworkConf.h“
#include
#include
#include “Iphlpapi.h“
#include
#pragma comment(lib“Iphlpapi“)
#pragma comment(lib“WS2_32“)
using namespace std;
CNetworkConf::CNetworkConf(void)
{
ZeroMemory(m_ucLocalMac6);
m_dwGatewayIP=m_dwLocalIP=m_dwMask=0;
m_pAdapterInfo=NULL;
m_lLen=0;
::GetAdaptersInfo(m_pAdapterInfo&m_lLen);
m_pAdapterInfo=(PIP_ADAPTER_INFO)::GlobalAlloc(GPTRm_lLen);
m_strNavigateUrl=_T(“http://www.whatismyip.com.tw/“);
}
CNetworkConf::~CNetworkConf(void)
{
}
/*
*函数名:GetIPinfo
*参 数:是否内网(支持获取外网IP)
*返回值:CString IP地址
*注 意:投递外网IP的链接可能不稳定
*/
CString CNetworkConf::GetIPinfo( bool bIntranet/*=false*/ )
{
CString strLocalIP=_T(““);
if (bIntranet)
{
if (InitNetInfo())
{
in_addr in;
in.S_un.S_addr=m_dwLocalIP;
strLocalIP=inet_ntoa(in);
}
}else
{
CAtlHttpClient httpClient;
m_strNavigateUrl=_T(“http://www.ip138.com/“);
if (httpClient.Navigate(m_strNavigateUrl))
{
if (httpClient.GetStatus()==200)
{
int nLen=httpClient.GetBodyLength();
char* pBuff=new char[nLen+1];
memset(pBuff0nLen+1);
memcpy(pBuffhttpClient.GetBody()nLen);
strLocalIP=pBuff;
int nLeft=strLocalIP.Find(_T(““))+CString(_T(““)).GetLength();
int nCount=strLocalIP.Find(_T(“
“))-nLeft;
strLocalIP=strLocalIP.Mid(nLeftnCount);
delete []pBuff;
}
}
}
return strLocalIP;
}
CString CNetworkConf::GetMacinfo()
{
CString strMac=_T(““);
if (InitNetInfo())
{
UCHAR *pInfo=m_ucLocalMac;
strMac.AppendFormat(_T(“%02X-%02X-%02X-%02X-%02X-%02X“) pInfo[0] pInfo[1] pInfo[2] pInfo[3] pInfo[4] pInfo[5]);
}
return strMac;
}
CString CNetworkConf::GetSubnetMask()
{
CString strMask=_T(““);
if (InitNetInfo())
{
in_addr in;
in.S_un.S_addr=m_dwMask;
strMask=inet_ntoa(in);
}
return strMask;
}
CString CNetworkConf::GetGateway()
{
CString strGatewayIP=_T(““);
if (InitNetInfo())
{
in_addr in;
in.S_un.S_addr=m_dwGatewayIP;
strGatewayIP=inet_ntoa(in);
}
return strGatewayIP;
}
bool CNetworkConf::InitNetInfo()
{
bool bSuccess=(ERROR_SUCCESS==::GetAdaptersInfo(m_pAdapterInfo&m_lLen)) ;
memcpy(m_ucLocalMac m_pAdapterInfo->Address 6);
m_dwGatewayIP = ::inet_addr(m_pAdapterInfo->GatewayList.IpAddress.String);
m_dwLocalIP = ::inet_addr(m_pAdapterInfo->IpAddressList.IpAddress.String);
m_dwMask = ::inet_addr(m_pAdapterInfo->IpAddressList.IpMask.String);
return bSuccess;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-09-29 13:04 NetworkConf\
文件 2642 2012-09-29 13:03 NetworkConf\NetworkConf.cpp
文件 535 2012-09-29 10:40 NetworkConf\NetworkConf.h
- 上一篇:MFCC的matlab实现
- 下一篇:MFC类图-vs2010最新
评论
共有 条评论