启动进程的服务不会显示 GUI C#

发布于 2024-09-25 03:35:11 字数 1473 浏览 5 评论 0 原文

嘿,我正在尝试获取服务来启动我的程序,但它没有显示 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 时,它都会工作。

关于为什么这不起作用的任何想法?

谢谢。

Hey, I am trying to get a service to start my program but it isn't showing the GUI. The process starts but nothing is shown. I have tried enabling 'Allow service to interact with desktop' but that still isn't working.
My program is a computer locking device to stop unauthorised users from accessing the computer. I am running windows 7 with a 64 bit OS.

Here is the code for my service:

        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();
    }

To try and get the service working I am using notepad.exe. When I look at the processes notepad is running but there is no GUI. Also the ServiceLog is there and working each time I run it.

Any ideas on why this isn't working?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朦胧时间 2024-10-02 03:35:11

本文解释了会话 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.

迷爱 2024-10-02 03:35:11

服务在不同的帐户下运行,因此记事本由另一个用户在另一个桌面上运行,这就是您看不到它的原因。从 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.

闻呓 2024-10-02 03:35:11

我知道这是一篇迟到的文章,但我发现 这篇文章对我很有帮助。我运行的是 Windows 7,本文提供的解决方案效果很好。

如果您下载代码,会发现有一个名为 ApplicationLoader 的类。将该类包含在您的项目中,然后就这么简单:

// the name of the application to launch
String applicationName = "cmd.exe";

// launch the application
ApplicationLoader.PROCESS_INFORMATION procInfo;
ApplicationLoader.StartProcessAndBypassUAC(applicationName, out procInfo);

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:

// the name of the application to launch
String applicationName = "cmd.exe";

// launch the application
ApplicationLoader.PROCESS_INFORMATION procInfo;
ApplicationLoader.StartProcessAndBypassUAC(applicationName, out procInfo);
瑕疵 2024-10-02 03:35:11

服务在不同的登录会话中运行,并且与用户具有不同的窗口站。这意味着所有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文