Beispiel #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
}
Beispiel #2
0
func (self *SCManager) OpenService(serviceName string) (*Service, error) {
	handle, err := wrappers.OpenService(
		self.handle,
		syscall.StringToUTF16Ptr(serviceName),
		wrappers.SERVICE_ALL_ACCESS)
	if err != nil {
		return nil, NewWindowsError("OpenService", err)
	}
	return &Service{handle: handle}, nil
}