2 * os_win32/hostname_win32.cpp
4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2004-8 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "hostname_win32.h"
21 const char * hostname_win32_c_cvsid
= "$Id: hostname_win32.cpp,v 1.6 2008/03/04 22:09:48 ballen4705 Exp $" HOSTNAME_WIN32_H_CVSID
;
23 #define WIN32_LEAN_AND_MEAN
27 #ifndef MAX_HOSTNAME_LEN
31 #define MAX_HOSTNAME_LEN 132
32 #define MAX_DOMAIN_NAME_LEN 132
33 #define MAX_SCOPE_ID_LEN 260
38 *PIP_ADDRESS_STRING
, IP_MASK_STRING
, *PIP_MASK_STRING
;
40 typedef struct _IP_ADDR_STRING
{
41 struct _IP_ADDR_STRING
* Next
;
42 IP_ADDRESS_STRING IpAddress
;
43 IP_MASK_STRING IpMask
;
49 char HostName
[MAX_HOSTNAME_LEN
];
50 char DomainName
[MAX_DOMAIN_NAME_LEN
];
51 PIP_ADDR_STRING CurrentDnsServer
;
52 IP_ADDR_STRING DnsServerList
;
54 char ScopeId
[MAX_SCOPE_ID_LEN
];
61 DWORD WINAPI
GetNetworkParams(PFIXED_INFO info
, PULONG size
);
63 #endif // MAX_HOSTNAME_LEN
66 // Call GetComputerNameEx() if available (Win2000/XP)
68 static BOOL
CallGetComputerNameExA(int type
, LPSTR name
, LPDWORD size
)
71 BOOL (WINAPI
* GetComputerNameExA_p
)(int/*enum COMPUTER_NAME_FORMAT*/, LPSTR
, LPDWORD
);
73 if (!(hdll
= LoadLibraryA("KERNEL32.DLL")))
75 if (!(GetComputerNameExA_p
= (BOOL (WINAPI
*)(int, LPSTR
, LPDWORD
))GetProcAddress(hdll
, "GetComputerNameExA")))
78 ret
= GetComputerNameExA_p(type
, name
, size
);
84 // Call GetNetworkParams() if available (Win98/ME/2000/XP)
86 static DWORD
CallGetNetworkParams(PFIXED_INFO info
, PULONG size
)
89 DWORD (WINAPI
* GetNetworkParams_p
)(PFIXED_INFO
, PULONG
);
91 if (!(hdll
= LoadLibraryA("IPHlpApi.dll")))
92 return ERROR_NOT_SUPPORTED
;
93 if (!(GetNetworkParams_p
= (DWORD (WINAPI
*)(PFIXED_INFO
, PULONG
))GetProcAddress(hdll
, "GetNetworkParams")))
94 ret
= ERROR_NOT_SUPPORTED
;
96 ret
= GetNetworkParams_p(info
, size
);
102 // Get host/domainname from registry (Win98/ME/NT4/2000/XP)
104 static DWORD
GetNamesFromRegistry(BOOL domain
, char * name
, int len
)
106 HKEY hk
; DWORD size
, type
;
107 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE
,
108 (GetVersion() & 0x80000000
109 ? "System\\CurrentControlSet\\Services\\VxD\\MSTCP" //Win9x/ME
110 : "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
111 0, KEY_READ
, &hk
) != ERROR_SUCCESS
)
114 if (!(RegQueryValueExA(hk
, (!domain
?"HostName":"Domain"), 0, &type
, (unsigned char *)name
, &size
) == ERROR_SUCCESS
&& type
== REG_SZ
))
116 if (size
== 0 && domain
) {
118 if (!(RegQueryValueExA(hk
, "DhcpDomain", 0, &type
, (unsigned char *)name
, &size
) == ERROR_SUCCESS
&& type
== REG_SZ
))
126 static int gethostdomname(int domain
, char * name
, int len
)
128 DWORD size
; FIXED_INFO info
;
130 // try KERNEL32.dll::GetComputerNameEx()
132 if (CallGetComputerNameExA((!domain
? 1:2/*ComputerNameDnsHost:Domain*/), name
, &size
))
135 // try IPHlpApi.dll::GetNetworkParams()
137 if (CallGetNetworkParams(&info
, &size
) == ERROR_SUCCESS
) {
138 strncpy(name
, (!domain
?info
.HostName
:info
.DomainName
), len
-1); name
[len
-1] = 0;
143 if (GetNamesFromRegistry(domain
, name
, len
))
149 // last resort: get NETBIOS name
151 if (GetComputerNameA(name
, &size
))
158 int gethostname(char * name
, int len
)
160 return gethostdomname(0, name
, len
);
164 int getdomainname(char * name
, int len
)
166 return gethostdomname(1, name
, len
);
177 if (gethostname(name
, sizeof(name
)))
178 strcpy(name
, "Error");
179 printf("hostname=\"%s\"\n", name
);
180 if (getdomainname(name
, sizeof(name
)))
181 strcpy(name
, "Error");
182 printf("domainname=\"%s\"\n", name
);