13

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.

Crazyd22
  • 783
  • 5
  • 10
  • 24

4 Answers4

21

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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • 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 Sep 26 '10 at 16:14
  • 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 Sep 26 '10 at 16:17
  • 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). –  Sep 26 '10 at 16:20
  • 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 Sep 26 '10 at 16:25
  • 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. –  Sep 26 '10 at 16:28
  • 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 Sep 26 '10 at 16:41
  • HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies, look under that registry key for useful options for disabling things after Ctrl+Alt+Del. –  Sep 26 '10 at 16:54
  • 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 Sep 26 '10 at 17:50
  • The path of a process that I will be starting from the service (notepad.exe) – Crazyd22 Sep 26 '10 at 18:41
  • 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? –  Sep 26 '10 at 18:46
  • 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 Sep 26 '10 at 18:50
  • I have got the service running and working properly, thank you for all your help! Much appreciated! :) – Crazyd22 Sep 26 '10 at 18:54
  • Absolutely. That's what we're here for. –  Sep 26 '10 at 19:30
4

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);
davehale23
  • 4,374
  • 2
  • 27
  • 40
4

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
  • 30,270
  • 13
  • 89
  • 125
  • Ah I see, how would I be able to run it with the current user? – Crazyd22 Sep 26 '10 at 16:10
  • 1
    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 Sep 26 '10 at 16:15
  • 1
    Also, this is true but not helping to solve the problem, so it should have been a comment not an answer. – Ben Voigt Sep 26 '10 at 16:17
2

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
  • 277,958
  • 43
  • 419
  • 720
  • 8
    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 Sep 27 '10 at 04:14