Skip to main content
 首页 » 编程设计

python之属性错误 : module 'ctypes.wintypes' has no attribute 'create_unicode_buffer'

2025年05月04日26oomusou

这是我的代码的一部分:

import os 
 
def _get_appdata_path(): 
    import ctypes 
    from ctypes import wintypes, windll 
    CSIDL_APPDATA = 26 
    _SHGetFolderPath = windll.shell32.SHGetFolderPathW 
    _SHGetFolderPath.argtypes = [wintypes.HWND, 
                                 ctypes.c_int, 
                                 wintypes.HANDLE, 
                                 wintypes.DWORD, 
                                 wintypes.LPCWSTR] 
    path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH) 
    result = _SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, path_buf) 
    return path_buf.value 

但是当我调用 wintypes.create_unicode_buffer() 时,我得到了错误:

AttributeError: module 'ctypes.wintypes' has no attribute 'create_unicode_buffer' 

我正在使用 Python 3.5.1。我能做什么?

请您参考如下方法:

使用 ctypes.create_unicode_buffer,它在 PY2 和 PY3 中都有效。由于使用 from ctypes import *,它只是在 PY2 的 wintypes 中意外出现。 :D