Similar to https://stackoverflow.com/a/467565/774828 which shows how to start and stop an existing Windows service on a remote machine, I am trying to install a Windows Service on a remote machine. I can start and stop the services on the remote machine fine, but I can't find how to install a new service remotely, without resorting to calling the sc.exe process (https://stackoverflow.com/a/1159059/774828) which I want to avoid if possible.
I know there is a System.Configuration.Install.ManagedInstallerClass.InstallHelper method, but I cannot get this to work to install on a remote machine. And, I cannot figure out if I can use System.ServiceProcess.ServiceInstaller or System.ServiceProcess.ServiceProcessInstaller.
The C# code to do this is as follows:
Safe Handle
When dealing with unmanaged handles, you should use SafeHandles which ensure that resources are freed correctly in C#.
P/Invoke signatures
All of the signatures for the functions you need can be found on http://pinvoke.net, and the entire Service Control API can be found here on MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms685942(v=vs.85).aspx