启动进程的服务不会显示 GUI C#
嘿,我正在尝试获取服务来启动我的程序,但它没有显示 GUI。该过程开始,但没有显示任何内容。我尝试启用“允许服务与桌面交互”,但仍然不起作用。 我的程序是一个计算机锁定设备,用于阻止未经授权的用户访问计算机。我正在运行带有 64 位操作系统的 Windows 7。
这是我的服务的代码:
protected override void OnStart(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.Start();
FileStream fs = new FileStream(@"C:\Users\David\Documents\Visual Studio 2010\Projects\LockPCService\LockPCService\bin\Debug\ServiceLog.dj",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" LockPCService: Service Started " + DateTime.Now + "\n" + "\n");
m_streamWriter.Flush();
m_streamWriter.Close();
}
protected override void OnStop()
{
FileStream fs = new FileStream(@"C:\Users\David\Documents\Visual Studio 2010\Projects\LockPCService\LockPCService\bin\Debug\ServiceLog.dj",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" LockPCService: Service Stopped " + DateTime.Now + "\n"); m_streamWriter.Flush();
m_streamWriter.Close();
}
为了尝试让服务正常工作,我使用 notepad.exe。当我查看记事本正在运行的进程时,但没有 GUI。此外,每次我运行 ServiceLog 时,它都会工作。
关于为什么这不起作用的任何想法?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
本文解释了
会话 0 隔离
,其中其他事情不允许服务在 Windows Vista/7 中创建 UI。在您的服务启动另一个进程时,它在会话 0 中启动,也不会显示任何 UI。 (顺便说一下,UI 已创建,只是 Session 0 从未显示)。 CodeProject 上的这篇文章可以帮助您从用户的桌面并显示其 UI。另外,请考虑将流对象包装在
using
语句中 以便妥善处置它们。This article explains
Session 0 Isolation
which among other things disallows services from creating a UI in Windows Vista/7. In your service starts another process, it starts in Session 0 and also will not show any UI. (By the way, the UI is created, it's just that Session 0 is never displayed). This article on CodeProject can help you create a process from a service on the user's desktop and show its UI.Also, please consider wrapping you stream objects in a
using
statement so that they are properly disposed.服务在不同的帐户下运行,因此记事本由另一个用户在另一个桌面上运行,这就是您看不到它的原因。从 Vista 开始,不再支持“允许服务与桌面交互”。
Services run under different account so notepad is run by another user and on another desktop so that's why you cannot see it. 'Allow service to interact with desktop' is not supported anymore starting from Vista.
我知道这是一篇迟到的文章,但我发现 这篇文章对我很有帮助。我运行的是 Windows 7,本文提供的解决方案效果很好。
如果您下载代码,会发现有一个名为
ApplicationLoader
的类。将该类包含在您的项目中,然后就这么简单:I know this is a late post, but I found that this article was very helpful to me. I am running Windows 7 and the solution provided in this article works great.
If you download the code, there is a class called
ApplicationLoader
. Include that class in your project and then it's as simple as this:服务在不同的登录会话中运行,并且与用户具有不同的窗口站。这意味着所有 GUI 活动都与用户的程序隔离,而不是服务无法显示 GUI。实际上,这种设计使得暂时阻止对用户程序的访问变得更加容易。
您需要调用 SwitchDesktop。
Services run in a different logon session and have a different window station from the user. That means that all GUI activity is segregated from the user's programs, not that the service can't display a GUI. Actually, this design makes it much easier to temporarily block access to the user's programs.
You'll need to call SwitchDesktop.