572
if has_ctypes and winver != 'Windows 98':
572
if has_ctypes and winver == 'Windows NT':
573
573
def get_unicode_argv():
574
574
prototype = ctypes.WINFUNCTYPE(ctypes.c_wchar_p)
575
575
GetCommandLineW = prototype(("GetCommandLineW",
580
580
# Skip the first argument, since we only care about parameters
581
581
argv = _command_line_to_argv(command_line, sys.argv)[1:]
585
def get_environ_unicode(key, default=None):
586
"""Get `key` from environment as unicode or `default` if unset
588
A large enough buffer will be allocated to retrieve the value, though
589
it may take two calls to the underlying library function.
591
This needs ctypes because pywin32 does not expose the wide version.
593
cfunc = getattr(get_environ_unicode, "_c_function", None)
595
from ctypes.wintypes import DWORD, LPCWSTR, LPWSTR
596
cfunc = ctypes.WINFUNCTYPE(DWORD, LPCWSTR, LPWSTR, DWORD)(
597
("GetEnvironmentVariableW", ctypes.windll.kernel32))
598
get_environ_unicode._c_function = cfunc
599
buffer_size = 256 # heuristic, 256 characters often enough
601
buffer = ctypes.create_unicode_buffer(buffer_size)
602
length = cfunc(key, buffer, buffer_size)
604
code = ctypes.GetLastError()
605
if code == 203: # ERROR_ENVVAR_NOT_FOUND
607
raise ctypes.WinError(code)
608
if buffer_size > length:
609
return buffer[:length]
584
get_unicode_argv = None
612
get_unicode_argv = get_environ_unicode = None