Windows 服务如何执行 GUI 应用程序?
我编写了一个 Windows 服务,允许我远程运行和停止应用程序.这些应用程序使用 CreateProcess 运行,这对我有用,因为它们中的大多数只执行后端处理.最近,我需要运行向当前登录用户显示 GUI 的应用程序.我如何使用 C++ 编写代码以允许我的服务定位当前活动的桌面并在其上运行 GUI?
I have written a Windows service that allows me to remotely run and stop applications. These applications are run using CreateProcess, and this works for me because most of them only perform backend processing. Recently, I need to run applications that present GUI to the current log in user. How do I code in C++ to allow my service to locate the currently active desktop and run the GUI on it?
推荐答案
Roger Lipscombe 的回答,使用 WTSEnumerateSessions 找到合适的桌面,然后 CreateProcessAsUser 在该桌面上启动应用程序(您将桌面句柄作为 STARTUPINFO 结构)是正确的.
Roger Lipscombe's answer, to use WTSEnumerateSessions to find the right desktop, then CreateProcessAsUser to start the application on that desktop (you pass it the handle of the desktop as part of the STARTUPINFO structure) is correct.
但是,我强烈建议不要这样做.在某些环境中,例如具有许多活跃用户的终端服务器主机,确定哪个桌面是活跃"桌面并不容易,甚至可能不可能.
However, I would strongly recommend against doing this. In some environments, such as Terminal Server hosts with many active users, determining which desktop is the 'active' one isn't easy, and may not even be possible.
但最重要的是,如果一个应用程序突然出现在用户的桌面上,这很可能发生在一个糟糕的时间(或者因为用户根本没有预料到它,或者因为你试图在会话尚未完全初始化,正在关闭或其他过程中).
But most importantly, if an application will suddenly appear on a user's desktop, this may very well occur at a bad time (either because the user simply isn't expecting it, or because you're trying to launch the app when the session isn't quite initialized yet, in the process of shutting down, or whatever).
更传统的方法是在全局启动组中为您的服务添加一个小型客户端应用程序的快捷方式.然后,此应用将与每个用户会话一起启动,并可用于启动其他应用(如果需要),而无需处理用户凭据、会话和/或桌面.
A more conventional approach would be to put a shortcut to a small client app for your service in the global startup group. This app will then launch along with every user session, and can be used start other apps (if so desired) without any juggling of user credentials, sessions and/or desktops.
此外,管理员可以根据需要移动/禁用此快捷方式,这将使您的应用程序的部署更加容易,因为它不会偏离其他 Windows 应用程序使用的标准...
Also, this shortcut can be moved/disabled by administrators as desired, which will make deployment of your application much easier, since it doesn't deviate from the standards used by other Windows apps...
相关文章