Пример #1
0
        internal static void SetServiceStartType(string serviceName, ServiceNativeMethods.SERVICE_STARTTYPE type)
        {
            //open service control manager
            using (SafeServiceHandle scManager = ServiceNativeMethods.OpenSCManager(null, null, ServiceNativeMethods.SC_MANAGER_ACCESS.GENERIC_READ))
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (scManager.IsInvalid)
                {
                    throw new System.ComponentModel.Win32Exception(errorCode);
                }

                // open service
                using (SafeServiceHandle service = ServiceNativeMethods.OpenService(scManager, serviceName, ServiceNativeMethods.SERVICE_ACCESS.GENERIC_WRITE))
                {
                    errorCode = Marshal.GetLastWin32Error();
                    if (service.IsInvalid)
                    {
                        throw new System.ComponentModel.Win32Exception(errorCode);
                    }

                    // change start type
                    if (!ServiceNativeMethods.ChangeServiceConfig(service, ServiceNativeMethods.SERVICE_TYPE.SERVICE_NO_CHANGE, type,
                                                                  ServiceNativeMethods.SERVICE_ERRORCONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null))
                    {
                        errorCode = Marshal.GetLastWin32Error();
                        throw new System.ComponentModel.Win32Exception(errorCode);
                    }
                }
            }
        }
        /// <summary>
        /// Initialised the required configuration for the
        /// service when the cluster is running application
        /// integrated jobs.
        /// </summary>
        public static void SetRequiredConfigurationForApplicationIntegration()
        {
            // the service must be enabled.
            ServiceNativeMethods.SERVICE_STARTTYPE startType = ServiceHelpers.GetServiceStartType(ServiceKey);

            if (startType == ServiceNativeMethods.SERVICE_STARTTYPE.SERVICE_DISABLED)
            {
                ServiceHelpers.SetServiceStartType(ServiceKey, ServiceNativeMethods.SERVICE_STARTTYPE.SERVICE_DEMAND_START);
            }

            AllowAccounts = new List <string>();
            // the admins and users groups should be granted
            // permission to create endpoints.
            AllowAccounts.Add(NetTcpPortSharingService.AdminValue);
            AllowAccounts.Add(NetTcpPortSharingService.UserValue);
            Update();
        }