public TopshelfExitCode Run() { var exitCode = TopshelfExitCode.AbnormalExit; try { exitCode = TopshelfExitCode.StartServiceFailed; _log.InfoFormat("The {0} service is being started.", _settings.ServiceName); _serviceHandle.Start(this); _log.InfoFormat("The {0} service was started.", _settings.ServiceName); Thread.Sleep(100); exitCode = TopshelfExitCode.StopServiceFailed; _log.InfoFormat("The {0} service is being stopped.", _settings.ServiceName); _serviceHandle.Stop(this); _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName); exitCode = TopshelfExitCode.Ok; } catch (Exception ex) { _settings.ExceptionCallback?.Invoke(ex); _log.Error("The service threw an exception during testing.", ex); } finally { _serviceHandle.Dispose(); } return(exitCode); }
void StopService() { try { if (_hasCancelled == false) { _log.InfoFormat("Stopping the {0} service", _settings.ServiceName); if (!_serviceHandle.Stop(this)) { throw new TopshelfException("The service failed to stop (returned false)."); } } } catch (Exception ex) { _log.Error("The service did not shut down gracefully", ex); } finally { _serviceHandle.Dispose(); _log.InfoFormat("The {0} service has stopped.", _settings.ServiceName); } }
public TopshelfExitCode Run() { try { _log.InfoFormat("The {0} service is being started.", _settings.ServiceName); _serviceHandle.Start(this); _log.InfoFormat("The {0} service was started.", _settings.ServiceName); Thread.Sleep(100); _log.InfoFormat("The {0} service is being stopped.", _settings.ServiceName); _serviceHandle.Stop(this); } catch (Exception ex) { _log.Error("The service did not shut down gracefully", ex); } finally { _serviceHandle.Dispose(); _log.InfoFormat("The {0} service was stopped.", _settings.ServiceName); } return(TopshelfExitCode.Ok); }
public void Dispose() { if (ServiceHandle != null) { ServiceHandle.Dispose(); ServiceHandle = null; } }
public void Dispose() { if (!_disposed) { _signalListener.Dispose(); _serviceHandle?.Dispose(); _stopSignal.Close(); _stopSignal.Dispose(); _disposed = true; } }
protected override void Dispose(bool disposing) { if (disposing && !_disposed) { if (_serviceHandle != null) { _serviceHandle.Dispose(); } _disposed = true; } base.Dispose(disposing); }
public void LoadService() { // Attempt to load the driver, then try again. ServiceHandle shandle; bool created = false; try { using (shandle = new ServiceHandle(_deviceName, ServiceAccess.Start)) { shandle.Start(); } } catch { using (ServiceManagerHandle scm = new ServiceManagerHandle(ScManagerAccess.CreateService)) { shandle = scm.CreateService( _deviceName, _deviceName, ServiceType.KernelDriver, Application.StartupPath + "\\kprocesshacker.sys" ); shandle.Start(); created = true; } } try { _fileHandle = new FileHandle( @"\Device\" + _deviceName, 0, FileAccess.GenericRead | FileAccess.GenericWrite ); } finally { if (created) { // The SCM will delete the service when it is stopped. shandle.Delete(); } shandle.Dispose(); } }
void UnloadServiceAppDomain() { try { _service.Dispose(); } finally { try { AppDomain.Unload(_appDomain); } catch (Exception ex) { _log.Error("Failed to unload AppDomain", ex); } } }
public void Dispose() { _serviceHandle.Dispose(); }
public KProcessHacker(string deviceName, string fileName) { _deviceName = deviceName; if (IntPtr.Size != 4) throw new NotSupportedException("KProcessHacker does not support 64-bit Windows."); try { _fileHandle = new FileHandle( @"\Device\" + deviceName, 0, FileAccess.GenericRead | FileAccess.GenericWrite ); } catch (WindowsException ex) { if ( ex.Status == NtStatus.NoSuchDevice || ex.Status == NtStatus.NoSuchFile || ex.Status == NtStatus.ObjectNameNotFound ) { ServiceHandle shandle; bool created = false; try { using (shandle = new ServiceHandle("KProcessHacker", ServiceAccess.Start)) { shandle.Start(); } } catch { using (var scm = new ServiceManagerHandle(ScManagerAccess.CreateService)) { shandle = scm.CreateService( deviceName, deviceName, ServiceType.KernelDriver, fileName ); shandle.Start(); created = true; } } try { _fileHandle = new FileHandle( @"\Device\" + deviceName, 0, FileAccess.GenericRead | FileAccess.GenericWrite ); } finally { if (shandle != null) { if (created) { shandle.Delete(); } shandle.Dispose(); } } } else { throw ex; } } _fileHandle.SetHandleFlags(Win32HandleFlags.ProtectFromClose, Win32HandleFlags.ProtectFromClose); byte[] bytes = _fileHandle.Read(4); fixed (byte* bytesPtr = bytes) _baseControlNumber = *(uint*)bytesPtr; try { _features = this.GetFeatures(); } catch { } }
/// <summary> /// Creates a connection to KProcessHacker. /// </summary> /// <param name="deviceName">The name of the KProcessHacker service and device.</param> /// <param name="fileName">The file name of the KProcessHacker driver.</param> public KProcessHacker(string deviceName, string fileName) { _deviceName = deviceName; if (IntPtr.Size != 4) { throw new NotSupportedException("KProcessHacker does not support 64-bit Windows."); } try { _fileHandle = new FileHandle( @"\Device\" + deviceName, 0, FileAccess.GenericRead | FileAccess.GenericWrite ); } catch (WindowsException ex) { if ( ex.Status == NtStatus.NoSuchDevice || ex.Status == NtStatus.NoSuchFile || ex.Status == NtStatus.ObjectNameNotFound ) { // Attempt to load the driver, then try again. ServiceHandle shandle; bool created = false; try { using (shandle = new ServiceHandle("KProcessHacker", ServiceAccess.Start)) { shandle.Start(); } } catch { using (var scm = new ServiceManagerHandle(ScManagerAccess.CreateService)) { shandle = scm.CreateService( deviceName, deviceName, ServiceType.KernelDriver, fileName ); shandle.Start(); created = true; } } try { _fileHandle = new FileHandle( @"\Device\" + deviceName, 0, FileAccess.GenericRead | FileAccess.GenericWrite ); } finally { if (shandle != null) { if (created) { // The SCM will delete the service when it is stopped. shandle.Delete(); } shandle.Dispose(); } } } else { throw ex; } } _fileHandle.SetHandleFlags(Win32HandleFlags.ProtectFromClose, Win32HandleFlags.ProtectFromClose); byte[] bytes = _fileHandle.Read(4); fixed(byte *bytesPtr = bytes) _baseControlNumber = *(uint *)bytesPtr; try { _features = this.GetFeatures(); } catch { } }