public void ServiceMainCallback(int argCount, IntPtr argPointer) { LPHANDLER_FUNCTION_EX handler = new LPHANDLER_FUNCTION_EX(Win32HandlerFn); // handler needs to last until the service stops service_handle = RegisterServiceCtrlHandlerEx(ServiceName ?? "", handler, IntPtr.Zero); if (service_handle != IntPtr.Zero) { SetStatus(ServiceControllerStatus.StartPending); stop_event = new ManualResetEvent(false); string[] args = new string[argCount]; for (int i = 0; i < argCount; i++) { IntPtr arg = Marshal.ReadIntPtr(argPointer, IntPtr.Size * i); args[i] = Marshal.PtrToStringUni(arg); } OnStart(args); SetStatus(ServiceControllerStatus.Running); stop_event.WaitOne(); SetStatus(ServiceControllerStatus.StopPending); OnStop(); SetStatus(ServiceControllerStatus.Stopped); } }
private static extern IntPtr RegisterServiceCtrlHandlerEx( string lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, IntPtr lpContext);
public void ServiceMainCallback (int argCount, IntPtr argPointer) { LPHANDLER_FUNCTION_EX handler = new LPHANDLER_FUNCTION_EX (Win32HandlerFn); // handler needs to last until the service stops service_handle = RegisterServiceCtrlHandlerEx (ServiceName ?? "", handler, IntPtr.Zero); if (service_handle != IntPtr.Zero) { SetStatus (ServiceControllerStatus.StartPending); stop_event = new ManualResetEvent (false); string[] args = new string[argCount]; for (int i=0; i<argCount; i++) { IntPtr arg = Marshal.ReadIntPtr (argPointer, IntPtr.Size * i); args[i] = Marshal.PtrToStringUni (arg); } OnStart (args); SetStatus (ServiceControllerStatus.Running); stop_event.WaitOne (); SetStatus (ServiceControllerStatus.StopPending); OnStop (); SetStatus (ServiceControllerStatus.Stopped); } }
public static unsafe extern IntPtr RegisterServiceCtrlHandlerEx( string lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, void* lpContext);
private static extern IntPtr RegisterServiceCtrlHandlerEx ( string lpServiceName, LPHANDLER_FUNCTION_EX lpHandlerProc, IntPtr lpContext);