操作系统中的服务列表 – golang

huangapple go评论85阅读模式
英文:

List of services in OS - golang

问题

你好!你可以使用golang.org/x/sys/windows/svc包来获取在Windows下当前安装的服务列表。以下是一个示例代码:

package main

import (
	"fmt"
	"golang.org/x/sys/windows/svc"
	"golang.org/x/sys/windows/svc/mgr"
)

func main() {
	manager, err := mgr.Connect()
	if err != nil {
		fmt.Println("Failed to connect to service manager:", err)
		return
	}
	defer manager.Disconnect()

	services, err := manager.ListServices()
	if err != nil {
		fmt.Println("Failed to retrieve service list:", err)
		return
	}

	fmt.Println("Currently installed services:")
	for _, service := range services {
		fmt.Println(service.DisplayName)
	}
}

这段代码连接到服务管理器,然后使用ListServices方法获取当前安装的服务列表,并打印出每个服务的显示名称。

希望对你有帮助!如果你有任何其他问题,请随时问我。

英文:

How can I get the list of currently installed services in golang under Windows?

I need something like:

List of currently running process in golang

but for services and not process.

答案1

得分: 5

标准库中没有这样的函数,而且很可能永远不会有。

考虑使用os/exec中的一个函数来启动一个Windows程序,该程序将列出可用的服务并解析其输出(例如,"sc query state=all")。你可以在这里找到相关函数的文档。

英文:

There is no such function in the standard library and there will likely never be.

Consider using one of the functions in os/exec to launch a Windows program that will list the available services and parse its output (e.g. "sc query state=all").

答案2

得分: 2

这是一个旧帖子,但我还是决定分享这个链接。
https://godoc.org/golang.org/x/sys/windows/svc/mgr

这个包提供了一个API,用于在本地和远程系统上创建、控制和列出Windows服务。

我从上面的链接中复制了以下文本:

func (m *Mgr) ListServices() ([]string, error)

ListServices函数在指定的服务控制管理器数据库m中枚举服务。如果调用者没有对服务的SERVICE_QUERY_STATUS访问权限,该服务将在返回的服务列表中被静默忽略。

英文:

This is an old post, but figured I would share this link anyways.
https://godoc.org/golang.org/x/sys/windows/svc/mgr

This package provides an API for creating, controlling and listing windows services on both the local and remote systems.

I copied the following text from the above link:

func (m *Mgr) ListServices() ([]string, error)

ListServices enumerates services in the specified service control manager database m. If the caller does not have the SERVICE_QUERY_STATUS access right to a service, the service is silently omitted from the list of services returned.

huangapple
  • 本文由 发表于 2015年3月24日 23:27:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/29236609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定