Windows 会话 API 在 Windows XP 上未及时加载以及绕过它的方法
我正在编写一个服务应用程序,需要枚举所有当前用户会话并获取其会话 ID,然后查看哪个会话正在运行哪些进程。我使用 WTSEnumerateSessions()
API 以及 WTSQuerySessionInformation()
来获取会话特定信息。不幸的是,那些 WTS API 非常不可靠。
在关闭快速用户切换的 Windows XP 计算机上,或者在加入域的 XP 计算机上,这些 API 无法立即可用。在 Windows XP 启动和我的服务启动后的几分钟内,它们可能会失败并显示 RPC_S_INVALID_BINDING
或 1702 错误代码。
我找不到任何官方文档解释如何处理此类限制。通过搜索引擎可用的方法是等待终端服务加载,这当然是可能的,但实施起来却成为一个主要痛苦。
因此,如果有人能回答以下问题,我将不胜感激:
- 是否有任何替代 API 可以处理会话特定数据,比那些 WTS 更可靠?我主要需要查看计算机上的当前会话,获取用户名和会话状态。还可以枚举进程以及每个进程的会话 ID。 (我知道这是可能的,因为 GINA 或登录屏幕可以在终端服务加载之前完成所有这些操作。)
- 是否可以 100% 保证 WTS 类 API(例如 WTSEnumerateSessions() 、
WTSQuerySessionInformation()
和WTSEnumerateProcesses()
) 将在我的服务启动之前加载有任何版本的 Windows Vista/Windows 7 计算机吗?
请注意之前和任何非常重要的规定。
I'm writing a service application that needs to enumerate all current user sessions, and obtain their session IDs, and later see what processes are running for what session. I'm using the WTSEnumerateSessions()
API, as well as WTSQuerySessionInformation()
to obtain a session specific information. Unfortunately those WTS APIs are very unreliable.
On a Windows XP machine with Fast User Switching turned off, or on an XP machine joined to a domain, those APIs are not immediately available. They may fail with the RPC_S_INVALID_BINDING
, or 1702, error code for as long as several minutes after the Windows XP boots up and my service starts.
I could not find any official documentation explaining how to handle such limitation. The one available via a search engine is to wait for the terminal services service to load up, which is of course possible, but becomes a MAJOR pain in the a** to implement.
So, if someone could answer the following I'd appreciate it:
- Are there any alternative APIs to work with session specific data, that are more reliable that those WTS ones? I mostly need to see current sessions on the machine, get a user name and session status. Also enumerate processes with session IDs for each process. (I know that this is possible, since GINA or a log-in screen, can do all that way before the terminal services load up.)
- Is there a 100% guaratee that WTS-class APIs (such as
WTSEnumerateSessions()
,WTSQuerySessionInformation()
andWTSEnumerateProcesses()
) will load up before my service starts up on any version of Windows Vista/Windows 7 machine?
Please note bofore and any stipulations that a VERY important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此处建议了一些解决方案。总之:
There are a few solutions suggested here. In summary:
我认为您所问的#2 在 Vista/7 上是有保证的,因为这些 API 需要在任何会话上创建任何进程> 0。
I think the #2 you ask about is guaranteed on Vista/7 since those APIs are required to function to create any process on any session > 0.
另一个可能对您有帮助的 API 是 LsaEnumerateLogonSessions 和 LsaGetLogonSessionData SECURITY_LOGON_SESSION_DATA 具有
会话
字段。请参阅代码示例和<一个href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa379437%28v=VS.85%29.aspx" rel="nofollow noreferrer">这个。要获取有关进程会话的信息,您可以使用 GetTokenInformation,以TokenSessionId
作为参数。要枚举进程,您可以使用 NtQuerySystemInformation (看看我的旧答案)。Another API which could be helpful for you are LsaEnumerateLogonSessions and LsaGetLogonSessionData SECURITY_LOGON_SESSION_DATA having
Session
field. See the code example and this one. To get information about the session of the process you can use GetTokenInformation withTokenSessionId
as parameter. To enumerate processes you can use NtQuerySystemInformation (see my old answer).