[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gethostname on Windows

From: Simon Josefsson
Subject: Re: gethostname on Windows
Date: Thu, 02 Apr 2009 10:11:14 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.90 (gnu/linux)

Bruno Haible <address@hidden> writes:

> Simon Josefsson wrote:
>> The current gethostname module will return an empty string on mingw:
>>   strcpy (name, "");         /* Hardcode your system name if you want.  */
>> This is sub-optimal since Windows has a gethostname function in
>> -lws2_32.
>> The following patch should make gethostname return proper values.
>> Tested on x86 debian lenny and mingw cross-compile.  Any objections to
>> installing it?
> Well, I don't much like the dependency on an external library for the
> modules 'gethostname' and 'uname' (later, when we get an 'uname' module).
> I did some experiments with the GetComputerNameEx function [1], which is
> present in kernel32.dll. The parameters ComputerNameDnsHostname and
> ComputerNamePhysicalDnsHostname returned the same value as gethostname()
> for me, except that gethostname() returns a lowercased result (which can
> be simulated in 3 lines of code).
> OTOH, the gethostname() in -lws2_32 is a "Unixy" API, and it seems idiotic
> want to use a "Windozy" API instead.
> I'm undecided.

Avoiding -lws2_32 is good, but if the application links to -lws2_32
anyway, there will be no saving.

Maybe the gethostname module could detect this?  I.e., if the gnulib
module sys_sockets_h is also used, so the application is likely to link
to ws2_32 anyway, it can use -lws2_32 otherwise it will fall back on
GetComputerNameEx.  I'm not sure how to implement a M4 test like that

Anyway, that solution isn't very reliable: you may want to use
gethostname in an application that doesn't use sockets otherwise.  Maybe
two different gnulib modules then?  gethostname uses -lws2_32 and
gethostname-lean uses GetComputerNameEx?

Some additional questions:

1) How do I use it?  This doesn't seem to work:

address@hidden:~$ cat foo.c
#define WINVER 0x0500
#include <windows.h>
#include <stdio.h>
int main () {
  char out[1024];
  DWORD size = 1024;
  BOOL t = GetComputerNameEx(ComputerNameDnsHostname, out, &size);
  printf("hi %d: %s", t, out);
address@hidden:~$ i586-mingw32msvc-gcc -o foo.exe foo.c 
/tmp/ccDXn2yi.o:foo.c:(.text+0x37): undefined reference to `_GetComputerNameEx'
collect2: ld returned 1 exit status

2) What is the maximum string size that GetComputerNameEx can return?
   For the gethostname, the max size is documented.

3) Is the GetComputerNameEx semantics right?  It seems the gethostname
   result will be different in some configurations?  I'm not sure how
   important that is, but for gss/shishi the use of gethostname is to
   derive a Kerberos V5 principal name so there could be security
   implications if the value is not right.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]