13
votes

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.

4

4 Answers

21
votes

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.

4
votes

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.

4
votes

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);
2
votes

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.