Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
 public static bool ChangeServiceConfig2(IntPtr serviceHandle, uint infoLevel, ref System.ServiceProcess.NativeMethods.SERVICE_DESCRIPTION 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);
            }
        }