【问题标题】:Python Ctypes enumerate SessionId'sPython Ctypes 枚举 SessionId 的
【发布时间】:2020-08-24 17:49:09
【问题描述】:

我有以下代码来获取与会话 ID 关联的用户名:

ppBuffer = ctypes.c_wchar_p()
pBytesReturned = ctypes.c_ulong()
ctypes.windll.wtsapi32.WTSQuerySessionInformationW(0, SessionId, 5, ctypes.byref(ppBuffer), ctypes.byref(pBytesReturned))
logging.info(f'Username: {ppBuffer.value}')

我在尝试通过“WTSEnumerateSessionsW”枚举以获取 SessionId 和 State 的数组时遇到问题

我已经做到了:

class WTS_SESSION_INFOW(ctypes.Structure):
    _fields_ = [("SessionId", ctypes.c_ulong),
                ("pWinStationName", ctypes.c_wchar_p),
                ("State", ctypes.c_int)]

ppSessionInfo = WTS_SESSION_INFOW()
pCount = ctypes.c_ulong()
ctypes.windll.wtsapi32.WTSEnumerateSessionsW(0, 0, 1, ctypes.byref(ppSessionInfo), ctypes.byref(pCount))

pCount.value 返回运行的正确实例数,但是 ppSessionInfo.SessionId 返回一个与任何当前会话 ID 都不匹配的大整数。

MS 文档说 ppSessionInfo 应该是 WTS_SESSION_INFOW 结构的数组,但是我不知道如何实现这一点?

我可以使用 win32ts.WTSEnumerateSessions() 和 win32ts.WTSQuerySessionInformation() 完成上述操作,但是我仅限于可以使用的导入。

任何帮助将不胜感激。

【问题讨论】:

    标签: python-3.x windows ctypes


    【解决方案1】:

    在这篇文章的帮助下:python ctypes, pass double pointer by reference 我能够解决我的问题。

    解决办法如下:

    ppSessionInfo = ctypes.POINTER(WTS_SESSION_INFOW)()
    pCount = ctypes.c_ulong()
    ctypes.windll.wtsapi32.WTSEnumerateSessionsW(0, 0, 1, ctypes.byref(ppSessionInfo), ctypes.byref(pCount))
    
    for index in range(pCount.value):
        ppSessionInfo[index].SessionId
        ppSessionInfo[index].State
        ppSessionInfo[index].pWinStationName
    

    写出问题帮助我想出更好的方法来搜索我需要完成的任务。

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      相关资源
      最近更新 更多