This article describes the VC to obtain the computer name and IP address method. Share to everyone for your reference. The implementation methods are 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 ("%s\n", 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 (bu F,inet_ntoa (* (struct in_addr *) phost->h_addr_list[i));
Inet_ntoa (* (struct in_addr *) phost->h_addr_list[i]);
IP address
printf ("%s\n", buf);
}
WSACleanup ();
}
I hope this article on the VC program for everyone to help.