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 }
func (self *SCManager) Close() error { if self.handle != 0 { if err := wrappers.CloseServiceHandle(self.handle); err != nil { return NewWindowsError("CloseServiceHandle", err) } self.handle = 0 } return nil }