Exemplo n.º 1
0
        /// <summary>
        /// Stop the service
        /// </summary>
        public void Stop()
        {
            var serviceStatus = new Advapi32.ServiceStatus();

            if (!Advapi32.ControlService(serviceHandle: this, Advapi32.ServiceControl.Stop, ref serviceStatus))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the command. Needs SERVICE_USER_DEFINED_CONTROL right.
        /// </summary>
        /// <param name="command">The command.</param>
        public void ExecuteCommand(int command)
        {
            if (command < 128 || command > 255)
            {
                throw new ArgumentException("Only a range of 128-255 is allowed for custom commands.");
            }

            var serviceStatus = new Advapi32.ServiceStatus();

            Advapi32.ControlService(this, command, ref serviceStatus);
        }
        private Advapi32.SERVICE_STATUS_PROCESS ControlService(SafeHandle serviceHandle, uint control)
        {
            IntPtr lpServiceStatus = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Advapi32.SERVICE_STATUS_PROCESS)));

            if (!Advapi32.ControlService(
                    serviceHandle.DangerousGetHandle(),
                    control,
                    lpServiceStatus
                    ))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return((Advapi32.SERVICE_STATUS_PROCESS)Marshal.PtrToStructure(lpServiceStatus, typeof(Advapi32.SERVICE_STATUS_PROCESS)));
        }