예제 #1
0
        public static void InstallAndStart(string ServiceName, string DisplayName, string FileName)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect | ServiceManagerRights.CreateService);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, ServiceName, ServiceRights.QueryStatus | ServiceRights.Start);
                if (intPtr1 == IntPtr.Zero)
                {
                    intPtr1 = ServiceInstaller.CreateService(intPtr, ServiceName, DisplayName, ServiceRights.QueryStatus | ServiceRights.Start, 16, ServiceBootFlag.AutoStart, ServiceError.Normal, FileName, null, IntPtr.Zero, null, null, null);
                }
                if (intPtr1 == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to install service.");
                }
                try
                {
                    ServiceInstaller.StartService(intPtr1);
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
        }
예제 #2
0
        public static ServiceState GetServiceStatus(string ServiceName)
        {
            ServiceState serviceStatus;
            IntPtr       intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, ServiceName, ServiceRights.QueryStatus);
                if (intPtr1 != IntPtr.Zero)
                {
                    try
                    {
                        serviceStatus = ServiceInstaller.GetServiceStatus(intPtr1);
                    }
                    finally
                    {
                        ServiceInstaller.CloseServiceHandle(intPtr);
                    }
                }
                else
                {
                    serviceStatus = ServiceState.NotFound;
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
            return(serviceStatus);
        }
예제 #3
0
        public static void Uninstall(string ServiceName)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, ServiceName, ServiceRights.QueryStatus | ServiceRights.Stop | ServiceRights.Delete | ServiceRights.StandardRightsRequired);
                if (intPtr1 == IntPtr.Zero)
                {
                    throw new ApplicationException("Service not installed.");
                }
                try
                {
                    ServiceInstaller.StopService(intPtr1);
                    if (ServiceInstaller.DeleteService(intPtr1) == 0)
                    {
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        throw new ApplicationException(string.Concat("Could not delete service ", lastWin32Error));
                    }
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
        }
예제 #4
0
        public static void StopService(string Name)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, Name, ServiceRights.QueryStatus | ServiceRights.Stop);
                if (intPtr1 == IntPtr.Zero)
                {
                    throw new ApplicationException("Could not open service.");
                }
                try
                {
                    ServiceInstaller.StopService(intPtr1);
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
        }
예제 #5
0
        private static IntPtr OpenSCManager(ServiceManagerRights Rights)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(null, null, Rights);

            if (intPtr == IntPtr.Zero)
            {
                throw new ApplicationException("Could not connect to service control manager.");
            }
            return(intPtr);
        }
예제 #6
0
        public static bool ServiceIsInstalled(string ServiceName)
        {
            bool   flag;
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, ServiceName, ServiceRights.QueryStatus);
                if (intPtr1 != IntPtr.Zero)
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
            return(flag);
        }