Пример #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 void StartService(string Name)
        {
            IntPtr intPtr = ServiceInstaller.OpenSCManager(ServiceManagerRights.Connect);

            try
            {
                IntPtr intPtr1 = ServiceInstaller.OpenService(intPtr, Name, ServiceRights.QueryStatus | ServiceRights.Start);
                if (intPtr1 == IntPtr.Zero)
                {
                    throw new ApplicationException("Could not open service.");
                }
                try
                {
                    ServiceInstaller.StartService(intPtr1);
                }
                finally
                {
                    ServiceInstaller.CloseServiceHandle(intPtr1);
                }
            }
            finally
            {
                ServiceInstaller.CloseServiceHandle(intPtr);
            }
        }
Пример #3
0
 private static void StartService(IntPtr hService)
 {
     ServiceInstaller.SERVICE_STATUS sERVICESTATU = new ServiceInstaller.SERVICE_STATUS();
     ServiceInstaller.StartService(hService, 0, 0);
     ServiceInstaller.WaitForServiceStatus(hService, ServiceState.Starting, ServiceState.Run);
 }