Vc to get the computer name and IP address method

  • 2020-04-02 03:03:21
  • OfStack

This article illustrates how vc gets the computer name and IP address. Share with you for your reference. The specific implementation method is as follows:


#include <winsock2.h> 
#include <stdio.h> 
#pragma comment(lib,"ws2_32.lib")
void main() 
{ 
WSADATA wsadata; 
WORD dwVersionRequested; 
int err; 
err=WSAStartup(dwVersionRequested,&wsadata); 
char hostname[128]; 
if(gethostname(hostname,128)==0) 
{ 
  printf("%sn",hostname);//Computer name
} 
char buf[20];
//memset(buf,0,80);
struct hostent *pHost = gethostbyname(hostname);
for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++) 
{  
  //Put it in a character array for easy application
  strcpy(buf,inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
  //inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]);
  //The IP address
  printf("%sn",buf);
} 
WSACleanup(); 
}

Hope that this article described the VC programming for you to help.


Related articles: