gethostbyname代码示例(Windows CE)

/*
 * 功能: 由域名得到 IP 地址
 * 入口参数: pDomain 域名,如: www.163.com
 *			 pIPBuff 保存解析后的 IP
 * 出口参数: pIPBuff 保存解析后的 IP
 * 返回值: TRUE:成功;FALSE:失败
*/
BOOL CGetIPFromURLDlg::DomainToIP(char *pDomain,char *pIPBuff)    
{
	unsigned long lgIP = inet_addr(pDomain);

	WSADATA wsaData;
	WSAStartup(MAKEWORD(1,1),&wsaData);

	//输入的IP字符串
	if(lgIP != INADDR_NONE)
	{
		memcpy(pIPBuff,pDomain,strlen(pDomain));
		WSACleanup();

		return TRUE;
	}

	HOSTENT *host_entry;
	host_entry = gethostbyname(pDomain);

	if(host_entry != 0)  	
	{
		sprintf(pIPBuff,"%d.%d.%d.%d",
			(host_entry->h_addr_list[0][0] & 0xff),
			(host_entry->h_addr_list[0][1] & 0xff),
			(host_entry->h_addr_list[0][2] & 0xff),
			(host_entry->h_addr_list[0][3] & 0xff));
	}
	else
	{
		WSACleanup();
		return FALSE;  	
	} 

	WSACleanup();
	return TRUE; 
}

你可能感兴趣的:(windows,list,struct,XP,null)