简体   繁体   English

如何获取以用户身份运行的Windows 7应用程序的MainWindowHandle <foo> 从作为本地系统运行的服务中?

[英]How can I get the MainWindowHandle of a Windows 7 application running as user <foo> from within a service running as Local System?

I've created a service that runs as the Local System user. 我创建了一个以“本地系统”用户身份运行的服务。 This service launches and monitors a Silverlight Out-of-browser application using native interop and the CreateProcessAsUser() method (to run it as the currently logged-in user, rather than Local System). 此服务使用本机互操作和CreateProcessAsUser()方法启动并监视Silverlight浏览器外应用程序(以当前登录用户(而不是本地系统)的身份运行它)。 I'm able to get a handle on the spawned Process and do things like Kill() it, however, I've become aware that the service is unable to get a handle to the main window of the child application because the child application is running as a different user. 我能够在生成的Process上获得一个句柄,并执行类似Kill()的操作,但是,我知道该服务无法获得子应用程序主窗口的句柄,因为子应用程序是以其他用户身份运行。 I'm running on Windows 7. 我在Windows 7上运行。

My end goal is to respond properly to when the Process stops responding (ie Process.Responding == false) so that I can kill the application and restart it. 我的最终目标是对Process停止响应的时间做出正确响应(即Process.Responding == false),以便我可以终止该应用程序并重新启动它。 However, Process.Responding requires a handle to the main window of the process (Process.MainWindowHandle, to be exact), however, in this scenario, Process.MainWindowHandle always returns 0. 但是,Process.Responding需要处理主窗口的句柄(确切地说是Process.MainWindowHandle),但是,在这种情况下,Process.MainWindowHandle始终返回0。

I'm stumped here. 我被困在这里。 Is there any way for one user to get a window handle to a process running as another user in Win 7? 在Win 7中,一个用户有什么方法可以获取以另一个用户身份运行的进程的窗口句柄?

Thanks in advance for any and all help. 在此先感谢您提供的所有帮助。

No, that's not possible. 不,那不可能。 Windows Services are completely isolated from user-mode applications for security reasons. 出于安全原因,Windows服务与用户模式应用程序完全隔离。 If you could get the handle to a window, you could manipulate and otherwise interact with that window, leaving open a huge security vulnerability. 如果您可以获取窗口的句柄,则可以操纵该窗口并与之交互,从而留下一个巨大的安全漏洞。

More information is available here: 更多信息请点击这里:

Strictly speaking, what you're using the Windows Service for in the first place is bad design. 严格地说,你首先使用Windows服务,什么是坏的设计。 It shouldn't be creating or launching any user-mode processes, or interacting with the user in any way. 它不应该创建或启动任何用户模式的进程,也不应该以任何方式与用户交互。 Remember that services run even when there is no user logged in! 请记住,即使没有用户登录,服务也会运行! Thus, they shouldn't be starting applications. 因此,他们不应该启动应用程序。

A better solution is a simple background application, set to launch automatically when the user logs in. This background application could then launch the Silverlight application, monitor its state, and interact with it as necessary, because both would be running under the context of the same local user account. 更好的解决方案是一个简单的后台应用程序,该后台应用程序设置为在用户登录时自动启动。然后,该后台应用程序可以启动Silverlight应用程序,监视其状态并根据需要与之交互,因为这两个应用程序都将在以下情况下运行:相同的本地用户帐户。 The effect is similar to a service, but without any of the drawbacks of isolation. 效果类似于服务,但没有隔离的任何缺点。 The easiest way to do this in Visual Studio is to create a WinForms application (or possibly a WPF application, I have less experience in that area) that simply doesn't show any forms/windows. 在Visual Studio中执行此操作的最简单方法是创建一个WinForms应用程序(或者可能是WPF应用程序,我在该领域经验较少),该应用程序根本不显示任何窗体/窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从以SYSTEM身份运行的服务中启动可与用户交互的应用程序 - Starting application from service running as SYSTEM that can interact with the user 通过在本地配置文件下运行服务,从Windows服务获取用户appdata路径 - Get user appdata path from windows service by running service under local profile 在本地系统下运行的Windows服务能否在其运行的计算机上创建新文件? - Can a Windows service running under Local System create new files on the computer it is running on? 如何验证 Windows 服务是否正在运行 - How can I verify if a Windows Service is running 从服务运行msiexec(本地系统帐户) - Running msiexec from a service (Local System account) 如何使用C#Windows Service获取正在运行的应用程序 - How to Get Running Application with C# Windows Service 如何获取正在运行的Windows Service应用程序的命令行参数? - How to get the command-line arguments of a running windows service application? 如何从MainWindowHandle获取表单? - How to get a form from MainWindowHandle? 我可以通过以应用程序池标识运行的IIS应用程序停止和启动Windows服务吗? - Can I stop and start a windows service from IIS app running under application pool identity? 我们可以访问在本地系统下运行的Windows服务中的网络文件吗? - Can we access network file in windows service running under local system?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM