/// <summary> /// This is called whenever a service control event happens such as pausing, stopping, etc... /// </summary> /// <param name="Opcode"></param> /// <param name="eventType"></param> /// <param name="eventData"></param> /// <param name="context"></param> /// <returns></returns> private int baseServiceControlHandler(ServicesAPI.ServiceControlType Opcode, int eventType, IntPtr eventData, IntPtr context) { System.Windows.Forms.Message m = new System.Windows.Forms.Message { Msg = DeviceNotification.WM_DEVICECHANGE, WParam = (IntPtr)eventType, LParam = eventData }; switch (Opcode) { case ServicesAPI.ServiceControlType.SERVICE_CONTROL_PAUSE: this.ServiceState = ServiceState.Paused; this._servStatus.dwCurrentState = ServicesAPI.ServiceCurrentStateType.SERVICE_PAUSED; try { this.ServiceSendCommand(Command.Pause); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to pause the service:" + e); } ServicesAPI.SetServiceStatus(this._servStatusHandle, ref this._servStatus); break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_CONTINUE: this.ServiceState = ServiceState.Running; this._servStatus.dwCurrentState = ServicesAPI.ServiceCurrentStateType.SERVICE_RUNNING; ServicesAPI.SetServiceStatus(this._servStatusHandle, ref this._servStatus); try { this.ServiceSendCommand(Command.Continue); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to continue the service:" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_STOP: this.ServiceState = ServiceState.Stopped; this._servStatus.dwWin32ExitCode = 0; this._servStatus.dwCurrentState = ServicesAPI.ServiceCurrentStateType.SERVICE_STOPPED; this._servStatus.dwCheckPoint = 0; this._servStatus.dwWaitHint = 0; ServicesAPI.SetServiceStatus(this._servStatusHandle, ref this._servStatus); try { this.ServiceSendCommand(Command.Stop); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to stop the service:" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_SHUTDOWN: this.ServiceState = ServiceState.ShuttingDown; this._servStatus.dwCurrentState = ServicesAPI.ServiceCurrentStateType.SERVICE_STOPPED; ServicesAPI.SetServiceStatus(this._servStatusHandle, ref this._servStatus); try { this.ServiceSendCommand(Command.Shutdown); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to shut down the service:" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_INTERROGATE: //this.ServiceState = ServiceState.Interrogating; //this.servStatus.dwCurrentState = ServicesAPI.ServiceCurrentStateType. .SERVICE_INTERROGATE; ServicesAPI.SetServiceStatus(this._servStatusHandle, ref this._servStatus); try { this.ServiceSendCommand(Command.Interrogate); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to interrogate the service:" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_DEVICEEVENT: try { //TODO: Implement proper device event parsing!! var devevt = Notifications.Notifications.Transform(ref m); this.ServiceSendDeviceEvent(devevt.Device); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to update Device Event" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_HARDWAREPROFILECHANGE: try { this.ServiceSendHardwareProfileChange((ServicesAPI.SERVICE_CONTROL_HARDWAREPROFILECHANGE_Control)eventType); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to change hardware profile" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_NETBINDADD: try { this.ServiceSendNetBind(ServicesAPI.NetBindControl.NETBINDADD); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to netbind add" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_NETBINDDISABLE: try { this.ServiceSendNetBind(ServicesAPI.NetBindControl.NETBINDDISABLE); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to netbind disable" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_NETBINDENABLE: try { this.ServiceSendNetBind(ServicesAPI.NetBindControl.NETBINDENABLE); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to netbind enable" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_NETBINDREMOVE: try { this.ServiceSendNetBind(ServicesAPI.NetBindControl.NETBINDREMOVE); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to netbind remove" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_PARAMCHANGE: try { //TODO could be a mistake this.ServiceSendCustomCommand((int)eventData); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to send custom command" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_POWEREVENT: try { var powevt = Notifications.Notifications.Transform(ref m); this.ServiceSendPowerEvent(powevt.Power); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to process power event" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_PRESHUTDOWN: try { this.ServiceSendCommand(Command.PreShutdown); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to execute Pre Shutdown event" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_SESSIONCHANGE: try { var wtsnotification = (ServicesAPI.WTSSESSION_NOTIFICATION)Marshal.PtrToStructure(eventData, typeof(ServicesAPI.WTSSESSION_NOTIFICATION)); var sne = Notifications.SessionNotification.Transform(wtsnotification.dwSessionId); this.ServiceSendSessionChange((ServicesAPI.SERVICE_CONTROL_SESSIONCHANGE_Control)eventType, sne); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to process session change event" + e); } break; case ServicesAPI.ServiceControlType.SERVICE_CONTROL_TIMECHANGE: try { var sti = (ServicesAPI.SERVICE_TIMECHANGE_INFO)Marshal.PtrToStructure(eventData, typeof(ServicesAPI.SERVICE_TIMECHANGE_INFO)); this.ServiceSendTimeChange(sti.liOldTimeToDateTime(), sti.liNewTimeToDateTime()); } catch (Exception e) { this.Logger.WriteEntry(System.Diagnostics.EventLogEntryType.Error, "An exception occurred while trying to process time change event" + e); } break; //case ServicesAPI.ServiceControlType.SERVICE_CONTROL_TRIGGEREVENT: // break; } return(0); //NO_ERROR }
internal void ChangeServiceState(ServicesAPI.ServiceControlType OpCode) { this.baseServiceControlHandler(OpCode, 0, IntPtr.Zero, IntPtr.Zero); }