其他分享
首页 > 其他分享> > VC 根据域名获取IP

VC 根据域名获取IP

作者:互联网

#include   "Winsock2.h " 
#pragma comment(lib, "Ws2_32.lib")

/// 注意字符串需以\0结尾 。 例如:lpzSeverName  = " www.baidu.com/0";

bool WINAPI Domain2IP(char*  lpzSeverName,  TString & strIp)
{
if (!lpzSeverName)return false;
WORD   wVersionRequested  =  MAKEWORD(1,   1); 
WSADATA   wsaData; 
ZeroMemory(&wsaData, sizeof(wsaData));
WSAStartup(wVersionRequested,   &wsaData); 
hostent*   pHostent   =   gethostbyname(lpzSeverName); 
sockaddr_in  sa; 
ZeroMemory(&sa, sizeof(sa));
memcpy(&sa.sin_addr.s_addr,pHostent-> h_addr_list[0],pHostent-> h_length); 
std::string strTemp   =   inet_ntoa(sa.sin_addr);//IP 
WSACleanup(); 


TCHAR* lpzResult = NULL ;
if (!strTemp.c_str())return  false;

int nLen  = 0;
#ifdef  UNICODE
nLen =  ::MultiByteToWideChar(CP_ACP, 0, strTemp.c_str(), -1, NULL, 0); //include '\0' 
lpzResult = new wchar_t [nLen]; 
::MultiByteToWideChar(CP_ACP, 0, strTemp.c_str(), -1, lpzResult, nLen);
#else 
nLen = strlen(lpzByte);
lpzResult = new char[nLen + 1];
StringCbCopy(lpzResult, nLen + 1, strTemp.c_str());
#endif

strIp = lpzResult;
SAFE_DELETE(lpzResult);

return true;
}

 

标签:VC,addr,IP,lpzSeverName,nLen,lpzResult,域名,strTemp,sa
来源: https://blog.csdn.net/lizhong2008/article/details/6649750