ServiceStatusHandle INativeInterop.RegisterServiceCtrlHandlerExW(
     string serviceName
     , ServiceControlHandler serviceControlHandler
     , System.IntPtr context)
 {
     return(RegisterServiceCtrlHandlerExW(serviceName, serviceControlHandler, context));
 }
示例#2
0
        /// <summary>
        /// Runs the windows service that this instance was initialized with.
        /// This method is inteded to be run from the application's main thread and will block until the service has stopped.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Win32Exception">Thrown when an exception ocurrs when communicating with windows' service system.</exception>
        /// <exception cref="PlatformNotSupportedException">Thrown when run on a non-windows platform.</exception>
        public static int RunAsService(Options opt)
        {
            ServiceOptions = opt;
            serviceMainFunctionDelegate   = MainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
            var serviceTable = new ServiceTableEntry[2]; // second one is null/null to indicate termination

            serviceTable[0].serviceName         = ServiceName;
            serviceTable[0].serviceMainFunction = Marshal.GetFunctionPointerForDelegate(serviceMainFunctionDelegate);

            try
            {
                // StartServiceCtrlDispatcherW call returns when ServiceMainFunction has exited and all services have stopped
                // at least this is what's documented even though linked c++ sample has an additional stop event
                // to let the service main function dispatched to block until the service stops.
                if (!Win32ServiceInterop.Wrapper.StartServiceCtrlDispatcherW(serviceTable))
                {
                    opt.LogError("ASTOOL running as Window Service - Error while calling StartServiceCtrlDispatcherW");
                }
            }
            catch (DllNotFoundException dllException)
            {
                opt.LogError("ASTOOL running as Window Service - this service is not available on the current platform: " + dllException.Message);
            }

            if (resultException != null)
            {
                opt.LogError("ASTOOL running as Window Service - Exception: " + resultException.Message);
            }

            return(resultCode);
        }
示例#3
0
 internal Win32ServiceHost(string serviceName, IWin32ServiceStateMachine stateMachine, INativeInterop nativeInterop)
 {
     _serviceName   = serviceName ?? throw new ArgumentNullException(nameof(serviceName));
     _stateMachine  = stateMachine ?? throw new ArgumentNullException(nameof(stateMachine));
     _nativeInterop = nativeInterop ?? throw new ArgumentNullException(nameof(nativeInterop));
     _serviceMainFunctionDelegate   = ServiceMainFunction;
     _serviceControlHandlerDelegate = HandleServiceControlCommand;
 }
        internal Win32ServiceHost([NotNull] string serviceName, [NotNull] IWin32ServiceStateMachine stateMachine, [NotNull] INativeInterop nativeInterop)
        {
            this.serviceName   = serviceName ?? throw new System.ArgumentNullException(nameof(serviceName));
            this.stateMachine  = stateMachine ?? throw new System.ArgumentNullException(nameof(stateMachine));
            this.nativeInterop = nativeInterop ?? throw new System.ArgumentNullException(nameof(nativeInterop));

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
示例#5
0
        internal Win32ServiceHost([NotNull] IWin32Service service, [NotNull] INativeInterop nativeInterop)
        {
            serviceName        = service.ServiceName;
            this.stateMachine  = (service == null) ? throw new ArgumentNullException(nameof(service)) : new SimpleServiceStateMachine(service);
            this.nativeInterop = nativeInterop ?? throw new ArgumentNullException(nameof(nativeInterop));

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
        internal Win32ServiceHost([NotNull] IPausableWin32Service service, [NotNull] INativeInterop nativeInterop)
        {
            if (service == null)
            {
                throw new System.ArgumentNullException(nameof(service));
            }

            this.nativeInterop = nativeInterop ?? throw new System.ArgumentNullException(nameof(nativeInterop));
            serviceName        = service.ServiceName;
            stateMachine       = new PausableServiceStateMachine(service);

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
        private void GivenServiceControlManagerIsExpectingService()
        {
            A.CallTo(() => nativeInterop.StartServiceCtrlDispatcherW(A <ServiceTableEntry[]> ._))
            .Invokes(new Action <ServiceTableEntry[]>(HandleNativeStartServiceCtrlDispatcherW))
            .Returns(value: true);
            A.CallTo(() => nativeInterop.RegisterServiceCtrlHandlerExW(TestServiceName, A <ServiceControlHandler> ._, A <IntPtr> ._))
            .ReturnsLazily((string serviceName, ServiceControlHandler controlHandler, IntPtr context) =>
            {
                serviceName.Should().Be(TestServiceName);
                serviceControlHandler = controlHandler;
                serviceControlContext = context;

                return(serviceStatusHandle);
            });
        }
        internal Win32ServiceHost(IWin32Service service, INativeInterop nativeInterop)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (nativeInterop == null)
            {
                throw new ArgumentNullException(nameof(nativeInterop));
            }

            serviceName        = service.ServiceName;
            stateMachine       = new SimpleServiceStateMachine(service);
            this.nativeInterop = nativeInterop;

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
        internal Win32ServiceHost(string serviceName, IWin32ServiceStateMachine stateMachine, INativeInterop nativeInterop)
        {
            if (serviceName == null)
            {
                throw new ArgumentNullException(nameof(serviceName));
            }
            if (stateMachine == null)
            {
                throw new ArgumentNullException(nameof(stateMachine));
            }
            if (nativeInterop == null)
            {
                throw new ArgumentNullException(nameof(nativeInterop));
            }

            this.serviceName   = serviceName;
            this.stateMachine  = stateMachine;
            this.nativeInterop = nativeInterop;

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
示例#10
0
 private static extern ServiceStatusHandle RegisterServiceCtrlHandlerExW(string serviceName, ServiceControlHandler serviceControlHandler, IntPtr context);
 internal static extern IntPtr RegisterServiceCtrlHandlerExW(string serviceName, ServiceControlHandler serviceControlHandler, IntPtr context);
示例#12
0
 ServiceStatusHandle Win32ServiceInterface.RegisterServiceCtrlHandlerExW(string serviceName, ServiceControlHandler serviceControlHandler, IntPtr context)
 {
     return(RegisterServiceCtrlHandlerExW(serviceName, serviceControlHandler, context));
 }