問題描述
我編寫了一個 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...
這篇關于Windows 服務如何執行 GUI 應用程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!