1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Package mgr can be used to manage Windows service programs.
8 // It can be used to install and remove them. It can also start,
9 // stop and pause them. The package can query / change current
10 // service state and config parameters.
19 "golang.org/x/sys/windows"
22 // Mgr is used to manage Windows service.
27 // Connect establishes a connection to the service control manager.
28 func Connect() (*Mgr, error) {
29 return ConnectRemote("")
32 // ConnectRemote establishes a connection to the
33 // service control manager on computer named host.
34 func ConnectRemote(host string) (*Mgr, error) {
37 s = syscall.StringToUTF16Ptr(host)
39 h, err := windows.OpenSCManager(s, nil, windows.SC_MANAGER_ALL_ACCESS)
43 return &Mgr{Handle: h}, nil
46 // Disconnect closes connection to the service control manager m.
47 func (m *Mgr) Disconnect() error {
48 return windows.CloseServiceHandle(m.Handle)
51 func toPtr(s string) *uint16 {
55 return syscall.StringToUTF16Ptr(s)
58 // toStringBlock terminates strings in ss with 0, and then
59 // concatenates them together. It also adds extra 0 at the end.
60 func toStringBlock(ss []string) *uint16 {
65 for _, s := range ss {
74 return &utf16.Encode([]rune(t))[0]
77 // CreateService installs new service name on the system.
78 // The service will be executed by running exepath binary.
79 // Use config c to specify service parameters.
80 // Any args will be passed as command-line arguments when
81 // the service is started; these arguments are distinct from
82 // the arguments passed to Service.Start or via the "Start
83 // parameters" field in the service's Properties dialog box.
84 func (m *Mgr) CreateService(name, exepath string, c Config, args ...string) (*Service, error) {
86 c.StartType = StartManual
88 if c.ErrorControl == 0 {
89 c.ErrorControl = ErrorNormal
91 if c.ServiceType == 0 {
92 c.ServiceType = windows.SERVICE_WIN32_OWN_PROCESS
94 s := syscall.EscapeArg(exepath)
95 for _, v := range args {
96 s += " " + syscall.EscapeArg(v)
98 h, err := windows.CreateService(m.Handle, toPtr(name), toPtr(c.DisplayName),
99 windows.SERVICE_ALL_ACCESS, c.ServiceType,
100 c.StartType, c.ErrorControl, toPtr(s), toPtr(c.LoadOrderGroup),
101 nil, toStringBlock(c.Dependencies), toPtr(c.ServiceStartName), toPtr(c.Password))
105 if c.Description != "" {
106 err = updateDescription(h, c.Description)
111 return &Service{Name: name, Handle: h}, nil
114 // OpenService retrieves access to service name, so it can
115 // be interrogated and controlled.
116 func (m *Mgr) OpenService(name string) (*Service, error) {
117 h, err := windows.OpenService(m.Handle, syscall.StringToUTF16Ptr(name), windows.SERVICE_ALL_ACCESS)
121 return &Service{Name: name, Handle: h}, nil
124 // ListServices enumerates services in the specified
125 // service control manager database m.
126 // If the caller does not have the SERVICE_QUERY_STATUS
127 // access right to a service, the service is silently
128 // omitted from the list of services returned.
129 func (m *Mgr) ListServices() ([]string, error) {
131 var bytesNeeded, servicesReturned uint32
138 err = windows.EnumServicesStatusEx(m.Handle, windows.SC_ENUM_PROCESS_INFO,
139 windows.SERVICE_WIN32, windows.SERVICE_STATE_ALL,
140 p, uint32(len(buf)), &bytesNeeded, &servicesReturned, nil, nil)
144 if err != syscall.ERROR_MORE_DATA {
147 if bytesNeeded <= uint32(len(buf)) {
150 buf = make([]byte, bytesNeeded)
152 if servicesReturned == 0 {
155 services := (*[1 << 20]windows.ENUM_SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0]))[:servicesReturned]
157 for _, s := range services {
158 name := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(s.ServiceName))[:])
159 names = append(names, name)