/// <summary>服务是否已安装</summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public override Boolean IsInstalled(String serviceName)
        {
            using var manager = new SafeServiceHandle(OpenSCManager(null, null, ServiceControllerOptions.SC_MANAGER_CONNECT));
            if (manager == null || manager.IsInvalid)
            {
                return(false);
            }

            using var service = new SafeServiceHandle(OpenService(manager, serviceName, ServiceOptions.SERVICE_QUERY_CONFIG));
            if (service == null || service.IsInvalid)
            {
                return(false);
            }

            return(true);
        }
Exemple #2
0
        /// <summary>停止服务</summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public override unsafe Boolean Stop(String serviceName)
        {
            XTrace.WriteLine("{0}.Stop {1}", GetType().Name, serviceName);

            using var manager = new SafeServiceHandle(OpenSCManager(null, null, ServiceControllerOptions.SC_MANAGER_ALL));
            if (manager.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            using var service = new SafeServiceHandle(OpenService(manager, serviceName, ServiceOptions.SERVICE_STOP));
            if (service.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            SERVICE_STATUS status = default;

            if (!ControlService(service, ControlOptions.CONTROL_STOP, &status))
                throw new Win32Exception(Marshal.GetLastWin32Error()); }
        /// <summary>安装服务</summary>
        /// <param name="serviceName">服务名</param>
        /// <param name="displayName"></param>
        /// <param name="binPath"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public override Boolean Install(String serviceName, String displayName, String binPath, String description)
        {
            XTrace.WriteLine("{0}.Install {1}, {2}, {3}, {4}", GetType().Name, serviceName, displayName, binPath, description);

            using var manager = new SafeServiceHandle(OpenSCManager(null, null, ServiceControllerOptions.SC_MANAGER_CREATE_SERVICE));
            if (manager.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            using var service = new SafeServiceHandle(CreateService(manager, serviceName, displayName, ServiceOptions.SERVICE_ALL_ACCESS, 0x10, 2, 1, binPath, null, 0, null, null, null));
            if (service.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            // 设置描述信息
            if (!description.IsNullOrEmpty())
            {
                SERVICE_DESCRIPTION sd;
                sd.Description = Marshal.StringToHGlobalUni(description);
                var lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(sd));

                try
                {
                    Marshal.StructureToPtr(sd, lpInfo, false);

                    const Int32 SERVICE_CONFIG_DESCRIPTION = 1;
                    ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, lpInfo);
                }
                finally
                {
                    Marshal.FreeHGlobal(lpInfo);
                    Marshal.FreeHGlobal(sd.Description);
                }
            }

            return(true);
        }
        /// <summary>启动服务</summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public override Boolean Start(String serviceName)
        {
            XTrace.WriteLine("{0}.Start {1}", GetType().Name, serviceName);

            using var manager = new SafeServiceHandle(OpenSCManager(null, null, ServiceControllerOptions.SC_MANAGER_CONNECT));
            if (manager.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            using var service = new SafeServiceHandle(OpenService(manager, serviceName, ServiceOptions.SERVICE_START));
            if (service.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            if (!StartService(service, 0, IntPtr.Zero))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(true);
        }
        /// <summary>服务是否已启动</summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public override unsafe Boolean IsRunning(String serviceName)
        {
            using var manager = new SafeServiceHandle(OpenSCManager(null, null, ServiceControllerOptions.SC_MANAGER_CONNECT));
            if (manager == null || manager.IsInvalid)
            {
                return(false);
            }

            using var service = new SafeServiceHandle(OpenService(manager, serviceName, ServiceOptions.SERVICE_QUERY_STATUS));
            if (service == null || service.IsInvalid)
            {
                return(false);
            }

            SERVICE_STATUS status = default;

            if (!QueryServiceStatus(service, &status))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(status.currentState == ServiceControllerStatus.Running);
        }
Exemple #6
0
 public static extern Boolean ChangeServiceConfig2(SafeServiceHandle serviceHandle, Int32 dwInfoLevel, IntPtr pInfo);
Exemple #7
0
 internal static extern Boolean StartService(SafeServiceHandle serviceHandle, Int32 argNum, IntPtr argPtrs);
Exemple #8
0
 internal unsafe static extern Boolean QueryServiceStatus(SafeServiceHandle serviceHandle, SERVICE_STATUS *pStatus);
Exemple #9
0
 public static extern Int32 DeleteService(SafeServiceHandle serviceHandle);
Exemple #10
0
 internal static extern IntPtr CreateService(SafeServiceHandle databaseHandle, String lpSvcName, String lpDisplayName,
                                             Int32 dwDesiredAccess, Int32 dwServiceType, Int32 dwStartType,
                                             Int32 dwErrorControl, String lpPathName, String lpLoadOrderGroup,
                                             Int32 lpdwTagId, String lpDependencies, String lpServiceStartName,
                                             String lpPassword);
Exemple #11
0
 internal static extern IntPtr OpenService(SafeServiceHandle databaseHandle, String serviceName, Int32 access);
Exemple #12
0
 internal unsafe static extern Boolean ControlService(SafeServiceHandle serviceHandle, Int32 control, SERVICE_STATUS *pStatus);
 internal static extern unsafe Boolean ControlService(SafeServiceHandle serviceHandle, ControlOptions control, SERVICE_STATUS *pStatus);