How to install a windows service that runs as an administrator?

I wrote an installer that installs a Windows service (A), which should start / stop another service (B). However, when A tries to start / stop B, I get this exception:

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

The installer installs the service as a local service and requests administrator rights through the UAC popup that I provide. I also added the app.manifest file to the service for which administrator privileges are set:

But I still get this error.

This is how I start the service (the stop is the same, except that 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();
}

?

+3
2

UAC. , UAC, , , . , - LocalSystem. , , .

+7

, services.msc , " ", , ? , LocalService . UAC, , , , .

+1

Source: https://habr.com/ru/post/1765701/


All Articles