views:

72

answers:

3

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  A: 

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.

Giorgi
Ah I see, how would I be able to run it with the current user?
Crazyd22
Even services running under the account of the logged in user would be in a different logon session and run in an isolation GUI environment. You need to read about Window Stations and Desktops. http://msdn.microsoft.com/en-us/library/ms681928.aspx
Ben Voigt
Also, this is true but not helping to solve the problem, so it should have been a comment not an answer.
Ben Voigt
+6  A: 

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.

Matthew Ferreira
For this task it's actually better not to use the user's desktop, because you're trying to block access to it.
Ben Voigt
Brilliant, will have a look at it, thanks!
Crazyd22
Its a form that is shown and locks the mouse inside it, so when they enter the password, the mouse will be released and they can use the computer again
Crazyd22
If you're trying to block access, you'd want to get your process running on the user's desktop. Then it can hook keyboard and mouse to block the desktop entirely or show some kind of blocking window to cover it up. SwitchDesktop would still allow someone to Ctrl+Alt+Del there way into killing the service. The injected process can disable all the options available after a Ctrl+Alt+Del (actually, the service can probably do this too).
Matthew Ferreira
I have disabled all of that through the program (not properly but you can't get the taskman to open). The only thing they can do is use the menu that opens when you press ctrl+alt+del as I am not sure how to disable that. But whatever they do the program will still be there.
Crazyd22
You can't disable Ctrl+Alt+Del, but you can disable or hide the options shown there. They are registry settings. You should be able to find them fairly easily. Consider killing taskman.exe and perhaps other alternate task managers (procexp.exe and procexp64.exe for Process Explorer) if you want to be thorough. I can say that I've done all of this for one of my company's product, basically a complete desktop lock, but I can't give you any code of course.
Matthew Ferreira
Okay thanks, I have disabled taskman.exe and will disable the other ones. Using that guide on CodeProject I have it working now, thanks! I have heard that to be able to disable them I would have to write my own keyboard driver but I have no idea how to do that. I didn't think that I was able to disable the options that you can choose from. I will try to find them in the registry. Is it in the same place in every registry? Thanks.
Crazyd22
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies, look under that registry key for useful options for disabling things after Ctrl+Alt+Del.
Matthew Ferreira
Brilliant, thanks!
Crazyd22
I have been playing around with my code and I was wondering if it is possible to make the service read the path of the process from a file? When I try to do this it starts and stops the service straight away? Any ideas?
Crazyd22
The path of what process?
Matthew Ferreira
The path of a process that I will be starting from the service (notepad.exe)
Crazyd22
Consider not using a separate process, but launching your service executable with either a special command line or have it use IPC to communicate with the service for instructions. If you must use a separate process, keep it in your current directory. If you must read the process path from a file, how about App.config?
Matthew Ferreira
Well basically the service is a seperate process in the root folder to my main program, the main program saves the directory of the process that it wants to run. Then the service reads this and starts up that process. But if I have them in the same folder and just type the name in, would that work? For example "\\run.exe"?
Crazyd22
I have got the service running and working properly, thank you for all your help! Much appreciated! :)
Crazyd22
Absolutely. That's what we're here for.
Matthew Ferreira
A: 

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.

Ben Voigt
You know, that little box that pops up when you downvote to suggest that you leave a comment explaining why, it's there for a reason. Answerers really do pay attention to rational feedback.
Ben Voigt