This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project. See the Cygwin home page for more information.
Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: gethostname


Aldo Mazzilli <aldo.mazzilli@inria.fr> writes:
> Hello,
> 
> I have to port a little C program to WindowsNT and I have some problems
> with the following 'gethostname' command.
> 
> I just want to get the HOSTNAME value using :
> 	- gcc with cygwin32 libs (local compilation)
> and 	- egcs with mingw32 libs (cross-compilation)
> 
> None of these both solutions work.
> 
> Do you know why the following program can't give me the HOSTNAME value :
> ---------
> main ()
> {
>      char HOSTNAME[4096];
>  
>      gethostname (HOSTNAME, 4096);
>         printf("HOST : %s",HOSTNAME) ;
> }
> ---------
> 
> NB : it seems that WindowsNT requires to start up the Windows Sockets
> API using WSAStartup() . Am I right ?

What exactly does not work?? I tried out your test code With Cygwin b20.1
and it spit out the hostname for me. Please provide more details when you
post these "but it doesn't work!" messages.

For mingw32, you'll have to make sure the correct prototype is included
(via winsock.h or some such header) and also wsock32 library is linked
in. You quite possibly also have to initialize Winsock, and I'm sure any
win32 book will tell you what the right thing is ...

Try the following (untested, mind you):

  #include <stdio.h>
  #include <unistd.h>
  #if defined (_WIN32) && ! defined (__CYGWIN__)
  #include <winsock.h>
  #endif

  int
  main ()
  {
     char HOSTNAME[4096];
   
  #if defined (_WIN32) && ! defined (__CYGWIN__)
     WSADATA p;
     WSAStartup ((2<<8) | 2, &p);
  #endif
     gethostname (HOSTNAME, 4096);
     printf("HOST : %s\n", HOSTNAME) ;
     return 0;
  }

Regards,
Mumit


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com