python类HLOCAL的实例源码

windows_cmdline.py 文件源码 项目:kapsel 作者: conda 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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
windows_cmdline.py 文件源码 项目:anaconda-project 作者: Anaconda-Platform 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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


问题


面经


文章

微信
公众号

扫码关注公众号