public static extern IntPtr CreateService( SCMHandle hSCManager, string lpServiceName, string lpDisplayName, ServiceAccessRights dwDesiredAccess, SC_SERVICE_TYPE dwServiceType, ServiceStartMode dwStartType, SC_SERVICE_ERROR_CONTROL dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, string lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
private void WithServiceHandle(ServiceAccessRights access, Action <IntPtr> action) { using (SCMHandle hScm = new SCMHandle(SCM_ACCESS.STANDARD_RIGHTS_REQUIRED)) { IntPtr hSvc = Win32.OpenService(hScm, _svcName, access); if (hSvc == IntPtr.Zero) { throw new Win32Exception(); } try { action(hSvc); } finally { Win32.CloseServiceHandle(hSvc); } } }
/// <summary> Creates the specified service and returns a SvcControlManager for the service created </summary> public static SvcControlManager Create(string serviceName, string displayName, bool interactive, ServiceStartMode startupType, string exePath, string[] arguments, string accountName, string password) { exePath = ArgumentList.EscapeArguments(new string[] { Check.NotEmpty(exePath) }); if (arguments != null && arguments.Length > 0) { exePath = String.Format("{0} {1}", exePath, ArgumentList.EscapeArguments(arguments)); } using (SCMHandle hScm = new SCMHandle(SCM_ACCESS.SC_MANAGER_CREATE_SERVICE)) { IntPtr hSvc = Win32.CreateService( hScm, serviceName, displayName ?? serviceName, ServiceAccessRights.SERVICE_ALL_ACCESS, SC_SERVICE_TYPE.SERVICE_WIN32_OWN_PROCESS | (interactive ? SC_SERVICE_TYPE.SERVICE_INTERACTIVE_PROCESS : 0), startupType, SC_SERVICE_ERROR_CONTROL.SERVICE_ERROR_NORMAL, exePath, null, null, null, accountName, password); if (hSvc == IntPtr.Zero) { throw new Win32Exception(); } Win32.CloseServiceHandle(hSvc); } return(new SvcControlManager(serviceName)); }
public static extern IntPtr OpenService(SCMHandle hSCManager, string lpServiceName, ServiceAccessRights dwDesiredAccess);