Windows服务如何执行GUI应用程序? [英] How can a Windows service execute a GUI application?

查看:525
本文介绍了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's answer,使用 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应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆