VC获取计算机名和IP地址

void GetPcNameAndIp(CString &strPCName,vector<CString> &vIp)
{
	vIp.clear();	 
	WSADATA wsadata; 
	WORD dwVersionRequested; 
	int err = WSAStartup(dwVersionRequested,&wsadata); 
	char hostname[128]; 
	if(gethostname(hostname,128)==0) 
	{ 
		strPCName.Format("%s",hostname);
	} 
	char buf[20];
	struct hostent *pHost = gethostbyname(hostname);

	CString strTmp;
	for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++) 
	{   
		strcpy_s(buf,inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
		strTmp.Format("%s",buf);
		vIp.push_back(strTmp);
	} 

	WSACleanup();
}

 

posted @ 2015-01-21 20:19  柳絮飘诗  阅读(512)  评论(0编辑  收藏  举报