Ejemplo n.º 1
0
 public static bool ChangeServiceConfig2(IntPtr serviceHandle, uint infoLevel, ref System.ServiceProcess.NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO serviceDesc);
        public override void Install(IDictionary stateSaver)
        {
            base.Context.LogMessage(System.ServiceProcess.Res.GetString("InstallingService", new object[] { this.ServiceName }));
            try
            {
                CheckEnvironment();
                string servicesStartName       = null;
                string password                = null;
                ServiceProcessInstaller parent = null;
                if (base.Parent is ServiceProcessInstaller)
                {
                    parent = (ServiceProcessInstaller)base.Parent;
                }
                else
                {
                    for (int j = 0; j < base.Parent.Installers.Count; j++)
                    {
                        if (base.Parent.Installers[j] is ServiceProcessInstaller)
                        {
                            parent = (ServiceProcessInstaller)base.Parent.Installers[j];
                            break;
                        }
                    }
                }
                if (parent == null)
                {
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("NoInstaller"));
                }
                switch (parent.Account)
                {
                case ServiceAccount.LocalService:
                    servicesStartName = @"NT AUTHORITY\LocalService";
                    break;

                case ServiceAccount.NetworkService:
                    servicesStartName = @"NT AUTHORITY\NetworkService";
                    break;

                case ServiceAccount.User:
                    servicesStartName = parent.Username;
                    password          = parent.Password;
                    break;
                }
                string str3 = base.Context.Parameters["assemblypath"];
                if (string.IsNullOrEmpty(str3))
                {
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("FileName"));
                }
                if (str3.IndexOf('"') == -1)
                {
                    str3 = "\"" + str3 + "\"";
                }
                if (!ValidateServiceName(this.ServiceName))
                {
                    object[] args = new object[] { this.ServiceName, 80.ToString(CultureInfo.CurrentCulture) };
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("ServiceName", args));
                }
                if (this.DisplayName.Length > 0xff)
                {
                    throw new ArgumentException(System.ServiceProcess.Res.GetString("DisplayNameTooLong", new object[] { this.DisplayName }));
                }
                string dependencies = null;
                if (this.ServicesDependedOn.Length > 0)
                {
                    StringBuilder builder = new StringBuilder();
                    for (int k = 0; k < this.ServicesDependedOn.Length; k++)
                    {
                        string name = this.ServicesDependedOn[k];
                        try
                        {
                            ServiceController controller = new ServiceController(name, ".");
                            name = controller.ServiceName;
                        }
                        catch
                        {
                        }
                        builder.Append(name);
                        builder.Append('\0');
                    }
                    builder.Append('\0');
                    dependencies = builder.ToString();
                }
                IntPtr databaseHandle = SafeNativeMethods.OpenSCManager(null, null, 0xf003f);
                IntPtr zero           = IntPtr.Zero;
                if (databaseHandle == IntPtr.Zero)
                {
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("OpenSC", new object[] { "." }), new Win32Exception());
                }
                int serviceType = 0x10;
                int num4        = 0;
                for (int i = 0; i < base.Parent.Installers.Count; i++)
                {
                    if (base.Parent.Installers[i] is ServiceInstaller)
                    {
                        num4++;
                        if (num4 > 1)
                        {
                            break;
                        }
                    }
                }
                if (num4 > 1)
                {
                    serviceType = 0x20;
                }
                try
                {
                    zero = System.ServiceProcess.NativeMethods.CreateService(databaseHandle, this.ServiceName, this.DisplayName, 0xf01ff, serviceType, (int)this.StartType, 1, str3, null, IntPtr.Zero, dependencies, servicesStartName, password);
                    if (zero == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }
                    if (this.Description.Length != 0)
                    {
                        System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION serviceDesc = new System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION {
                            description = Marshal.StringToHGlobalUni(this.Description)
                        };
                        bool flag = System.ServiceProcess.NativeMethods.ChangeServiceConfig2(zero, 1, ref serviceDesc);
                        Marshal.FreeHGlobal(serviceDesc.description);
                        if (!flag)
                        {
                            throw new Win32Exception();
                        }
                    }
                    if ((Environment.OSVersion.Version.Major > 5) && (this.StartType == ServiceStartMode.Automatic))
                    {
                        System.ServiceProcess.NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO service_delayed_autostart_info = new System.ServiceProcess.NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO {
                            fDelayedAutostart = this.DelayedAutoStart
                        };
                        if (!System.ServiceProcess.NativeMethods.ChangeServiceConfig2(zero, 3, ref service_delayed_autostart_info))
                        {
                            throw new Win32Exception();
                        }
                    }
                    stateSaver["installed"] = true;
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        SafeNativeMethods.CloseServiceHandle(zero);
                    }
                    SafeNativeMethods.CloseServiceHandle(databaseHandle);
                }
                base.Context.LogMessage(System.ServiceProcess.Res.GetString("InstallOK", new object[] { this.ServiceName }));
            }
            finally
            {
                base.Install(stateSaver);
            }
        }
        public override void Install(IDictionary stateSaver)
        {
            base.Context.LogMessage(System.ServiceProcess.Res.GetString("InstallingService", new object[] { this.ServiceName }));
            try
            {
                CheckEnvironment();
                string servicesStartName = null;
                string password = null;
                ServiceProcessInstaller parent = null;
                if (base.Parent is ServiceProcessInstaller)
                {
                    parent = (ServiceProcessInstaller) base.Parent;
                }
                else
                {
                    for (int j = 0; j < base.Parent.Installers.Count; j++)
                    {
                        if (base.Parent.Installers[j] is ServiceProcessInstaller)
                        {
                            parent = (ServiceProcessInstaller) base.Parent.Installers[j];
                            break;
                        }
                    }
                }
                if (parent == null)
                {
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("NoInstaller"));
                }
                switch (parent.Account)
                {
                    case ServiceAccount.LocalService:
                        servicesStartName = @"NT AUTHORITY\LocalService";
                        break;

                    case ServiceAccount.NetworkService:
                        servicesStartName = @"NT AUTHORITY\NetworkService";
                        break;

                    case ServiceAccount.User:
                        servicesStartName = parent.Username;
                        password = parent.Password;
                        break;
                }
                string str3 = base.Context.Parameters["assemblypath"];
                if (string.IsNullOrEmpty(str3))
                {
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("FileName"));
                }
                if (str3.IndexOf('"') == -1)
                {
                    str3 = "\"" + str3 + "\"";
                }
                if (!ValidateServiceName(this.ServiceName))
                {
                    object[] args = new object[] { this.ServiceName, 80.ToString(CultureInfo.CurrentCulture) };
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("ServiceName", args));
                }
                if (this.DisplayName.Length > 0xff)
                {
                    throw new ArgumentException(System.ServiceProcess.Res.GetString("DisplayNameTooLong", new object[] { this.DisplayName }));
                }
                string dependencies = null;
                if (this.ServicesDependedOn.Length > 0)
                {
                    StringBuilder builder = new StringBuilder();
                    for (int k = 0; k < this.ServicesDependedOn.Length; k++)
                    {
                        string name = this.ServicesDependedOn[k];
                        try
                        {
                            ServiceController controller = new ServiceController(name, ".");
                            name = controller.ServiceName;
                        }
                        catch
                        {
                        }
                        builder.Append(name);
                        builder.Append('\0');
                    }
                    builder.Append('\0');
                    dependencies = builder.ToString();
                }
                IntPtr databaseHandle = SafeNativeMethods.OpenSCManager(null, null, 0xf003f);
                IntPtr zero = IntPtr.Zero;
                if (databaseHandle == IntPtr.Zero)
                {
                    throw new InvalidOperationException(System.ServiceProcess.Res.GetString("OpenSC", new object[] { "." }), new Win32Exception());
                }
                int serviceType = 0x10;
                int num4 = 0;
                for (int i = 0; i < base.Parent.Installers.Count; i++)
                {
                    if (base.Parent.Installers[i] is ServiceInstaller)
                    {
                        num4++;
                        if (num4 > 1)
                        {
                            break;
                        }
                    }
                }
                if (num4 > 1)
                {
                    serviceType = 0x20;
                }
                try
                {
                    zero = System.ServiceProcess.NativeMethods.CreateService(databaseHandle, this.ServiceName, this.DisplayName, 0xf01ff, serviceType, (int) this.StartType, 1, str3, null, IntPtr.Zero, dependencies, servicesStartName, password);
                    if (zero == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }
                    if (this.Description.Length != 0)
                    {
                        System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION serviceDesc = new System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION {
                            description = Marshal.StringToHGlobalUni(this.Description)
                        };
                        bool flag = System.ServiceProcess.NativeMethods.ChangeServiceConfig2(zero, 1, ref serviceDesc);
                        Marshal.FreeHGlobal(serviceDesc.description);
                        if (!flag)
                        {
                            throw new Win32Exception();
                        }
                    }
                    if ((Environment.OSVersion.Version.Major > 5) && (this.StartType == ServiceStartMode.Automatic))
                    {
                        System.ServiceProcess.NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO service_delayed_autostart_info = new System.ServiceProcess.NativeMethods.SERVICE_DELAYED_AUTOSTART_INFO {
                            fDelayedAutostart = this.DelayedAutoStart
                        };
                        if (!System.ServiceProcess.NativeMethods.ChangeServiceConfig2(zero, 3, ref service_delayed_autostart_info))
                        {
                            throw new Win32Exception();
                        }
                    }
                    stateSaver["installed"] = true;
                }
                finally
                {
                    if (zero != IntPtr.Zero)
                    {
                        SafeNativeMethods.CloseServiceHandle(zero);
                    }
                    SafeNativeMethods.CloseServiceHandle(databaseHandle);
                }
                base.Context.LogMessage(System.ServiceProcess.Res.GetString("InstallOK", new object[] { this.ServiceName }));
            }
            finally
            {
                base.Install(stateSaver);
            }
        }