Пример #1
0
        public bool Delete()
        {
            IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_ALL_ACCESS);

            if (manager == IntPtr.Zero)
            {
                return(false);
            }


            IntPtr service = AdvApi32.OpenService(manager, _id, AdvApi32.SERVICE_ACCESS_MASK.SERVICE_ALL_ACCESS);

            if (service == IntPtr.Zero)
            {
                return(true);
            }


            AdvApi32.SERVICE_STATUS status = new AdvApi32.SERVICE_STATUS();
            AdvApi32.ControlService(service, AdvApi32.SERVICE_CONTROL.SERVICE_CONTROL_STOP, ref status);
            AdvApi32.DeleteService(service);
            AdvApi32.CloseServiceHandle(service);
            AdvApi32.CloseServiceHandle(manager);

            return(true);
        }
Пример #2
0
        public static bool DeleteService(string serviceName)
        // Marks the specified service for deletion from
        // the service control manager database
        {
            Trace.WriteLine(
                "Deleting service: \'" + serviceName + "\'"
                );

            IntPtr scManagerHandle = AdvApi32.OpenSCManager(
                null,
                null,
                AdvApi32.SC_MANAGER_ALL_ACCESS
                );

            if (scManagerHandle == IntPtr.Zero)
            {
                Win32Error.Set("OpenSCManager");
                Trace.WriteLine(Win32Error.GetFullErrMsg());
                return(false);
            }

            IntPtr serviceHandle = AdvApi32.OpenService(
                scManagerHandle,
                serviceName,
                AdvApi32.DELETE
                );

            if (serviceHandle == IntPtr.Zero)
            {
                Win32Error.Set("OpenService");
                Trace.WriteLine(Win32Error.GetFullErrMsg());
                AdvApi32.CloseServiceHandle(scManagerHandle);
                return(false);
            }

            bool success = AdvApi32.DeleteService(serviceHandle);

            if (success)
            {
                Trace.WriteLine("Service deleted successfully");
            }
            else
            {
                Win32Error.Set("DeleteService");
                Trace.WriteLine(Win32Error.GetFullErrMsg());
            }

            AdvApi32.CloseServiceHandle(serviceHandle);
            AdvApi32.CloseServiceHandle(scManagerHandle);

            return(success);
        }
Пример #3
0
        public static void Uninstall()
        {
            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");

                try
                {
                    logger.Info($"Checking for existing {Constants.ServiceName} service");
                    var 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");

                    if (!AdvApi32.DeleteService(serviceHandle))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    logger.Info($"Deleted existing {Constants.ServiceName} service");
                }
                catch (Win32Exception ex)
                {
                    if (ex.NativeErrorCode == 1060)
                    {
                        logger.Info($"Existing {Constants.ServiceName} service not found");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Unable to uninstall service");
            }
        }
Пример #4
0
 private static void Uninstall()
 {
     AdvApi32.DeleteService(serviceName);
 }