winpath

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: MPL-2.0 Imports: 4 Imported by: 0

README

winpath

An example of getting, setting, and broadcasting PATHs on Windows.

This requires the unsafe package to use a syscall with special message poitners to update PATH without a reboot. It will also build without unsafe.

go build -tags unsafe -o winpath.exe
winpath show

        %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
        C:\Users\me\AppData\Local\Programs\Microsoft VS Code\bin
        %USERPROFILE%\go\bin
        C:\Users\me\AppData\Roaming\npm
        C:\Users\me\AppData\Local\Keybase\
winpath append C:\someplace\special

	Run the following for changes to take affect immediately:
	PATH %PATH%;C:\someplace\special
winpath prepend C:\someplace\special

	Run the following for changes to take affect immediately:
	PATH C:\someplace\special;%PATH%
winpath remove C:\someplace\special

Special Considerations

Giving away the secret sauce right here:

  • HWND_BROADCAST
  • WM_SETTINGCHANGE

This is essentially the snippet you need to have the HKCU and HKLM Environment registry keys propagated without rebooting:

	HWND_BROADCAST   := uintptr(0xffff)
	WM_SETTINGCHANGE := uintptr(0x001A)
	_, _, err := syscall.
		NewLazyDLL("user32.dll").
		NewProc("SendMessageW").
		Call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("ENVIRONMENT"))))

  • os.Getenv("COMSPEC")
  • os.Getenv("SHELL")

If you check SHELL and it isn't empty, then you're probably in MINGW or some such. If that's empty but COMSPEC isn't, you can be reasonably sure that you're in cmd.exe or Powershell.

Documentation

Overview

Package winpath is useful for managing PATH as part of the Environment in the Windows HKey Local User registry. It returns an error for most operations on non-Windows systems.

Index

Constants

This section is empty.

Variables

View Source
var ErrWrongPlatform = fmt.Errorf("method not implemented on this platform")

ErrWrongPlatform indicates that this was not built for Windows

Functions

func Add

func Add(p string) (bool, error)

Add will rewrite the Windows registry HKLU Environment, prepending the given directory path to the user's PATH. It will return whether the PATH was modified and an error if it should have been modified, but wasn't.

func IndexOf

func IndexOf(paths []string, p string) int

IndexOf searches the given path list for first occurence of the given path entry and returns the index, or -1

func NormalizePathEntry

func NormalizePathEntry(pathentry string) (string, string)

NormalizePathEntry will return the given directory path relative from its absolute path to the %USERPROFILE% (home) directory.

func Paths

func Paths() ([]string, error)

Paths returns all PATHs according to the Windows HKLU registry (or nil on non-windows platforms)

func Remove

func Remove(p string) (bool, error)

Remove will rewrite the Windows registry HKLU Environment without the given directory path. It will return whether the PATH was modified and an error if it should have been modified, but wasn't.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL