public void CreateService() { Console.WriteLine($"created service serviceName:{serviceName}, displayName:{displayName}, desiredAccess: 0x{(uint)desiredAccess:X8}," + $" serviceType: {serviceType},startType: {startType}, errorControl: {errorControl}," + $" binaryPathName: {binaryPathName}, serviceStartName: {serviceStartName}"); using var scManager = AdvApi32.OpenSCManager(null, null, desiredAccess); using var serviceHandle = AdvApi32.CreateService(scManager, serviceName, displayName, desiredAccess, serviceType, startType, errorControl, binaryPathName, null, 0, null, serviceStartName, password); }
private static void Install() { string servicePath = AppDomain.CurrentDomain.BaseDirectory + typeof(Service).Assembly.ManifestModule.Name; AdvApi32.CreateService(servicePath, serviceName, serviceDisplayName, serviceDescription, null, null); }
public bool Install(string path, out string errorMessage) { IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_ALL_ACCESS); if (manager == IntPtr.Zero) { errorMessage = "OpenSCManager returned zero."; return(false); } IntPtr service = AdvApi32.CreateService(manager, _id, _id, AdvApi32.SERVICE_ACCESS_MASK.SERVICE_ALL_ACCESS, AdvApi32.SERVICE_TYPE.SERVICE_KERNEL_DRIVER, AdvApi32.SERVICE_START.SERVICE_DEMAND_START, AdvApi32.SERVICE_ERROR.SERVICE_ERROR_NORMAL, path, null, null, null, null, null); if (service == IntPtr.Zero) { if (Marshal.GetHRForLastWin32Error() == Kernel32.ERROR_SERVICE_EXISTS) { errorMessage = "Service already exists"; return(false); } errorMessage = "CreateService returned the error: " + Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()).Message; AdvApi32.CloseServiceHandle(manager); return(false); } if (!AdvApi32.StartService(service, 0, null)) { if (Marshal.GetHRForLastWin32Error() != Kernel32.ERROR_SERVICE_ALREADY_RUNNING) { errorMessage = "StartService returned the error: " + Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()).Message; AdvApi32.CloseServiceHandle(service); AdvApi32.CloseServiceHandle(manager); return(false); } } AdvApi32.CloseServiceHandle(service); AdvApi32.CloseServiceHandle(manager); #if NETFRAMEWORK try { // restrict the driver access to system (SY) and builtin admins (BA) // TODO: replace with a call to IoCreateDeviceSecure in the driver FileSecurity fileSecurity = File.GetAccessControl(@"\\.\" + _id); fileSecurity.SetSecurityDescriptorSddlForm("O:BAG:SYD:(A;;FA;;;SY)(A;;FA;;;BA)"); File.SetAccessControl(@"\\.\" + _id, fileSecurity); } catch { } #endif errorMessage = null; return(true); }
public static void Install(string path, string username, string password) { try { logger.Info($"Opening service control manager"); var serviceManager = AdvApi32.OpenSCManager(null, null, AdvApi32.ScManagerAccessTypes.SC_MANAGER_ALL_ACCESS); string[] dependencies = new[] { "http" }; logger.Info($"Opened service control manager"); AdvApi32.SafeSC_HANDLE serviceHandle; if (!path.StartsWith("\"")) { path = "\"" + path; } if (!path.EndsWith("\"")) { path = path + "\""; } try { logger.Info($"Checking for existing {Constants.ServiceName} service"); serviceHandle = AdvApi32.OpenService(serviceManager, Constants.ServiceName, AdvApi32.ServiceAccessTypes.SERVICE_ALL_ACCESS); if (serviceHandle.IsNull) { throw new Win32Exception(Marshal.GetLastWin32Error()); } logger.Info($"Found existing {Constants.ServiceName} service"); } catch (Win32Exception ex) { if (ex.NativeErrorCode == 1060) { logger.Info($"Existing {Constants.ServiceName} service not found"); logger.Info($"Attempting to create server {Constants.ServiceName} for user {username} at {path}"); serviceHandle = AdvApi32.CreateService(serviceManager, Constants.ServiceName, Constants.ServiceDisplayName, (uint)AdvApi32.ServiceAccessTypes.SERVICE_ALL_ACCESS, AdvApi32.ServiceTypes.SERVICE_WIN32_OWN_PROCESS, AdvApi32.ServiceStartType.SERVICE_DEMAND_START, AdvApi32.ServiceErrorControlType.SERVICE_ERROR_NORMAL, path, null, IntPtr.Zero, dependencies, username, password); if (serviceHandle.IsNull) { throw new Win32Exception(Marshal.GetLastWin32Error()); } logger.Info($"Created {Constants.ServiceName} service"); } else { throw; } } var description = new AdvApi32.SERVICE_DESCRIPTION() { lpDescription = Constants.ServiceDescription }; logger.Info($"Updating service description"); if (!AdvApi32.ChangeServiceConfig2(serviceHandle, AdvApi32.ServiceConfigOption.SERVICE_CONFIG_DESCRIPTION, description)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } var sidConfig = new AdvApi32.SERVICE_SID_INFO() { dwServiceSidType = 0x1 }; logger.Info($"Updating service SID configuration"); if (!AdvApi32.ChangeServiceConfig2(serviceHandle, AdvApi32.ServiceConfigOption.SERVICE_CONFIG_SERVICE_SID_INFO, sidConfig)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } logger.Info($"Updated existing {Constants.ServiceName} service parameters"); TryGrantLogonAsAService(username); } catch (Exception ex) { logger.Error(ex, "Unable to install service"); throw; } }