在终端服务器上,服务如何在用户会话中启动进程?
从运行在终端服务器(在全局空间)上的 Windows 服务,我们希望能够启动一个在特定用户的终端服务器会话中运行 Windows 应用程序的进程。
如何去做这件事呢?
场景:Windows 服务在启动时启动。用户登录到终端服务器用户会话后,基于某些只有 Windows 服务已知的标准,Windows 服务希望在用户会话中启动一个进程来运行 Windows 应用程序。
示例:我们希望向用户显示“5 分钟后关闭”警告。 Windows 服务将检测到这种情况,并在每个用户会话中启动一个进程,该进程启动显示警告的 Windows 应用程序。而且,是的,我知道还有其他方式来显示警告对话框,这就是示例,我们想要做的更具侵入性。
From a Windows Service running on a Terminal Server (in global space), we would like to be able to start up a process running a windows application in a specific user's Terminal Server sessions.
How does one go about doing this?
The Scenerio: the windows service starts at boot time. After the user has logged into a Terminal Server user session, based on some criteria known only to the windows service, the windows service wants to start a process in the user's session running a windows application.
An example: We would like to display a 'Shutdown in 5 minutes' warning to the users. The windows service would detect this condition, and start up a process in each user session that starts the windows app that displays the warning. And, yes, I know there are other ways of displaying a warning dialog, this is the example, what we want to do is much more invasive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 CreateProcessAsUser 来执行此操作 - 但这需要一些努力。我相信以下步骤是基本的必需过程:
在启动进程后,您还需要确保清理所有适当的句柄、令牌等。
You can use CreateProcessAsUser to do this - but it requires a bit of effort. I believe the following steps are the basic required procedure:
You'll also want to make sure to clean up all of the appropriate handles, tokens, etc., after you've launched the process.
回复确实很晚,但也许有人会发现这有帮助。
您可以使用 PsExec 在远程(或本地)上启动应用程序)使用以下命令在指定会话内的服务器:
其中 SESSION_ID 表示启动应用程序的会话 ID。
您需要知道服务器上哪些会话处于活动状态以及哪个会话 ID 映射到哪个用户登录。以下线程为这个确切的问题提供了一个很好的代码示例: 如何检索 .NET 中已登录/已连接用户的列表?
Really late reply but maybe somebody will find this helpful.
You can use PsExec to launch an application on a remote (or local) server inside a specified session by using the following command:
Where SESSION_ID indicates the session id in which to launch the application.
You will need to know what sessions are active on the server and which session id maps to which user login. The following thread provides a nice code sample for this exact problem: How do you retrieve a list of logged-in/connected users in .NET?
较晚回复,但在上面的答案中 DuplicateToken 是不必要的,因为 WTSQueryUserToken 已经返回主令牌。
Late reply but in the answer above DuplicateToken is not necessary since WTSQueryUserToken already returns a primary token.