def windows_split_command_line(command):
from ctypes import windll, c_int, POINTER, byref
from ctypes.wintypes import LPCWSTR, LPWSTR, HLOCAL
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
CommandLineToArgvW.restype = POINTER(LPWSTR)
LocalFree = windll.kernel32.LocalFree
LocalFree.argtypes = [HLOCAL]
LocalFree.restype = HLOCAL
argc = c_int(0)
argv = CommandLineToArgvW(command, byref(argc))
if not argv:
# The docs say CommandLineToArgvW returns NULL on error, but they
# don't describe any possible errors, so who knows when/if this happens.
# Maybe only on low memory or something?
raise WindowsCommandLineException("Windows could not parse command line: " + str(command))
try:
result = []
i = 0
while i < argc.value:
try:
result.append(str(argv[i]))
except UnicodeEncodeError as e:
message = (
"Windows cannot represent this command line in its character encoding: " + command + ": " + str(e))
raise WindowsCommandLineException(message)
i += 1
finally:
LocalFree(argv)
return result
python类HLOCAL的实例源码
def windows_split_command_line(command):
from ctypes import windll, c_int, POINTER, byref
from ctypes.wintypes import LPCWSTR, LPWSTR, HLOCAL
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
CommandLineToArgvW.restype = POINTER(LPWSTR)
LocalFree = windll.kernel32.LocalFree
LocalFree.argtypes = [HLOCAL]
LocalFree.restype = HLOCAL
argc = c_int(0)
argv = CommandLineToArgvW(command, byref(argc))
if not argv:
# The docs say CommandLineToArgvW returns NULL on error, but they
# don't describe any possible errors, so who knows when/if this happens.
# Maybe only on low memory or something?
raise WindowsCommandLineException("Windows could not parse command line: " + str(command))
try:
result = []
i = 0
while i < argc.value:
try:
result.append(str(argv[i]))
except UnicodeEncodeError as e:
message = (
"Windows cannot represent this command line in its character encoding: " + command + ": " + str(e))
raise WindowsCommandLineException(message)
i += 1
finally:
LocalFree(argv)
return result