我有下面的代码来获取与会话id相关联的用户名,但在通过 "WTSEnumerateSessionsW "来获取SessionId和State的数组时遇到了问题。
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's和State's的数组时遇到了问题。
我已经到了。
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的ctypes,通过引用传递双指针 我的问题得以解决。
这里是解决方案。
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
写出问题帮助我想到了更好的方法来寻找我需要完成的事情。