Windows 会话 API 在 Windows XP 上未及时加载以及绕过它的方法

发布于 2024-12-12 17:02:17 字数 840 浏览 1 评论 0原文

我正在编写一个服务应用程序,需要枚举所有当前用户会话并获取其会话 ID,然后查看哪个会话正在运行哪些进程。我使用 WTSEnumerateSessions() API 以及 WTSQuerySessionInformation() 来获取会话特定信息。不幸的是,那些 WTS API 非常不可靠。

在关闭快速用户切换的 Windows XP 计算机上,或者在加入域的 XP 计算机上,这些 API 无法立即可用。在 Windows XP 启动和我的服务启动后的几分钟内,它们可能会失败并显示 RPC_S_INVALID_BINDING 或 1702 错误代码。

我找不到任何官方文档解释如何处理此类限制。通过搜索引擎可用的方法是等待终端服务加载,这当然是可能的,但实施起来却成为一个主要痛苦。

因此,如果有人能回答以下问题,我将不胜感激:

  1. 是否有任何替代 API 可以处理会话特定数据,比那些 WTS 更可靠?我主要需要查看计算机上的当前会话,获取用户名和会话状态。还可以枚举进程以及每个进程的会话 ID。 (我知道这是可能的,因为 GINA 或登录屏幕可以在终端服务加载之前完成所有这些操作。)
  2. 是否可以 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:

  1. 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.)
  2. Is there a 100% guaratee that WTS-class APIs (such as WTSEnumerateSessions(), WTSQuerySessionInformation() and WTSEnumerateProcesses()) 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懒猫 2024-12-19 17:02:18

此处建议了一些解决方案。总之:

  1. 确保“TermSrv”正在运行(通过依赖项、手动启动/等待等)。
  2. 等待“Global\TermSrvReadyEvent”事件设置。

There are a few solutions suggested here. In summary:

  1. Make sure "TermSrv" is running (via dependency, manually starting/waiting for it, etc).
  2. Wait for the "Global\TermSrvReadyEvent" event to be set.
煮酒 2024-12-19 17:02:18

我认为您所问的#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.

江心雾 2024-12-19 17:02:17

另一个可能对您有帮助的 API 是 LsaEnumerateLogonSessionsLsaGetLogonSessionData 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 with TokenSessionId as parameter. To enumerate processes you can use NtQuerySystemInformation (see my old answer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文