public static extern IntPtr CreateService(IntPtr scHandle, //[MarshalAs(UnmanagedType.LPTStr)] string lpSvcName, //[MarshalAs(UnmanagedType.LPTStr)] string lpDisplayName, Attributes.ServiceAccessType dwDesiredAccess, Attributes.ServiceType dwServiceType, Attributes.ServiceStartType dwStartType, Attributes.ServiceErrorControl dwErrorControl, //[MarshalAs(UnmanagedType.LPTStr)] string lpPathName, //[MarshalAs(UnmanagedType.LPTStr)] string lpLoadOrderGroup, int lpdwTagId, //[MarshalAs(UnmanagedType.LPTStr)] string lpDependencies, //[MarshalAs(UnmanagedType.LPTStr)] string lpServiceStartName, //[MarshalAs(UnmanagedType.LPTStr)] string lpPassword);
public static bool Install(string ServicePath, string Name, string DisplayName, string Description, Attributes.ServiceType ServType, Attributes.ServiceAccessType ServAccessType, Attributes.ServiceStartType ServStartType, Attributes.ServiceErrorControl ServErrorControl, string dependencies) { if (Name.Length > 256) { throw new Exception("The maximum length for a service name is 256 characters."); } if (Name.IndexOf(@"\") >= 0 || Name.IndexOf(@"/") >= 0) { throw new Exception(@"Service names cannot contain \ or / characters."); } if (DisplayName.Length > 256) { throw new Exception("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(@"\", @"\\"); try { IntPtr sc_handle = ServicesAPI.OpenSCManagerA(null, null, ServicesAPI.ServiceControlManagerType.SC_MANAGER_CREATE_SERVICE); if (sc_handle == IntPtr.Zero) { return(false); } IntPtr sv_handle = ServicesAPI.CreateService(sc_handle, Name, DisplayName, ServAccessType, ServType, ServStartType, ServErrorControl, ServicePath, null, 0, dependencies, 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) { ServicesAPI.CloseServiceHandle(sc_handle); return(false); } ServicesAPI.CloseServiceHandle(sv_handle); ServicesAPI.CloseServiceHandle(sc_handle); //Sets a service's description by adding a registry entry for it. if (Description != null && Description != "") { try { using (Microsoft.Win32.RegistryKey serviceKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Services\" + Name, true)) { serviceKey.SetValue("Description", Description); } } catch { return(false); } } return(true); } catch { return(false); } }