Ayse
Ayse

Reputation: 2754

sethostname identifier not found error

I want to set the name of my local host. I am using sethostname() function but I get an error as follows:

error C3861: 'sethostname': identifier not found.

Below is the code snippet:

WSADATA wsa;
///Initialise winsock///
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
   {
      //Socket Initialization Failed///
      exit(EXIT_FAILURE);
   }
 char setname[128]="Ayesha-PC";
 sethostname(setname, sizeof (setname));

Upvotes: 2

Views: 1015

Answers (1)

Anya Shenanigans
Anya Shenanigans

Reputation: 94769

Windows uses it's own mechanism for setting the host name. The function you use is either SetComputerName or SetComputerNameEx.

Changes take effect on the next reboot. You can request a reboot using ExitWindowsEx.

You'll need to have administrator privileges to invoke these routines, the reason for the reboot requirement is because a lot of windows sub systems will not check the hostname after starting up and will keep using the old name (this is similar in Linux); It may have consequences for DNS in domains as well.

Upvotes: 4

Related Questions