Esempio n. 1
0
 public void Dispose()
 {
     if (!NativeServiceFunctions.CloseServiceHandle(Handle))
     {
         Log.Warn("Warning, unable to close ServiceControlManager.Handle");
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Returns the status of all currently existing services on this SCM
        /// </summary>
        /// <param name="ServiceType">Type of serviec </param>
        /// <returns>Enumerable list of service statuses</returns>
        public IEnumerable <ENUM_SERVICE_STATUS_PROCESS> Refresh(SC_SERVICE_TYPE ServiceType)
        {
            // Quote from MSDN: Windows Server 2003 and Windows XP:
            // The maximum size of this array is 64K bytes. This limit
            // was increased as of Windows Server 2003 SP1 and Windows XP SP2.

            const int BytesAllocated = 63 * 1024;

            IntPtr lpServices       = Marshal.AllocHGlobal((int)BytesAllocated);
            int    cbBytesNeeded    = 0;
            int    ServicesReturned = 0;
            int    ResumeHandle     = 0;
            bool   repeat           = true;

            while (repeat)
            {
                if (NativeServiceFunctions.EnumServicesStatusEx(Handle,
                                                                SC_ENUM_TYPE.SC_ENUM_PROCESS_INFO,
                                                                ServiceType,
                                                                SC_QUERY_SERVICE_STATUS.SERVICE_STATE_ALL,
                                                                lpServices,
                                                                BytesAllocated,
                                                                ref cbBytesNeeded,
                                                                ref ServicesReturned,
                                                                ref ResumeHandle,
                                                                null))
                {
                    Log.InfoFormat("Got {0} services in last chunk", ServicesReturned);
                    repeat = false;
                }
                else
                {
                    int LastError = Marshal.GetLastWin32Error();
                    if (LastError == NativeServiceFunctions.ERROR_MORE_DATA)
                    {
                        Log.InfoFormat("Got {0} services in this chunk", ServicesReturned);
                    }
                    else
                    {
                        NativeHelpers.ReportFailure("EnumServicesStatusEx()");
                        break;
                    }
                }

                int iPtr = lpServices.ToInt32();
                for (int i = 0; i < ServicesReturned; i++)
                {
                    ENUM_SERVICE_STATUS_PROCESS essp = (ENUM_SERVICE_STATUS_PROCESS)
                                                       Marshal.PtrToStructure(
                        new IntPtr(iPtr),
                        typeof(ENUM_SERVICE_STATUS_PROCESS));

                    yield return(essp);

                    iPtr += Marshal.SizeOf(essp);
                }
            }
        }
Esempio n. 3
0
 public void Dispose()
 {
     if (IsValid)
     {
         if (!NativeServiceFunctions.CloseServiceHandle(Handle))
         {
             Log.WarnFormat("Warning, unable to close NativeService.Handle");
         }
         Handle = new IntPtr(0);
     }
 }
Esempio n. 4
0
        public NativeSCManager(string machineName)
        {
            if (string.IsNullOrEmpty(machineName))
            {
                machineName = Environment.MachineName;
            }

            Handle = NativeServiceFunctions.OpenSCManager(
                machineName,
                NativeServiceFunctions.SERVICES_ACTIVE_DATABASE,
                (uint)(ACCESS_MASK.STANDARD_RIGHTS_READ | ACCESS_MASK.GENERIC_READ));
        }
Esempio n. 5
0
        /// <summary>
        /// The constructor opens access to the service
        /// </summary>
        /// <param name="scm">The SCM instance that contains the service</param>
        /// <param name="ServiceName">Name of the service</param>
        /// <param name="am">Access rights required.</param>
        public NativeService(NativeSCManager scm, string serviceName, ACCESS_MASK am = ACCESS_MASK.STANDARD_RIGHTS_READ | ACCESS_MASK.GENERIC_READ)
        {
            ServiceName = serviceName;

            Handle = NativeServiceFunctions.OpenService(
                scm.Handle,
                serviceName,
                (uint)am);

            IsValid = (Handle.ToInt32() != 0);
            if (!IsValid)
            {
                NativeHelpers.ReportFailure("OpenService({0}, {1})", serviceName, am);
            }
        }
Esempio n. 6
0
        public bool Start()
        {
            if (!Service.IsValid)
            {
                Log.WarnFormat("Warning, don't attempt to call StartService on {0}", Service);
                return(false);
            }
            Log.InfoFormat("StartService({0})", Service);
            if (NativeServiceFunctions.StartService(Service.Handle, 0, Memory))
            {
                return(true);
            }

            return(NativeHelpers.ReportFailure("StartService({0})", Service));
        }
Esempio n. 7
0
 public bool Uninstall(NativeSCManager scm)
 {
     try
     {
         using (NativeService ns = new NativeService(scm,
                                                     InternalID,
                                                     ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS | ACCESS_MASK.DELETE))
         {
             return(NativeServiceFunctions.DeleteService(ns.Handle));
         }
     }
     catch (Exception e)
     {
         Log.Error("DeleteService", e);
         return(false);
     }
 }
Esempio n. 8
0
 public bool Control(SC_CONTROL_CODE code)
 {
     if (!Service.IsValid)
     {
         Log.WarnFormat("Warning, don't attempt to call Control({1}) on {0}", Service, code);
         return(false);
     }
     Log.InfoFormat("ControlService({0}, {1})", Service, code);
     if (NativeServiceFunctions.ControlService(Service.Handle, code, Memory))
     {
         Status = (SERVICE_STATUS_PROCESS)Marshal.PtrToStructure(
             Memory,
             typeof(SERVICE_STATUS_PROCESS));
         Log.InfoFormat("Currentstatus = {0}", Status.CurrentState);
         return(true);
     }
     return(NativeHelpers.ReportFailure("ControlService({0}, {1})", Service, code));
 }
Esempio n. 9
0
        public bool ApplyStartupChanges(NativeSCManager scm, SC_START_TYPE startupType)
        {
            bool success = true;

            if (startupType != StartType)
            {
                Log.InfoFormat("{0}: Change SC_START_TYPE from {1} to {2}", InternalID, StartType, startupType);
                using (NativeService ns = new NativeService(scm,
                                                            InternalID,
                                                            ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS))
                {
                    success = NativeServiceFunctions.ChangeServiceConfig(ns.Handle,
                                                                         StartType: startupType);
                    if (success)
                    {
                        StartType = startupType;
                    }
                }
            }
            return(success);
        }
Esempio n. 10
0
        public void ApplyChanges(
            NativeSCManager scm,
            SC_START_TYPE startupType,
            string displayName,
            string binaryPathName,
            string description)
        {
            using (NativeService ns = new NativeService(scm,
                                                        InternalID,
                                                        ACCESS_MASK.SERVICE_CHANGE_CONFIG | ACCESS_MASK.SERVICE_QUERY_STATUS))
            {
                bool success = NativeServiceFunctions.ChangeServiceConfig(ns.Handle,
                                                                          StartType: startupType,
                                                                          DisplayName: displayName,
                                                                          BinaryPathName: binaryPathName);
                if (success)
                {
                    StartType = startupType;
                    if (displayName != null)
                    {
                        SetStringProperty("DisplayName", displayName);
                    }
                    if (binaryPathName != null)
                    {
                        SetStringProperty("BinaryPathName", binaryPathName);
                        NotifyPropertyChanged("InstallLocation");
                    }

                    if ((description != null) && !description.Equals(Description))
                    {
                        ns.Description = description;
                        SetStringProperty("Description", description);
                    }
                }
            }
        }
Esempio n. 11
0
        public static bool ChangeServiceDescription(IntPtr Handle, string description)
        {
            try
            {
                SERVICE_DESCRIPTION sd = new SERVICE_DESCRIPTION();
                sd.Description = description;

                const int cbBufSize = 8 * 1024;

                IntPtr lpMemory = Marshal.AllocHGlobal((int)cbBufSize);
                Marshal.StructureToPtr(sd, lpMemory, false);

                bool result = NativeServiceFunctions.ChangeServiceConfig2(Handle, SC_SERVICE_CONFIG.SERVICE_CONFIG_DESCRIPTION, lpMemory);

                Marshal.FreeHGlobal(lpMemory);

                return(result);
            }
            catch (Exception e)
            {
                Log.Error("ChangeServiceDescription() failed", e);
                return(false);
            }
        }
Esempio n. 12
0
        public bool Refresh()
        {
            if (!Service.IsValid)
            {
                Log.WarnFormat("Warning, don't attempt to call QueryServiceStatusEx() on {0}", Service);
                return(false);
            }
            int bytesNeeded;

            if (NativeServiceFunctions.QueryServiceStatusEx(
                    Service.Handle,
                    SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO,
                    Memory,
                    Marshal.SizeOf(Status),
                    out bytesNeeded))
            {
                Status = (SERVICE_STATUS_PROCESS)Marshal.PtrToStructure(
                    Memory,
                    typeof(SERVICE_STATUS_PROCESS));
                Log.InfoFormat("CurrentStatus as returned by QSSE = {0}", Status.CurrentState);
                return(true);
            }
            return(NativeHelpers.ReportFailure("QueryServiceStatusEx({0})", Service));
        }