Esempio n. 1
0
 /// <summary>
 /// Stops this service. Returns true if the service state has been verified as stopped, or false otherwise.
 /// Will wait up to 5 seconds for the service to stop. Does not throw any exceptions. Note: if the service
 /// is running under the service host, in "service mode", use <see cref="StopSelf"/> instead.
 /// </summary>
 public new bool Stop()
 {
     return(ServiceUtil.StopService(ServiceName));
 }
Esempio n. 2
0
        public static void InstallService(IntPtr databaseHandle, int totalServices, string serviceName, string serviceDisplayName, string serviceDescription, ServiceStartMode serviceStartMode, IList <string> servicesDependedOn, string binaryPathAndArgs, string user, string password)
        {
            if (!ServiceUtil.ValidateServiceName(serviceName))
            {
                throw new ArgumentException(string.Format("The service name \"{0}\" is not acceptable.", serviceName), "serviceName");
            }
            if (serviceDisplayName.Length > 0xFF)
            {
                throw new ArgumentException("The service display name is too long; maximum length is 255 characters", "serviceDisplayName");
            }

            IntPtr serviceHandle = IntPtr.Zero;

            try
            {
                serviceHandle = NativeMethods.CreateService(databaseHandle, serviceName, serviceDisplayName, 0xF01FF, totalServices == 1 ? 0x10 : 0x20, (int)serviceStartMode, 1, binaryPathAndArgs, null, IntPtr.Zero, ServiceUtil.MakeDependenciesString(servicesDependedOn), user, password);
                if (serviceHandle == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }

                if (!string.IsNullOrEmpty(serviceDescription))
                {
                    NativeMethods.SERVICE_DESCRIPTION serviceDesc = new NativeMethods.SERVICE_DESCRIPTION();
                    serviceDesc.description = Marshal.StringToHGlobalUni(serviceDescription);
                    bool flag = NativeMethods.ChangeServiceConfig2(serviceHandle, 1, ref serviceDesc);
                    Marshal.FreeHGlobal(serviceDesc.description);
                    if (!flag)
                    {
                        throw new Win32Exception();
                    }
                }
            }
            finally
            {
                if (serviceHandle != IntPtr.Zero)
                {
                    SafeNativeMethods.CloseServiceHandle(serviceHandle);
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Starts this service. Returns true if the service state has been verified as running, or false otherwise.
 /// Will wait up to 5 seconds for the service to start. Does not throw any exceptions.
 /// </summary>
 public bool Start()
 {
     return(ServiceUtil.StartService(ServiceName));
 }