Esempio n. 1
0
func IsServiceRunning(serviceName string) (bool, error) {
	// This function requires fewer access rights than using the above classes and can be used to check if a service
	// is running without administrator access.
	scmhandle, err := wrappers.OpenSCManager(
		nil,
		syscall.StringToUTF16Ptr(wrappers.SERVICES_ACTIVE_DATABASE),
		wrappers.SC_MANAGER_CONNECT)
	if err != nil {
		return false, NewWindowsError("OpenSCManager", err)
	}
	defer wrappers.CloseServiceHandle(scmhandle)
	handle, err := wrappers.OpenService(
		scmhandle,
		syscall.StringToUTF16Ptr(serviceName),
		wrappers.SERVICE_QUERY_STATUS)
	if err == wrappers.ERROR_SERVICE_DOES_NOT_EXIST {
		return false, nil
	} else if err != nil {
		return false, NewWindowsError("OpenService", err)
	}
	defer wrappers.CloseServiceHandle(handle)
	var status wrappers.SERVICE_STATUS
	if err := wrappers.QueryServiceStatus(handle, &status); err != nil {
		return false, NewWindowsError("QueryServiceStatus", err)
	}
	return status.CurrentState == wrappers.SERVICE_RUNNING, nil
}
Esempio n. 2
0
func (self *Service) GetStatus() (*ServiceStatusInfo, error) {
	var status wrappers.SERVICE_STATUS
	if err := wrappers.QueryServiceStatus(self.handle, &status); err != nil {
		return nil, NewWindowsError("QueryServiceStatus", err)
	}
	return &ServiceStatusInfo{
		ServiceType:             ServiceType(status.ServiceType),
		CurrentState:            ServiceState(status.CurrentState),
		ControlsAccepted:        ServiceControlsAccepted(status.ControlsAccepted),
		Win32ExitCode:           status.Win32ExitCode,
		ServiceSpecificExitCode: status.ServiceSpecificExitCode,
		CheckPoint:              status.CheckPoint,
		WaitHint:                status.WaitHint,
	}, nil
}