MFC 获取外网IP地址和MAC地址

MAC地址获取:

CString GetMacAddress(void)
{
	//CString strIP,strGateWay,strSubnetMask;
	CString strMac;
	strMac.Format(_T(""));
	u_char pMac[6];
	PIP_ADAPTER_INFO adp = NULL;
	ULONG uLong=0;
	//为适配器申请内存
	::GetAdaptersInfo(adp,&uLong);
	adp = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR,uLong);

	//取得本地适配器结构信息
	if(::GetAdaptersInfo(adp,&uLong) == ERROR_SUCCESS)
	{
		if(adp != NULL)
		{
			//strMacAdd.Format("%s",adp->Address);
			memcpy(pMac,adp->Address,6);
			strMac.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),pMac[0],pMac[1],pMac[2],pMac[3],pMac[4],pMac[5]);
			//strGateWay.Format(_T("%s"),adp->GatewayList.IpAddress.String);// 网关
			//strIP.Format(_T("%s"),adp->IpAddressList.IpAddress.String);//IP
			//strSubnetMask.Format(_T("%s"),adp->IpAddressList.IpMask.String);//子网掩码
			//MessageBox(strMac);
		}
	}
	return strMac;
}


ip地址获取:

CString GetSystemIp(void)
{
	CString csSource;
	CString csAddress;
	CString csIPAddress;
	csIPAddress.Format(_T(" "));
	CInternetSession mySession(NULL,0);
	CHttpFile* myHttpFile=NULL;
	csAddress=_T("http://iframe.ip138.com/ic.asp");//ip138网页 http://www.ip138.com/ip2city.asp
	myHttpFile=(CHttpFile*)mySession.OpenURL(csAddress);//读取网络地址
	while(myHttpFile->ReadString(csSource))
	{  //循环读取下载来的网页文本
		//code 转换
		char *pStr = (char*)csSource.GetBuffer(csSource.GetLength()); //取得str对象的原始字符串
		int nBufferSize = MultiByteToWideChar(CP_UTF8, 0,pStr, -1, NULL, 0); //取得所需缓存的多少
		wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申请缓存空间
		MultiByteToWideChar(CP_UTF8, 0, pStr, -1 , pBuffer, nBufferSize*sizeof(wchar_t));//转码
		//MessageBox(pBuffer); //显示
		csSource.Format(_T("%s"),pBuffer);
		free(pBuffer); //释放缓存

		int begin=0;
		begin=csSource.Find(_T("["),0);
		if(begin!=-1)//如果找到"[", 则找"]"  中括号内的文本则是 你的外网ip
		{
			int end=csSource.Find(_T("]"));
			csIPAddress.Format(_T("%s"),csSource.Mid(begin+1,end-begin-1));//提取外网ip
			return csIPAddress;
		}
	}
	return csIPAddress;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值