Exemple #1
0
        private void LaunchService(IntPtr hSCManager)
        {
            var hService = NativeServiceWrapper.OpenServiceW(hSCManager, serviceName, (uint)ServicePermissions.SERVICE_START);

            if (hService == IntPtr.Zero)
            {
                var error    = Marshal.GetLastWin32Error();
                var errorMsg = "Could not open the existing service. Error: " + error;
                logger.Error("Launch service failed: " + errorMsg);
                throw new Win32Exception(error, errorMsg);
            }

            try
            {
                LaunchService(hSCManager, hService);
            }
            finally
            {
                NativeServiceWrapper.CloseServiceHandle(hService);
            }
        }
Exemple #2
0
        public void CreateService(bool start = true)
        {
            var hSCManager = IntPtr.Zero;
            var hService   = IntPtr.Zero;

            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var errorMsg = "Services can only be installed on Windows platforms.";
                logger.Error("Create service failed: " + errorMsg);
                throw new PlatformNotSupportedException();
            }

            try
            {
                hSCManager = NativeServiceWrapper.OpenSCManagerW(null, null, (uint)SCMPermissions.SC_MANAGER_ALL_ACCESS);
                if (hSCManager == IntPtr.Zero)
                {
                    var error    = Marshal.GetLastWin32Error();
                    var errorMsg = "Error when opening SCM: " + error;

                    if (error == 0x5)
                    {
                        errorMsg = "Error when opening SCM: Access Denied. Please try again with administrator privileges.";
                    }

                    logger.Error("Create service failed: " + errorMsg);
                    throw new Win32Exception(error, errorMsg);
                }

                var serviceCmd = new StringBuilder();
                serviceCmd.AppendFormat("\"{0}\"", binaryPath);
                if (serviceArgs != null)
                {
                    foreach (var arg in serviceArgs)
                    {
                        serviceCmd.Append(" ");
                        serviceCmd.Append(arg);
                    }
                }

                hService = NativeServiceWrapper.CreateServiceW(hSCManager, serviceName, displayName, (uint)ServicePermissions.SERVICE_ALL_ACCESS, serviceType, startType, errorSeverity, serviceCmd.ToString(), null, IntPtr.Zero, null, username, password);
                if (hService == IntPtr.Zero)
                {
                    var error = Marshal.GetLastWin32Error();
                    if (error != (int)ServiceErrorCodes.ERROR_SERVICE_EXISTS)
                    {
                        var errorMsg = "Create service failed with error: " + error;
                        logger.Error("Create service failed: " + errorMsg);
                        throw new Win32Exception(error, errorMsg);
                    }

                    hService = NativeServiceWrapper.OpenServiceW(hSCManager, serviceName, (uint)ServicePermissions.SERVICE_START);
                    if (hService == IntPtr.Zero)
                    {
                        error = Marshal.GetLastWin32Error();
                        var errorMsg = "Could not open the existing service. Error: " + error;
                        logger.Error("Create service failed: " + errorMsg);
                        throw new Win32Exception(error, errorMsg);
                    }
                }

                if (start)
                {
                    LaunchService(hSCManager, hService);
                }
            }
            finally
            {
                if (hService != IntPtr.Zero)
                {
                    NativeServiceWrapper.CloseServiceHandle(hService);
                }

                if (hSCManager != IntPtr.Zero)
                {
                    NativeServiceWrapper.CloseServiceHandle(hSCManager);
                }
            }
        }
Exemple #3
0
        public void RemoveService()
        {
            var hSCManager = IntPtr.Zero;
            var hService   = IntPtr.Zero;

            try
            {
                hSCManager = NativeServiceWrapper.OpenSCManagerW(null, null, (uint)SCMPermissions.SC_MANAGER_ALL_ACCESS);
                if (hSCManager == IntPtr.Zero)
                {
                    var error    = Marshal.GetLastWin32Error();
                    var errorMsg = "Error when opening SCM: " + error;

                    if (error == 0x5)
                    {
                        errorMsg = "Error when opening SCM: Access Denied. Please try again with administrator privileges.";
                    }

                    logger.Error("Remove service failed: " + errorMsg);
                    throw new Win32Exception(error, errorMsg);
                }

                hService = NativeServiceWrapper.OpenServiceW(hSCManager, serviceName, (uint)ServicePermissions.SERVICE_DELETE | (uint)ServicePermissions.SERVICE_STOP);
                if (hService == IntPtr.Zero)
                {
                    var error    = Marshal.GetLastWin32Error();
                    var errorMsg = "Could not open the service for removal. Error: " + error;
                    logger.Error("Remove service failed: " + errorMsg);
                    throw new Win32Exception(error, errorMsg);
                }

                ServiceStatus status = new ServiceStatus();
                if (!NativeServiceWrapper.ControlService(hService, ServiceControls.SERVICE_CONTROL_STOP, ref status))
                {
                    var error = Marshal.GetLastWin32Error();
                    if (error != (int)ServiceErrorCodes.ERROR_SERVICE_NOT_ACTIVE &&
                        error != (int)ServiceErrorCodes.ERROR_SERVICE_CANNOT_ACCEPT_CTRL &&
                        error != (int)ServiceErrorCodes.ERROR_SHUTDOWN_IN_PROGRESS)
                    {
                        var errorMsg = "Could not stop the service for removal. Error: " + error;
                        logger.Error("Remove service failed: " + errorMsg);
                        throw new Win32Exception(error, errorMsg);
                    }
                }

                if (!NativeServiceWrapper.DeleteService(hService))
                {
                    var error    = Marshal.GetLastWin32Error();
                    var errorMsg = "Could not delete the service. Error: " + error;
                    logger.Error("Remove service failed: " + errorMsg);
                    throw new Win32Exception(error, errorMsg);
                }
            }
            finally
            {
                if (hService != IntPtr.Zero)
                {
                    NativeServiceWrapper.CloseServiceHandle(hService);
                }

                if (hSCManager != IntPtr.Zero)
                {
                    NativeServiceWrapper.CloseServiceHandle(hSCManager);
                }
            }
        }