我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用ctypes.create_unicode_buffer()。
def _get_win_folder_with_ctypes(csidl_name): import ctypes csidl_const = { "CSIDL_APPDATA": 26, "CSIDL_COMMON_APPDATA": 35, "CSIDL_LOCAL_APPDATA": 28, }[csidl_name] buf = ctypes.create_unicode_buffer(1024) ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) # Downgrade to short path name if have highbit chars. See # <http://bugs.activestate.com/show_bug.cgi?id=85099>. has_high_char = False for c in buf: if ord(c) > 255: has_high_char = True break if has_high_char: buf2 = ctypes.create_unicode_buffer(1024) if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024): buf = buf2 return buf.value
def _get_win_folder_with_ctypes(csidl_name): csidl_const = { "CSIDL_APPDATA": 26, "CSIDL_COMMON_APPDATA": 35, "CSIDL_LOCAL_APPDATA": 28, }[csidl_name] buf = ctypes.create_unicode_buffer(1024) ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) # Downgrade to short path name if have highbit chars. See # <http://bugs.activestate.com/show_bug.cgi?id=85099>. has_high_char = False for c in buf: if ord(c) > 255: has_high_char = True break if has_high_char: buf2 = ctypes.create_unicode_buffer(1024) if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024): buf = buf2 return buf.value
def getvolumeinfo(path): """ Return information for the volume containing the given path. This is going to be a pair containing (file system, file system flags). """ # Add 1 for a trailing backslash if necessary, and 1 for the terminating # null character. volpath = ctypes.create_unicode_buffer(len(path) + 2) rv = GetVolumePathName(path, volpath, len(volpath)) if rv == 0: raise WinError() fsnamebuf = ctypes.create_unicode_buffer(MAX_PATH + 1) fsflags = DWORD(0) rv = GetVolumeInformation(volpath, None, 0, None, None, byref(fsflags), fsnamebuf, len(fsnamebuf)) if rv == 0: raise WinError() return (fsnamebuf.value, fsflags.value)
def get_windows_env_var(key): """Get an env var. Raises: WindowsError """ if not isinstance(key, text_type): raise TypeError("%r not of type %r" % (key, text_type)) buf = ctypes.create_unicode_buffer(32767) stored = winapi.GetEnvironmentVariableW(key, buf, 32767) if stored == 0: raise ctypes.WinError() return buf[:stored]
def test_buffers(self): ctypes.set_conversion_mode("ascii", "strict") buf = ctypes.create_unicode_buffer("abc") self.assertEqual(len(buf), 3+1) ctypes.set_conversion_mode("ascii", "replace") buf = ctypes.create_unicode_buffer("ab") self.assertEqual(buf[:], u"ab\uFFFD\uFFFD\uFFFD\0") self.assertEqual(buf[::], u"ab\uFFFD\uFFFD\uFFFD\0") self.assertEqual(buf[::-1], u"\0\uFFFD\uFFFD\uFFFDba") self.assertEqual(buf[::2], u"a\uFFFD\uFFFD") self.assertEqual(buf[6:5:-1], u"") ctypes.set_conversion_mode("ascii", "ignore") buf = ctypes.create_unicode_buffer("ab") # is that correct? not sure. But with 'ignore', you get what you pay for.. self.assertEqual(buf[:], u"ab\0\0\0\0") self.assertEqual(buf[::], u"ab\0\0\0\0") self.assertEqual(buf[::-1], u"\0\0\0\0ba") self.assertEqual(buf[::2], u"a\0\0") self.assertEqual(buf[6:5:-1], u"")