Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/sys: howto set a autorun registry in windows #32748

Closed
b1gcat opened this issue Jun 24, 2019 · 2 comments
Closed

x/sys: howto set a autorun registry in windows #32748

b1gcat opened this issue Jun 24, 2019 · 2 comments

Comments

@b1gcat
Copy link

b1gcat commented Jun 24, 2019

I write the code, but does not work. it cannot create xxx and no error found.

func SetAutoRun(run bool) error {
	k, err := registry.OpenKey(registry.CURRENT_USER,
		`HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run`,
		registry.QUERY_VALUE|registry.SET_VALUE)
	if err != nil {
		return err
	}
	defer k.Close()
	if run {
		if err := k.SetStringValue("xxx", SysExecPath("xxx.exe")); err != nil {
			return err
		}
	} else {
		k.DeleteValue("xxx")
	}
	return nil
}

another code:

	key, _, err := registry.CreateKey(registry.CURRENT_USER, "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", registry.ALL_ACCESS)
	if err != nil {
		return err
	}
	defer key.Close()

	if run {
		nkey, _, err := registry.CreateKey(key, "xxx", registry.ALL_ACCESS)
		if err != nil {
			return err
		}
		defer nkey.Close()
		err = nkey.SetStringValue("String", SysExecPath("xxx.exe"))
		if err != nil {
			return err
		}
	} else {
		key.DeleteValue("xxx")
	}
@gopherbot gopherbot added this to the Unreleased milestone Jun 24, 2019
@ghost
Copy link

ghost commented Jun 24, 2019

I didn't try running the code, but since you already are opening registry.CURRENT_USER, you can drop HKEY_CURRENT_USER from the path:

registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\Microsoft\Windows\CurrentVersion\Run`, ...)

@coolaj86
Copy link

coolaj86 commented Jul 14, 2019

Just posting for the sake of posterity:

The reason I came across this was that I was trying to create a user-mode service that would run on user login every time.

Here's how I did it, which is working for me presently:
https://git.rootprojects.org/root/serviceman/src/commit/f95897cf308c3e19d6fe4b088547d8290907e384/manager/install_windows.go#L60-L109 (pinned to a specific commit since the line numbers are likely to change as I clean stuff up).

I also needed to do similar for Mac and Linux, and that project is the cross-platform service manager I came up with, suited to my needs - which are more focused on user-mode services than system-level services.

@golang golang locked and limited conversation to collaborators Jul 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants