本文整理汇总了Python中ctypes.create_unicode_buffer方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.create_unicode_buffer方法的具体用法?Python ctypes.create_unicode_buffer怎么用?Python ctypes.create_unicode_buffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.create_unicode_buffer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_window_text
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
def get_window_text(window_obj):
"""A nice wrapper for GetWindowTextW(). TODO
Syntax:
int GetWindowTextW(
HWND hWnd,
LPWSTR lpString,
int nMaxCount
);
int GetWindowTextLengthW(
HWND hWnd
);
Microsoft Documentation:
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowtextw
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getwindowtextlengthw
"""
textLenInCharacters = ctypes.windll.user32.GetWindowTextLengthW(window_obj.hWnd)
stringBuffer = ctypes.create_unicode_buffer(textLenInCharacters + 1) # +1 for the \0 at the end of the null-terminated string.
ctypes.windll.user32.GetWindowTextW(window_obj.hWnd, stringBuffer, textLenInCharacters + 1)
# TODO it's ambiguous if an error happened or the title text is just empty. Look into this later.
return stringBuffer.value
示例2: _get_win_folder_with_ctypes
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
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
示例3: _get_win_folder_with_ctypes
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
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
示例4: GetClassNameW
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
def GetClassNameW(hWnd):
_GetClassNameW = windll.user32.GetClassNameW
_GetClassNameW.argtypes = [HWND, LPWSTR, ctypes.c_int]
_GetClassNameW.restype = ctypes.c_int
nMaxCount = 0x1000
dwCharSize = sizeof(WCHAR)
while 1:
lpClassName = ctypes.create_unicode_buffer(nMaxCount)
nCount = _GetClassNameW(hWnd, lpClassName, nMaxCount)
if nCount == 0:
raise ctypes.WinError()
if nCount < nMaxCount - dwCharSize:
break
nMaxCount += 0x1000
return str(lpClassName.value)
示例5: get_windows_env_var
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
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]
示例6: test_buffers
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
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"")
示例7: detectForgroundWindows
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
def detectForgroundWindows():
#Stolen fom https://sjohannes.wordpress.com/2012/03/23/win32-python-getting-all-window-titles/
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append(buff.value)
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
return titles
示例8: test_find_microbit_nt_exists
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
def test_find_microbit_nt_exists():
"""
Simulate being on os.name == 'nt' and a disk with a volume name 'MICROBIT'
exists indicating a connected micro:bit device.
"""
mock_windll = mock.MagicMock()
mock_windll.kernel32 = mock.MagicMock()
mock_windll.kernel32.GetVolumeInformationW = mock.MagicMock()
mock_windll.kernel32.GetVolumeInformationW.return_value = None
#
# Have every drive claim to be removable
#
mock_windll.kernel32.GetDriveTypeW = mock.MagicMock()
mock_windll.kernel32.GetDriveTypeW.return_value = 2
with mock.patch('os.name', 'nt'):
with mock.patch('os.path.exists', return_value=True):
return_value = ctypes.create_unicode_buffer('MICROBIT')
with mock.patch('ctypes.create_unicode_buffer',
return_value=return_value):
ctypes.windll = mock_windll
assert uflash.find_microbit() == 'A:\\'
示例9: test_find_microbit_nt_missing
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
def test_find_microbit_nt_missing():
"""
Simulate being on os.name == 'nt' and a disk with a volume name 'MICROBIT'
does not exist for a micro:bit device.
"""
mock_windll = mock.MagicMock()
mock_windll.kernel32 = mock.MagicMock()
mock_windll.kernel32.GetVolumeInformationW = mock.MagicMock()
mock_windll.kernel32.GetVolumeInformationW.return_value = None
with mock.patch('os.name', 'nt'):
with mock.patch('os.path.exists', return_value=True):
return_value = ctypes.create_unicode_buffer(1024)
with mock.patch('ctypes.create_unicode_buffer',
return_value=return_value):
ctypes.windll = mock_windll
assert uflash.find_microbit() is None
示例10: getWindowByTitle
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import create_unicode_buffer [as 别名]
def getWindowByTitle(self, wildcard, order=0):
""" Returns a handle for the first window that matches the provided "wildcard" regex """
EnumWindowsProc = ctypes.WINFUNCTYPE(
ctypes.c_bool,
ctypes.POINTER(ctypes.c_int),
ctypes.py_object)
def callback(hwnd, context):
if ctypes.windll.user32.IsWindowVisible(hwnd):
length = ctypes.windll.user32.GetWindowTextLengthW(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
ctypes.windll.user32.GetWindowTextW(hwnd, buff, length + 1)
if re.search(context["wildcard"], buff.value, flags=re.I) != None and not context["handle"]:
if context["order"] > 0:
context["order"] -= 1
else:
context["handle"] = hwnd
return True
data = {"wildcard": wildcard, "handle": None, "order": order}
ctypes.windll.user32.EnumWindows(EnumWindowsProc(callback), ctypes.py_object(data))
return data["handle"]