本文整理匯總了Golang中code/google/com/p/winsvc/mgr.Connect函數的典型用法代碼示例。如果您正苦於以下問題:Golang Connect函數的具體用法?Golang Connect怎麽用?Golang Connect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Connect函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: controlService
func controlService(name string, c svc.Cmd, to svc.State) error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(name)
if err != nil {
return fmt.Errorf("could not access service: %v", err)
}
defer s.Close()
status, err := s.Control(c)
if err != nil {
return fmt.Errorf("could not send control=%d: %v", c, err)
}
timeout := time.Now().Add(10 * time.Second)
for status.State != to {
if timeout.Before(time.Now()) {
return fmt.Errorf("timeout waiting for service to go to state=%d", to)
}
time.Sleep(300 * time.Millisecond)
status, err = s.Query()
if err != nil {
return fmt.Errorf("could not retrieve service status: %v", err)
}
}
return nil
}
示例2: Install
func (ws *windowsService) Install() error {
exepath, err := osext.Executable()
if err != nil {
return err
}
// Used if path contains a space.
exepath = `"` + exepath + `"`
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(ws.name)
if err == nil {
s.Close()
return fmt.Errorf("service %s already exists", ws.name)
}
s, err = m.CreateService(ws.name, exepath, mgr.Config{
DisplayName: ws.displayName,
Description: ws.description,
StartType: mgr.StartAutomatic,
})
if err != nil {
return err
}
defer s.Close()
err = eventlog.InstallAsEventCreate(ws.name, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
return fmt.Errorf("InstallAsEventCreate() failed: %s", err)
}
return nil
}
示例3: RemoveService
func (this *Service) RemoveService() error {
l, err := eventlog.Open(this.name)
if err != nil {
return fmt.Errorf("Cannot open eventlog")
}
m, err := mgr.Connect()
if err != nil {
l.Error(3, fmt.Sprintf("remove service: cannot connect to windows service manager.", err))
return err
}
defer m.Disconnect()
s, err := m.OpenService(this.name)
if err != nil {
l.Error(3, fmt.Sprintf("remove service: service %s doesn't exists", this.name))
return fmt.Errorf("service %s doesn't exists", this.name)
}
err = s.Delete()
if err != nil {
return err
}
l.Info(1, "Service Removed. removing event log")
l.Close()
eventlog.Remove(this.name)
return nil
}
示例4: installService
func installService(name, desc string) error {
exepath, err := exePath()
if err != nil {
return err
}
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(name)
if err == nil {
s.Close()
return fmt.Errorf("service %s already exists", name)
}
s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc, StartType: mgr.StartAutomatic})
if err != nil {
return err
}
defer s.Close()
err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
return fmt.Errorf("SetupEventLogSource() failed: %s", err)
}
return nil
}
示例5: ConfigServiceAutoStart
func (this *Service) ConfigServiceAutoStart() error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
// log.Debug("Connected mgr\r\n")
s, err := m.OpenService(this.name)
if err != nil {
return fmt.Errorf("could not access service: %v", err)
}
defer s.Close()
c, err := s.Config()
if err != nil {
return fmt.Errorf("could not get service config: %v", err)
}
// make service automatically start
c.StartType = mgr.StartAutomatic
err = s.UpdateConfig(c)
if err != nil {
return fmt.Errorf("could not update service config: %v", err)
}
return nil
}
示例6: Start
func (ws *windowsService) Start() error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(ws.Name)
if err != nil {
return err
}
defer s.Close()
return s.Start([]string{})
}
示例7: Install
func (ws *windowsService) Install() error {
exepath, err := ws.execPath()
if err != nil {
return err
}
binPath := &bytes.Buffer{}
// Quote exe path in case it contains a string.
binPath.WriteRune('"')
binPath.WriteString(exepath)
binPath.WriteRune('"')
// Arguments are encoded with the binary path to service.
// Enclose arguments in quotes. Escape quotes with a backslash.
for _, arg := range ws.Arguments {
binPath.WriteRune(' ')
binPath.WriteString(`"`)
binPath.WriteString(strings.Replace(arg, `"`, `\"`, -1))
binPath.WriteString(`"`)
}
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(ws.Name)
if err == nil {
s.Close()
return fmt.Errorf("service %s already exists", ws.Name)
}
s, err = m.CreateService(ws.Name, binPath.String(), mgr.Config{
DisplayName: ws.DisplayName,
Description: ws.Description,
StartType: mgr.StartAutomatic,
ServiceStartName: ws.UserName,
Password: ws.Option.string("Password", ""),
Dependencies: ws.Dependencies,
})
if err != nil {
return err
}
defer s.Close()
err = eventlog.InstallAsEventCreate(ws.Name, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
return fmt.Errorf("InstallAsEventCreate() failed: %s", err)
}
return nil
}
示例8: Stop
func (ws *windowsService) Stop() error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(ws.Name)
if err != nil {
return err
}
defer s.Close()
_, err = s.Control(svc.Stop)
return err
}
示例9: TestOpenLanManServer
func TestOpenLanManServer(t *testing.T) {
m, err := mgr.Connect()
if err != nil {
t.Fatalf("SCM connection failed: %s", err)
}
defer m.Disconnect()
s, err := m.OpenService("lanmanserver")
if err != nil {
t.Fatalf("OpenService(lanmanserver) failed: %s", err)
}
_, err = s.Config()
if err != nil {
t.Fatalf("Config failed: %s", err)
}
defer s.Close()
}
示例10: InstallService
func (this *Service) InstallService() error {
// open logger
elog, err := eventlog.Open(this.name)
if err != nil {
// install logger
err = eventlog.InstallAsEventCreate(this.name, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
return fmt.Errorf("SetupEventLogSource() failed: %s", err)
}
// open
elog, err = eventlog.Open(this.name)
if err != nil {
return fmt.Errorf("Cannot open eventlog")
}
}
defer elog.Close()
m, err := mgr.Connect()
if err != nil {
elog.Error(3, fmt.Sprintf("install service: cannot connect to windows service manager.", err))
return err
}
defer m.Disconnect()
s, err := m.OpenService(this.name)
if err == nil {
s.Close()
elog.Error(3, fmt.Sprintf("install service: service %s already exists", this.name))
return fmt.Errorf("service %s already exists", this.name)
}
s, err = m.CreateService(this.name, this.path, mgr.Config{DisplayName: this.desc})
if err != nil {
elog.Error(3, fmt.Sprintf("install service: Failed to create service: %v", err))
return err
}
defer s.Close()
err = this.ConfigServiceAutoStart()
if err != nil {
elog.Error(3, fmt.Sprintf("install service: cannot set service to auto start: %v", err))
return err
}
return nil
}
示例11: startService
func startService(name string) error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(name)
if err != nil {
return fmt.Errorf("could not access service: %v", err)
}
defer s.Close()
err = s.Start([]string{"p1", "p2", "p3"})
if err != nil {
return fmt.Errorf("could not start service: %v", err)
}
return nil
}
示例12: TestMyService
func TestMyService(t *testing.T) {
const name = "myservice"
m, err := mgr.Connect()
if err != nil {
t.Fatalf("SCM connection failed: %s", err)
}
defer m.Disconnect()
c := mgr.Config{
StartType: mgr.StartDisabled,
DisplayName: "my service",
Description: "my service is just a test",
Dependencies: []string{"LanmanServer", "W32Time"},
}
exename := os.Args[0]
exepath, err := filepath.Abs(exename)
if err != nil {
t.Fatalf("filepath.Abs(%s) failed: %s", exename, err)
}
install(t, m, name, exepath, c)
s, err := m.OpenService(name)
if err != nil {
t.Fatalf("service %s is not installed", name)
}
defer s.Close()
c.BinaryPathName = exepath
c = testConfig(t, s, c)
c.StartType = mgr.StartManual
err = s.UpdateConfig(c)
if err != nil {
t.Fatalf("UpdateConfig failed: %v", err)
}
testConfig(t, s, c)
remove(t, s)
}
示例13: removeService
func removeService(name string) error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(name)
if err != nil {
return fmt.Errorf("service %s is not installed", name)
}
defer s.Close()
err = s.Delete()
if err != nil {
return err
}
err = eventlog.Remove(name)
if err != nil {
return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
}
return nil
}
示例14: Stop
func Stop() error {
ctrl, err := mgr.Connect()
if err != nil {
ctrl.Disconnect()
return err
}
defer ctrl.Disconnect()
service, err := ctrl.OpenService(ServiceName)
if err != nil {
return fmt.Errorf("error creating handle to %s: %v", ServiceName, err)
}
defer service.Close()
if _, err := service.Control(svc.Stop); err != nil {
return fmt.Errorf("error stopping service %s: %v", ServiceName, err)
}
return nil
}
示例15: Uninstall
func (ws *windowsService) Uninstall() error {
m, err := mgr.Connect()
if err != nil {
return err
}
defer m.Disconnect()
s, err := m.OpenService(ws.Name)
if err != nil {
return fmt.Errorf("service %s is not installed", ws.Name)
}
defer s.Close()
err = s.Delete()
if err != nil {
return err
}
err = eventlog.Remove(ws.Name)
if err != nil {
return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
}
return nil
}