當前位置: 首頁>>代碼示例>>Python>>正文


Python ctypes.create_unicode_buffer方法代碼示例

本文整理匯總了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 
開發者ID:asweigart,項目名稱:nicewin,代碼行數:26,代碼來源:__init__.py

示例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 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:27,代碼來源:appdirs.py

示例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 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:appdirs.py

示例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) 
開發者ID:cb109,項目名稱:sublime3dsmax,代碼行數:18,代碼來源:winapi.py

示例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] 
開發者ID:bugatsinho,項目名稱:bugatsinho.github.io,代碼行數:18,代碼來源:_environ.py

示例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"") 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:23,代碼來源:test_unicode.py

示例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 
開發者ID:maldevel,項目名稱:gdog,代碼行數:22,代碼來源:client.py

示例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:\\' 
開發者ID:ntoll,項目名稱:uflash,代碼行數:23,代碼來源:test_uflash.py

示例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 
開發者ID:ntoll,項目名稱:uflash,代碼行數:18,代碼來源:test_uflash.py

示例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"] 
開發者ID:glitchassassin,項目名稱:lackey,代碼行數:22,代碼來源:PlatformManagerWindows.py


注:本文中的ctypes.create_unicode_buffer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。