Can anyone provide an example or stopping and/or starting an established Windows service?

3,717 views
Skip to first unread message

akc...@gmail.com

unread,
Feb 17, 2017, 11:07:34 AM2/17/17
to golang-nuts
I've just started learning the language and went through some of the packages that deal with file operations and that went well.  Has anyone had experience with starting and stopping windows services? I went and ran a go get on package:  "golang.org/x/sys/windows/svc/mgr".  Looking around and that seems like the correct package to be using?   I wanted to do a simple program to start the Printer Spooler service a windows 7 machine as a test but I seem to be striking out.

package main

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


func startService(name string) error {
m, err := mgr.Connect()
if err != nil {
return fmt.Errorf("Cannot connect to manager %v", err)
}
defer m.Disconnect()
s, err := m.OpenService(name)
if err != nil {
return fmt.Errorf("service %s does not exist", name)
}
defer s.Close()
s.Start()
if err != nil {
return fmt.Errorf("could not start the service: %v", err)
}
return nil
}


func main() {
startService("Spooler")
}


If anyone has any examples or point out what I'm doing wrong I'd appreciate it.  Thanks in advance!

Egon

unread,
Feb 17, 2017, 11:42:24 AM2/17/17
to golang-nuts

I'm guessing you are getting "Access Denied" and not seeing the error.

+ Egon

akc...@gmail.com

unread,
Feb 17, 2017, 1:42:00 PM2/17/17
to golang-nuts
What's driving me crazy is that if I change the name of the service being passed to something like "thisdoesnotexist" I don't get an error.   I would expect to see a error when it reaches this line in the function:

service, err := manager.OpenService(name)
if err != nil {
return fmt.Errorf("service %s does not exist: %v", name, err)
}

Egon

unread,
Feb 17, 2017, 2:07:38 PM2/17/17
to golang-nuts
Is your code up to date?
What happens, if you do:
fmt.Println(service, err)
fmt.Println(service.Config())

akc...@gmail.com

unread,
Feb 17, 2017, 3:06:47 PM2/17/17
to golang-nuts
I copied your code from playground.  I will make that change and let you know.

akc...@gmail.com

unread,
Feb 18, 2017, 5:33:18 AM2/18/17
to golang-nuts
Same thing.  Code runs and I get nothing.  That's really weird.

Egon

unread,
Feb 18, 2017, 6:05:57 AM2/18/17
to golang-nuts
Just checking whether I got my message across clearly :).
When I said, whether the code is up to date, I meant whether you had the latest golang.org/x/sys?

But yeah, sounds really weird.

Do you get empty lines or nil at least?
What version of go?

Anyways, I would then go try adding println-s into golang.org/x/sys or try diving in with delve/gdb.

+ Egon

akc...@gmail.com

unread,
Feb 21, 2017, 8:55:08 AM2/21/17
to golang-nuts
Sorry.  Yes, The package is up to date.  I did a 'go get' on the package on Feb 17.   I'm running version 1.73.  Will get on with gdb.  Thanks for your help!  I really appreciate it!

Tamás Gulácsi

unread,
Feb 21, 2017, 9:40:25 AM2/21/17
to golang-nuts
Reply all
Reply to author
Forward
0 new messages