none
Starting a service throws: Error 5: Access denied

    Question

  • Hello,

    Please need some help for Starting a Service written in C#:

    • I wrote a small example of writing services with C#.
    • I built the project.
    • I install the service with installUtil (that I run as administrator). Is was successful.
    • When I tried to start the service, I got: "Windows could not start the myService Service on Local Computer. Error 5: Access denied"
    • This happens in W7 and also with XP.
    • I opened the cmd box and the Service window as Admin (Run as Administrator) (local system). I also logged in as Administrator.  I do not know what else to do.

    Appreciate a hint

    Thanks a alot

    Friday, August 10, 2012 11:32 PM

Answers

  • Try use this NuGet plugin to assist debug and see if you can find anything abnormal.

    It's strange that the service fails to run even if OnStart code is removed. Maybe if you still can't find source of problem, you can try posting your Main() content here.

    Monday, August 13, 2012 2:18 AM

All replies

  • I think the "Access denied" error could be emitted by your service ifself.

    Check to see if your service emit any output (including file and Windows event log), and if so, check whether the account being used to run to service have right to write to them.

    If your service is supported to be run as console application or GUI program, you should try to run it directly in the account you're going to run in order to check for this possibility.

    Saturday, August 11, 2012 1:48 AM
  • Thanks a lot Cheong. Unfortunately the problem persists.

    I removed all the code in the "onStart" and "onStop" methods. The program does nothing now. However I still get the Error 5 mentioned in my first post.

    Any other suggestions?

    Thanks and best regards

    Saverio

    Saturday, August 11, 2012 4:50 AM
  • What kind of Service you are trying to start.

    Is it some application???

    Saturday, August 11, 2012 7:00 AM
  • Hello,

    I am guessing it is a Window Service. can you change the account under which this service run and check how it goes

    Thanks


    If this post is helpful, please vote as helpful. If this answers your question, please Mark as the Answer.

    Saturday, August 11, 2012 7:09 AM
  • Add a System.Serviceprocess as reference to your solution

    include headerfile as Using System.ServiceProcess

    This is my part of Code where i am starting and stopping a service:Print Spooler

      private void button3_Click(object sender, EventArgs e)
            {
                ServiceController startService = new ServiceController();
                startService.DisplayName = "Print Spooler";
                switch (startService.Status.ToString())
                {
                    case "Stopped":
                        startService.Start();
                        break;

                    case "Running":
                        startService.Stop();
                        break;
                }
            }

    There are some services that are reluctant to start and stop because of certain restrictions such as administrator privileges or dependencies on other applications. That you need to check with system configuration.

    But as of now this above piece of code will control print spooler

    If you want to create a new service of your own you can refer this 

    http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.100).aspx

    With Warm Regards

    Amit 


    • Edited by amit_kumar Saturday, August 11, 2012 7:44 AM
    Saturday, August 11, 2012 7:20 AM
  • Try use this NuGet plugin to assist debug and see if you can find anything abnormal.

    It's strange that the service fails to run even if OnStart code is removed. Maybe if you still can't find source of problem, you can try posting your Main() content here.

    Monday, August 13, 2012 2:18 AM
  • Can you change the service account property to LocalSystem ?

    programmer on .net

    • Proposed as answer by Dontgoogleme Monday, February 10, 2014 9:31 AM
    Monday, August 13, 2012 8:36 AM
  • This works for me
    Monday, February 10, 2014 9:32 AM
  • Mind you, if your service is not designed for running as LocalSystem (therefore is very careful in validating all kinds of input) and has any open access to network (including loopback interface for the less extent, but also feasible target), you can open the machine for "remote code execution"/ "privilege elevation" types of attacks.
    • Edited by cheong00 Wednesday, February 12, 2014 1:19 AM
    Wednesday, February 12, 2014 1:18 AM