4

I've written an installer that installs a windows service (A) that needs to start/stop another service (B). However, when A tries to start/stop B, I get this exception:

System.InvalidOperationException: Cannot open MyService service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied

The installer installs the service as a local service, and it requests admin rights via the UAC pop-up, which I grant. I've also added an app.manifest file to the service that is set to ask for admin rights:

Yet I'm still getting that error.

This is how I start the service (stopping is the same, except it calls Stop, of course):

using (Mutex mutex = new Mutex(false, "MyServiceLock"))
{
    mutex.WaitOne();

    if (ServiceExists(serviceName) == true)
    {
        using (ServiceController serviceController = new ServiceController(serviceName, "."))
        {
            serviceController.Start(); // this line throws the exception
        }
    }

    mutex.ReleaseMutex();
}

Why might access to this service be denied?

Mike Pateras
  • 14,715
  • 30
  • 97
  • 137

2 Answers2

11

A service cannot ask for a UAC elevation. It sounds to me that the UAC prompt you describe is actually requested by the installer, not the service. Services normally run with a very privileged account already, LocalSystem by default. Do make sure that you configure the service to use such a privileged account, not a restricted user account.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

As a quick test, if you open up services.msc and check your server to "run as" and enter your credentials, does the error go away? It may be that the LocalService does not have access to stop other services. Providing the UAC prompt permission is likely only allowing you to install the service in the first place, not telling it to run as administrator.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
Nate
  • 30,286
  • 23
  • 113
  • 184
  • The issue does seem to go away. So how can I get this service to run as an admin, then? I'm not going to know the proper admin credentials. – Mike Pateras Sep 20 '10 at 20:15