Beispiel #1
0
 internal static extern IntPtr CreateService(IntPtr scHandle,
                                             string lpSvcName,
                                             string lpDisplayName,
                                             ServiceInstaller.ServiceAccessType dwDesiredAccess,
                                             ServicesAPI.ServiceType dwServiceType,
                                             ServiceInstaller.ServiceStartType dwStartType,
                                             ServiceInstaller.ServiceErrorControl dwErrorControl,
                                             string lpPathName,
                                             string lpLoadOrderGroup,
                                             IntPtr lpdwTagId,
                                             string lpDependencies,
                                             string lpServiceStartName,
                                             string lpPassword);
Beispiel #2
0
 ///<summary>Describes a new Win32 service.</summary>
 /// <param name="Name">The name of the service used in the service database.</param>
 /// <param name="DisplayName">The name of the service that will be displayed in the services snap-in.</param>
 /// <param name="Description">The description of the service that will be displayed in the service snap-in.</param>
 /// <param name="Run">Indicates if you want the service to run or not on program startup.</param>
 /// <param name="ServiceType">Indicates the type of service you will be running. By default this is "Default."</param>
 /// <param name="ServiceAccessType">Access to the service. Before granting the requested access, the system checks the access token of the calling process.</param>
 /// <param name="ServiceStartType">Service start options. By default this is "AutoStart."</param>
 /// <param name="ServiceErrorControl">Severity of the error, and action taken, if this service fails to start.</param>
 /// <param name="ServiceControls">The controls or actions the service responds to.</param>
 public ServiceAttribute(
     string Name,
     string DisplayName = null,
     string Description = null,
     bool Run           = true,
     ServicesAPI.ServiceType ServiceType = ServicesAPI.ServiceType.SERVICE_WIN32_OWN_PROCESS,
     ServiceInstaller.ServiceAccessType ServiceAccessType     = ServiceInstaller.ServiceAccessType.SERVICE_ALL_ACCESS,
     ServiceInstaller.ServiceStartType ServiceStartType       = ServiceInstaller.ServiceStartType.SERVICE_AUTO_START,
     ServiceInstaller.ServiceErrorControl ServiceErrorControl = ServiceInstaller.ServiceErrorControl.SERVICE_ERROR_NORMAL,
     ServicesAPI.ControlsAccepted ServiceControls             = ServicesAPI.ControlsAccepted.SERVICE_ACCEPT_ALL)
 {
     this.Name                = Name;
     this.DisplayName         = DisplayName ?? this.Name;
     this.Description         = Description ?? this.Name;
     this.Run                 = Run;
     this.ServiceType         = ServiceType;
     this.ServiceAccessType   = ServiceAccessType;// ServiceInstaller.ServiceAccessType.SERVICE_ALL_ACCESS;
     this.ServiceStartType    = ServiceStartType;
     this.ServiceErrorControl = ServiceErrorControl;
     this.ServiceControls     = ServiceControls;
     this.LogName             = "Services";
 }
Beispiel #3
0
        /// <summary>
        /// Creates a service object and adds it to the specified service control manager database.
        /// </summary>
        /// <param name="ServicePath">
        /// The fully qualified path to the service binary file. If the path contains a space, it must be quoted so that it is correctly interpreted. For example, "d:\\my share\\myservice.exe" should be specified as "\"d:\\my share\\myservice.exe\"".
        /// <para>The path can also include arguments for an auto-start service. For example, "d:\\myshare\\myservice.exe arg1 arg2". These arguments are passed to the service entry point (typically the main function).</para>
        /// <para>If you specify a path on another computer, the share must be accessible by the computer account of the local computer because this is the security context used in the remote call. However, this requirement allows any potential vulnerabilities in the remote computer to affect the local computer. Therefore, it is best to use a local file.</para>
        /// </param>
        /// <param name="Name">The name of the service to install. The maximum string length is 256 characters. The service control manager database preserves the case of the characters, but service name comparisons are always case insensitive. Forward-slash (/) and backslash (\) are not valid service name characters.</param>
        /// <param name="DisplayName">The display name to be used by user interface programs to identify the service. This string has a maximum length of 256 characters. The name is case-preserved in the service control manager. Display name comparisons are always case-insensitive.</param>
        /// <param name="Description">The description to be used by user interface programs</param>
        /// <param name="ServType">The service type.</param>
        /// <param name="ServAccessType"></param>
        /// <param name="ServStartType"></param>
        /// <param name="ServErrorControl"></param>
        /// <returns></returns>
        internal static bool Install(string ServicePath, string Name, string DisplayName, string Description, ServicesAPI.ServiceType ServType, ServiceInstaller.ServiceAccessType ServAccessType, ServiceInstaller.ServiceStartType ServStartType, ServiceInstaller.ServiceErrorControl ServErrorControl)
        {
            if (Name.Length > 256)
            {
                throw new ServiceInstallException("The maximum length for a service name is 256 characters.");
            }
            if (Name.IndexOf(@"\") >= 0 || Name.IndexOf(@"/") >= 0)
            {
                throw new ServiceInstallException(@"Service names cannot contain \ or / characters.");
            }
            if (DisplayName.Length > 256)
            {
                throw new ServiceInstallException("The maximum length for a display name is 256 characters.");
            }

            //The spec says that if a service's path has a space in it, then we must quote it...
            //if (ServicePath.IndexOf(" ") >= 0)
            //  ServicePath = "\"" + ServicePath + "\"";
            //ServicePath = ServicePath.Replace(@"\", @"\\");

            IntPtr sc_handle = IntPtr.Zero;
            IntPtr sv_handle = IntPtr.Zero;

            try
            {
                sc_handle = OpenSCManagerA(null, null, ServiceControlManagerAccessType.SC_MANAGER_CREATE_SERVICE);
                if (sc_handle == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                sv_handle = CreateService(sc_handle, Name, DisplayName, ServAccessType, ServType, ServStartType,
                                          ServErrorControl, ServicePath, null, IntPtr.Zero, null, null, null);
                //IntPtr sv_handle = ServicesAPI.CreateService(sc_handle, Name, DisplayName, 0xF0000 | 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080 | 0x0100, 0x00000010, 0x00000002, 0x00000001, ServicePath, null, 0, null, null, null);
                if (sv_handle == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }


                //Sets a service's description by adding a registry entry for it.
                if (!string.IsNullOrEmpty(Description))
                {
                    try
                    {
                        using (
                            Microsoft.Win32.RegistryKey serviceKey =
                                Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                                    @"System\CurrentControlSet\Services\" + Name, true))
                        {
                            if (serviceKey != null)
                            {
                                serviceKey.SetValue("Description", Description);
                            }
                            else
                            {
                                DebugLogger.WriteLine("Unable to find service in registry, can't set Description");
                            }
                        }
                    }
                    catch
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (sv_handle != IntPtr.Zero)
                {
                    CloseServiceHandle(sv_handle);
                }
                if (sc_handle != IntPtr.Zero)
                {
                    CloseServiceHandle(sc_handle);
                }
            }
        }