英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论