-#if defined(__WIN32__) && !defined(__TWIN32__)
- DWORD nSize = maxSize;
- return (::GetComputerName(buf, &nSize) != 0);
-#else
- char *sysname;
- const char *default_host = "noname";
-
- if ((sysname = getenv("SYSTEM_NAME")) == NULL) {
- GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
- } else
- strncpy(buf, sysname, maxSize - 1);
- buf[maxSize] = '\0';
- return *buf ? TRUE : FALSE;
-#endif
+#if !defined( __WXMICROWIN__) && wxUSE_DYNAMIC_LOADER
+ // TODO should use GetComputerNameEx() when available
+
+ // we don't want to always link with Winsock DLL as we might not use it at
+ // all, so load it dynamically here if needed (and don't complain if it is
+ // missing, we handle this)
+ wxLogNull noLog;
+
+ wxDynamicLibrary dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM);
+ if ( dllWinsock.IsLoaded() )
+ {
+ typedef int (PASCAL *WSAStartup_t)(WORD, WSADATA *);
+ typedef int (PASCAL *gethostname_t)(char *, int);
+ typedef hostent* (PASCAL *gethostbyname_t)(const char *);
+ typedef hostent* (PASCAL *gethostbyaddr_t)(const char *, int , int);
+ typedef int (PASCAL *WSACleanup_t)(void);
+
+ #define LOAD_WINSOCK_FUNC(func) \
+ func ## _t \
+ pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
+
+ LOAD_WINSOCK_FUNC(WSAStartup);
+
+ WSADATA wsa;
+ if ( pfnWSAStartup && pfnWSAStartup(MAKEWORD(1, 1), &wsa) == 0 )
+ {
+ LOAD_WINSOCK_FUNC(gethostname);
+
+ wxString host;
+ if ( pfngethostname )
+ {
+ char bufA[256];
+ if ( pfngethostname(bufA, WXSIZEOF(bufA)) == 0 )
+ {
+ // gethostname() won't usually include the DNS domain name,
+ // for this we need to work a bit more
+ if ( !strchr(bufA, '.') )
+ {
+ LOAD_WINSOCK_FUNC(gethostbyname);
+
+ struct hostent *pHostEnt = pfngethostbyname
+ ? pfngethostbyname(bufA)
+ : NULL;
+
+ if ( pHostEnt )
+ {
+ // Windows will use DNS internally now
+ LOAD_WINSOCK_FUNC(gethostbyaddr);
+
+ pHostEnt = pfngethostbyaddr
+ ? pfngethostbyaddr(pHostEnt->h_addr,
+ 4, AF_INET)
+ : NULL;
+ }
+
+ if ( pHostEnt )
+ {
+ host = wxString::FromAscii(pHostEnt->h_name);
+ }
+ }
+ }
+ }
+
+ LOAD_WINSOCK_FUNC(WSACleanup);
+ if ( pfnWSACleanup )
+ pfnWSACleanup();
+
+
+ if ( !host.empty() )
+ {
+ wxStrncpy(buf, host, maxSize);
+
+ return true;
+ }
+ }
+ }
+#endif // !__WXMICROWIN__
+
+ return wxGetHostName(buf, maxSize);