/// <summary>Installs the service by writing service application information to the registry. This method is meant to be used by installation tools, which process the appropriate methods automatically.</summary> /// <param name="stateSaver">An <see cref="T:System.Collections.IDictionary" /> that contains the context information associated with the installation. </param> /// <exception cref="T:System.InvalidOperationException">The installation does not contain a <see cref="T:System.ServiceProcess.ServiceProcessInstaller" /> for the executable.-or- The file name for the assembly is null or an empty string.-or- The service name is invalid.-or- The Service Control Manager could not be opened. </exception> /// <exception cref="T:System.ArgumentException">The display name for the service is more than 255 characters in length.</exception> /// <exception cref="T:System.ComponentModel.Win32Exception">The system could not generate a handle to the service. -or-A service with that name is already installed.</exception> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" /> /// <IPermission class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> /// </PermissionSet> public override void Install(IDictionary stateSaver) { base.Context.LogMessage(Res.GetString("InstallingService", this.ServiceName)); try { ServiceInstaller.CheckEnvironment(); string servicesStartName = null; string password = null; ServiceProcessInstaller serviceProcessInstaller = null; if (base.Parent is ServiceProcessInstaller) { serviceProcessInstaller = (ServiceProcessInstaller)base.Parent; } else { int num = 0; while (num < base.Parent.Installers.Count) { if (!(base.Parent.Installers[num] is ServiceProcessInstaller)) { num++; continue; } serviceProcessInstaller = (ServiceProcessInstaller)base.Parent.Installers[num]; break; } } if (serviceProcessInstaller == null) { throw new InvalidOperationException(Res.GetString("NoInstaller")); } switch (serviceProcessInstaller.Account) { case ServiceAccount.LocalService: servicesStartName = "NT AUTHORITY\\LocalService"; break; case ServiceAccount.NetworkService: servicesStartName = "NT AUTHORITY\\NetworkService"; break; case ServiceAccount.User: servicesStartName = serviceProcessInstaller.Username; password = serviceProcessInstaller.Password; break; } string text = base.Context.Parameters["assemblypath"]; if (string.IsNullOrEmpty(text)) { throw new InvalidOperationException(Res.GetString("FileName")); } if (text.IndexOf('"') == -1) { text = "\"" + text + "\""; } if (!ServiceInstaller.ValidateServiceName(this.ServiceName)) { throw new InvalidOperationException(Res.GetString("ServiceName", this.ServiceName, 80.ToString(CultureInfo.CurrentCulture))); } if (this.DisplayName.Length > 255) { throw new ArgumentException(Res.GetString("DisplayNameTooLong", this.DisplayName)); } string dependencies = null; if (this.ServicesDependedOn.Length != 0) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < this.ServicesDependedOn.Length; i++) { string text2 = this.ServicesDependedOn[i]; try { text2 = new ServiceController(text2, ".").ServiceName; } catch { } stringBuilder.Append(text2); stringBuilder.Append('\0'); } stringBuilder.Append('\0'); dependencies = stringBuilder.ToString(); } IntPtr intPtr = SafeNativeMethods.OpenSCManager(null, null, 983103); IntPtr intPtr2 = IntPtr.Zero; if (intPtr == IntPtr.Zero) { throw new InvalidOperationException(Res.GetString("OpenSC", "."), new Win32Exception()); } int serviceType = 16; int num2 = 0; for (int j = 0; j < base.Parent.Installers.Count; j++) { if (base.Parent.Installers[j] is ServiceInstaller) { num2++; if (num2 > 1) { break; } } } if (num2 > 1) { serviceType = 32; } try { intPtr2 = NativeMethods.CreateService(intPtr, this.ServiceName, this.DisplayName, 983551, serviceType, (int)this.StartType, 1, text, null, IntPtr.Zero, dependencies, servicesStartName, password); if (intPtr2 == IntPtr.Zero) { throw new Win32Exception(); } if (this.Description.Length != 0) { NativeMethods.SERVICE_DESCRIPTION sERVICE_DESCRIPTION = default(NativeMethods.SERVICE_DESCRIPTION); sERVICE_DESCRIPTION.description = Marshal.StringToHGlobalUni(this.Description); bool num3 = NativeMethods.ChangeServiceConfig2(intPtr2, 1u, ref sERVICE_DESCRIPTION); Marshal.FreeHGlobal(sERVICE_DESCRIPTION.description); if (!num3) { throw new Win32Exception(); } } if (Environment.OSVersion.Version.Major > 5 && this.StartType == ServiceStartMode.Automatic) { NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO sERVICE_DELAYED_AUTOSTART_INFO = default(NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO); sERVICE_DELAYED_AUTOSTART_INFO.fDelayedAutostart = this.DelayedAutoStart; if (!NativeMethods.ChangeServiceConfig2(intPtr2, 3u, ref sERVICE_DELAYED_AUTOSTART_INFO)) { throw new Win32Exception(); } } stateSaver["installed"] = true; } finally { if (intPtr2 != IntPtr.Zero) { SafeNativeMethods.CloseServiceHandle(intPtr2); } SafeNativeMethods.CloseServiceHandle(intPtr); } base.Context.LogMessage(Res.GetString("InstallOK", this.ServiceName)); } finally { base.Install(stateSaver); } }
public override void Install(IDictionary stateSaver) { this.Context.LogMessage(System.ServiceProcess.Res.GetString("InstallingService", new object[1] { (object)this.ServiceName })); try { ServiceInstaller.CheckEnvironment(); string servicesStartName = (string)null; string password = (string)null; ServiceProcessInstaller processInstaller = (ServiceProcessInstaller)null; if (this.Parent is ServiceProcessInstaller) { processInstaller = (ServiceProcessInstaller)this.Parent; } else { for (int index = 0; index < this.Parent.Installers.Count; ++index) { if (this.Parent.Installers[index] is ServiceProcessInstaller) { processInstaller = (ServiceProcessInstaller)this.Parent.Installers[index]; break; } } } if (processInstaller == null) { throw new InvalidOperationException(System.ServiceProcess.Res.GetString("NoInstaller")); } switch (processInstaller.Account) { case ServiceAccount.LocalService: servicesStartName = "NT AUTHORITY\\LocalService"; break; case ServiceAccount.NetworkService: servicesStartName = "NT AUTHORITY\\NetworkService"; break; case ServiceAccount.User: servicesStartName = processInstaller.Username; password = processInstaller.Password; break; } string binaryPath = this.Context.Parameters["assemblypath"]; if (string.IsNullOrEmpty(binaryPath)) { throw new InvalidOperationException(System.ServiceProcess.Res.GetString("FileName")); } if (binaryPath.IndexOf('"') == -1) { binaryPath = "\"" + binaryPath + "\""; } if (!ServiceInstaller.ValidateServiceName(this.ServiceName)) { throw new InvalidOperationException(System.ServiceProcess.Res.GetString("ServiceName", (object)this.ServiceName, (object)80.ToString((IFormatProvider)CultureInfo.CurrentCulture))); } else if (this.DisplayName.Length > (int)byte.MaxValue) { throw new ArgumentException(System.ServiceProcess.Res.GetString("DisplayNameTooLong", new object[1] { (object)this.DisplayName })); } else { string dependencies = (string)null; if (this.ServicesDependedOn.Length > 0) { StringBuilder stringBuilder = new StringBuilder(); for (int index = 0; index < this.ServicesDependedOn.Length; ++index) { string name = this.ServicesDependedOn[index]; try { name = new ServiceController(name, ".").ServiceName; } catch { } stringBuilder.Append(name); stringBuilder.Append(char.MinValue); } stringBuilder.Append(char.MinValue); dependencies = ((object)stringBuilder).ToString(); } IntPtr num1 = SafeNativeMethods.OpenSCManager((string)null, (string)null, 983103); IntPtr num2 = IntPtr.Zero; if (num1 == IntPtr.Zero) { throw new InvalidOperationException(System.ServiceProcess.Res.GetString("OpenSC", new object[1] { (object)"." }), (Exception) new Win32Exception()); } else { int serviceType = 16; int num3 = 0; for (int index = 0; index < this.Parent.Installers.Count; ++index) { if (this.Parent.Installers[index] is ServiceInstaller) { ++num3; if (num3 > 1) { break; } } } if (num3 > 1) { serviceType = 32; } try { num2 = System.ServiceProcess.NativeMethods.CreateService(num1, this.ServiceName, this.DisplayName, 983551, serviceType, (int)this.StartType, 1, binaryPath, (string)null, IntPtr.Zero, dependencies, servicesStartName, password); if (num2 == IntPtr.Zero) { throw new Win32Exception(); } if (this.Description.Length != 0) { System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION serviceDesc = new System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION(); serviceDesc.description = Marshal.StringToHGlobalUni(this.Description); bool flag = System.ServiceProcess.NativeMethods.ChangeServiceConfig2(num2, 1U, ref serviceDesc); Marshal.FreeHGlobal(serviceDesc.description); if (!flag) { throw new Win32Exception(); } } if (Environment.OSVersion.Version.Major > 5 && this.StartType == ServiceStartMode.Automatic) { if (!System.ServiceProcess.NativeMethods.ChangeServiceConfig2(num2, 3U, ref new System.ServiceProcess.NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO() { fDelayedAutostart = this.DelayedAutoStart })) { throw new Win32Exception(); } } stateSaver[(object)"installed"] = (object)true; } finally { if (num2 != IntPtr.Zero) { SafeNativeMethods.CloseServiceHandle(num2); } SafeNativeMethods.CloseServiceHandle(num1); } this.Context.LogMessage(System.ServiceProcess.Res.GetString("InstallOK", new object[1] { (object)this.ServiceName })); } } } finally { base.Install(stateSaver); } }