当前位置: 首页>>代码示例>>Golang>>正文


Golang mgr.Connect函数代码示例

本文整理汇总了Golang中golang.org/x/sys/windows/svc/mgr.Connect函数的典型用法代码示例。如果您正苦于以下问题:Golang Connect函数的具体用法?Golang Connect怎么用?Golang Connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Connect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: installService

func installService(name, desc string, args ...string) error {
	exepath, err := exePath()
	if err != nil {
		return err
	}
	if len(args) > 0 {
		exepath = exepath + " " + strings.Join(args, " ")
	}
	println("exepath:", exepath)

	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:   windows.SERVICE_AUTO_START,
	})
	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
}
开发者ID:iolg,项目名称:golangdoc,代码行数:35,代码来源:service_manage_windows.go

示例2: removeService

func removeService(name string) error {
	log.Info("Removing service")

	m, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err != nil {
		log.Info("Service not installed")
		return ServiceNotFound
	}
	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)
	}
	log.Info("Service removed")
	return nil
}
开发者ID:Nanocloud,项目名称:community,代码行数:25,代码来源:service.go

示例3: 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: 2}, "is", "auto-started")
	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
}
开发者ID:procks,项目名称:direct_print_server,代码行数:27,代码来源:install.go

示例4: createService

func createService(c *cli.Context) {
	exePath, err := filepath.Abs("gcp-windows-connector.exe")
	if err != nil {
		fmt.Printf("Failed to find the connector executable: %s\n", err)
		os.Exit(1)
	}

	m, err := mgr.Connect()
	if err != nil {
		fmt.Printf("Failed to connect to service control manager: %s\n", err)
		os.Exit(1)
	}
	defer m.Disconnect()

	config := mgr.Config{
		DisplayName:  lib.ConnectorName,
		Description:  "Shares printers with Google Cloud Print",
		Dependencies: []string{"Spooler"},
		StartType:    mgr.StartAutomatic,
	}
	service, err := m.CreateService(lib.ConnectorName, exePath, config)
	if err != nil {
		fmt.Printf("Failed to create service: %s\n", err)
		os.Exit(1)
	}
	defer service.Close()

	fmt.Println("Service created successfully")
}
开发者ID:jacobmarble,项目名称:cloud-print-connector,代码行数:29,代码来源:main_windows.go

示例5: 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
}
开发者ID:2722,项目名称:lantern,代码行数:28,代码来源:manage.go

示例6: Install

func (ws *windowsService) Install() error {
	exepath, err := ws.execPath()
	if err != nil {
		return err
	}

	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,
		ServiceStartName: ws.UserName,
		Password:         ws.Option.string("Password", ""),
		Dependencies:     ws.Dependencies,
	}, ws.Arguments...)
	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
}
开发者ID:sdhjl2000,项目名称:service,代码行数:35,代码来源:service_windows.go

示例7: uninstallService

func uninstallService(name string) {
	log.Infof("Uninstalling service %s", name)
	m, err := mgr.Connect()
	if err != nil {
		log.Errorf("Failed to connect to service manager: %v", err)
	}
	defer m.Disconnect()

	s, err := m.OpenService(name)
	// service exist so we try to uninstall it
	if err != nil {
		log.Debugf("Service %s doesn't exist, no uninstall action needed", name)
		return
	}

	defer s.Close()
	err = s.Delete()
	if err != nil {
		log.Errorf("Can't delete service %s: %v", name, err)
	}

	err = eventlog.Remove(s.Name)
	if err != nil {
		log.Errorf("RemoveEventLogSource() failed: %s", err)
	}

	return
}
开发者ID:Graylog2,项目名称:collector-sidecar,代码行数:28,代码来源:control_handler_windows.go

示例8: InstallService

func InstallService(appPath, name, desc string, params ...string) error {
	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("winsvc.InstallService: service %s already exists", name)
	}
	s, err = m.CreateService(name, appPath,
		mgr.Config{
			DisplayName: desc,
			StartType:   windows.SERVICE_AUTO_START,
		},
		params...,
	)
	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("winsvc.InstallService: InstallAsEventCreate failed, err = %v", err)
	}
	return nil
}
开发者ID:chai2010,项目名称:winsvc,代码行数:29,代码来源:service.go

示例9: QueryService

func QueryService(name string) (status string, err error) {
	m, err := mgr.Connect()
	if err != nil {
		return
	}
	defer m.Disconnect()
	s, err := m.OpenService(name)
	if err != nil {
		err = fmt.Errorf("winsvc.QueryService: could not access service: %v", err)
		return
	}
	defer s.Close()

	statusCode, err := s.Query()
	if err != nil {
		return
	}
	switch statusCode.State {
	case svc.Stopped:
		return "Stopped", nil
	case svc.StartPending:
		return "StartPending", nil
	case svc.StopPending:
		return "StopPending", nil
	case svc.Running:
		return "Running", nil
	case svc.ContinuePending:
		return "ContinuePending", nil
	case svc.PausePending:
		return "PausePending", nil
	case svc.Paused:
		return "Paused", nil
	}
	panic("unreached")
}
开发者ID:chai2010,项目名称:winsvc,代码行数:35,代码来源:service.go

示例10: 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
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:33,代码来源:service_windows.go

示例11: TestMyService

func TestMyService(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode - it modifies system services")
	}

	const name = "myservice"

	m, err := mgr.Connect()
	if err != nil {
		if errno, ok := err.(syscall.Errno); ok && errno == syscall.ERROR_ACCESS_DENIED {
			t.Skip("Skipping test: we don't have rights to manage services.")
		}
		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)
}
开发者ID:Chengyumeng,项目名称:sys,代码行数:50,代码来源:mgr_test.go

示例12: ValidateBeforeStart

func (r *SvcRunner) ValidateBeforeStart() error {
	execPath, err := exec.LookPath(r.exec)
	if err != nil {
		return backends.SetStatusLogErrorf(r.name, "Failed to find collector executable %s", r.exec)
	}

	m, err := mgr.Connect()
	if err != nil {
		return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
	}
	defer m.Disconnect()

	serviceConfig := mgr.Config{
		DisplayName:    "Graylog collector sidecar - " + r.name + " backend",
		Description:    "Wrapper service for the NXLog backend",
		BinaryPathName: "\"" + r.exec + "\" " + strings.Join(r.args, " ")}

	s, err := m.OpenService(r.serviceName)
	// service exist so we only update the properties
	if err == nil {
		defer s.Close()
		log.Debugf("[%s] service %s already exists, updating properties", r.name)
		currentConfig, err := s.Config()
		if err == nil {
			currentConfig.DisplayName = serviceConfig.DisplayName
			currentConfig.Description = serviceConfig.Description
			currentConfig.BinaryPathName = serviceConfig.BinaryPathName
		}
		err = s.UpdateConfig(currentConfig)
		if err != nil {
			backends.SetStatusLogErrorf(r.name, "Failed to update service: %v", err)
		}
		// service needs to be created
	} else {
		s, err = m.CreateService(r.serviceName,
			execPath,
			serviceConfig)
		if err != nil {
			backends.SetStatusLogErrorf(r.name, "Failed to install service: %v", err)
		}
		defer s.Close()
		err = eventlog.InstallAsEventCreate(r.serviceName, eventlog.Error|eventlog.Warning|eventlog.Info)
		if err != nil {
			s.Delete()
			backends.SetStatusLogErrorf(r.name, "SetupEventLogSource() failed: %v", err)
		}
	}

	return nil
}
开发者ID:Graylog2,项目名称:collector-sidecar,代码行数:50,代码来源:svc_runner_windows.go

示例13: 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()
}
开发者ID:sdhjl2000,项目名称:service,代码行数:14,代码来源:service_windows.go

示例14: 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
}
开发者ID:AdrianoJS,项目名称:gitlab-ci-multi-runner,代码行数:15,代码来源:service_windows.go

示例15: 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("winsvc.StartService: could not access service: %v", err)
	}
	defer s.Close()
	err = s.Start("p1", "p2", "p3")
	if err != nil {
		return fmt.Errorf("winsvc.StartService: could not start service: %v", err)
	}
	return nil
}
开发者ID:chai2010,项目名称:winsvc,代码行数:17,代码来源:service.go


注:本文中的golang.org/x/sys/windows/svc/mgr.Connect函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。