207
197
return os.environ.get('USERNAME', None)
210
# 1 == ComputerNameDnsHostname, which returns "The DNS host name of the local
211
# computer or the cluster associated with the local computer."
212
_WIN32_ComputerNameDnsHostname = 1
214
200
def get_host_name():
215
201
"""Return host machine name.
216
202
If name cannot be obtained return None.
218
:return: A unicode string representing the host name. On win98, this may be
219
a plain string as win32 api doesn't support unicode.
204
Returned value can be unicode or plain sring.
205
To convert plain string to unicode use
206
s.decode(bzrlib.user_encoding)
223
return win32api.GetComputerNameEx(_WIN32_ComputerNameDnsHostname)
224
except (NotImplementedError, win32api.error):
225
# NotImplemented will happen on win9x...
229
210
kernel32 = ctypes.windll.kernel32
211
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix)
230
212
except AttributeError:
231
pass # Missing the module we need
233
215
buf = create_buffer(MAX_COMPUTERNAME_LENGTH+1)
234
216
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
236
# Try GetComputerNameEx which gives a proper Unicode hostname
237
GetComputerNameEx = getattr(kernel32, 'GetComputerNameEx'+suffix,
239
if (GetComputerNameEx is not None
240
and GetComputerNameEx(_WIN32_ComputerNameDnsHostname,
241
buf, ctypes.byref(n))):
244
# Try GetComputerName in case GetComputerNameEx wasn't found
245
# It returns the NETBIOS name, which isn't as good, but still ok.
246
# The first GetComputerNameEx might have changed 'n', so reset it
247
n = ctypes.c_int(MAX_COMPUTERNAME_LENGTH+1)
248
GetComputerName = getattr(kernel32, 'GetComputerName'+suffix,
250
if (GetComputerName is not None
251
and GetComputerName(buf, ctypes.byref(n))):
253
# otherwise try env variables, which will be 'mbcs' encoded
254
# on Windows (Python doesn't expose the native win32 unicode environment)
256
# http://msdn.microsoft.com/en-us/library/aa246807.aspx
257
# environment variables should always be encoded in 'mbcs'.
259
return os.environ['COMPUTERNAME'].decode("mbcs")
217
if GetComputerName(buf, ctypes.byref(n)):
219
# otherwise try env variables
220
return os.environ.get('COMPUTERNAME', None)
264
223
def _ensure_unicode(s):