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;
}


 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐