github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/syscall/windows/registry/key.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build windows
     6  
     7  // Package registry provides access to the Windows registry.
     8  //
     9  // Here is a simple example, opening a registry key and reading a string value from it.
    10  //
    11  //	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
    12  //	if err != nil {
    13  //		log.Fatal(err)
    14  //	}
    15  //	defer k.Close()
    16  //
    17  //	s, _, err := k.GetStringValue("SystemRoot")
    18  //	if err != nil {
    19  //		log.Fatal(err)
    20  //	}
    21  //	fmt.Printf("Windows system root is %q\n", s)
    22  //
    23  // NOTE: This package is a copy of golang.org/x/sys/windows/registry
    24  // with KeyInfo.ModTime removed to prevent dependency cycles.
    25  package registry
    26  
    27  import (
    28  	"github.com/shogo82148/std/syscall"
    29  )
    30  
    31  const (
    32  	// Registry key security and access rights.
    33  	// See https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-key-security-and-access-rights
    34  	// for details.
    35  	ALL_ACCESS         = 0xf003f
    36  	CREATE_LINK        = 0x00020
    37  	CREATE_SUB_KEY     = 0x00004
    38  	ENUMERATE_SUB_KEYS = 0x00008
    39  	EXECUTE            = 0x20019
    40  	NOTIFY             = 0x00010
    41  	QUERY_VALUE        = 0x00001
    42  	READ               = 0x20019
    43  	SET_VALUE          = 0x00002
    44  	WOW64_32KEY        = 0x00200
    45  	WOW64_64KEY        = 0x00100
    46  	WRITE              = 0x20006
    47  )
    48  
    49  // Key is a handle to an open Windows registry key.
    50  // Keys can be obtained by calling OpenKey; there are
    51  // also some predefined root keys such as CURRENT_USER.
    52  // Keys can be used directly in the Windows API.
    53  type Key syscall.Handle
    54  
    55  const (
    56  	// Windows defines some predefined root keys that are always open.
    57  	// An application can use these keys as entry points to the registry.
    58  	// Normally these keys are used in OpenKey to open new keys,
    59  	// but they can also be used anywhere a Key is required.
    60  	CLASSES_ROOT   = Key(syscall.HKEY_CLASSES_ROOT)
    61  	CURRENT_USER   = Key(syscall.HKEY_CURRENT_USER)
    62  	LOCAL_MACHINE  = Key(syscall.HKEY_LOCAL_MACHINE)
    63  	USERS          = Key(syscall.HKEY_USERS)
    64  	CURRENT_CONFIG = Key(syscall.HKEY_CURRENT_CONFIG)
    65  )
    66  
    67  // Close closes open key k.
    68  func (k Key) Close() error
    69  
    70  // OpenKey opens a new key with path name relative to key k.
    71  // It accepts any open key, including CURRENT_USER and others,
    72  // and returns the new key and an error.
    73  // The access parameter specifies desired access rights to the
    74  // key to be opened.
    75  func OpenKey(k Key, path string, access uint32) (Key, error)
    76  
    77  // ReadSubKeyNames returns the names of subkeys of key k.
    78  func (k Key) ReadSubKeyNames() ([]string, error)
    79  
    80  // CreateKey creates a key named path under open key k.
    81  // CreateKey returns the new key and a boolean flag that reports
    82  // whether the key already existed.
    83  // The access parameter specifies the access rights for the key
    84  // to be created.
    85  func CreateKey(k Key, path string, access uint32) (newk Key, openedExisting bool, err error)
    86  
    87  // DeleteKey deletes the subkey path of key k and its values.
    88  func DeleteKey(k Key, path string) error
    89  
    90  // A KeyInfo describes the statistics of a key. It is returned by Stat.
    91  type KeyInfo struct {
    92  	SubKeyCount     uint32
    93  	MaxSubKeyLen    uint32
    94  	ValueCount      uint32
    95  	MaxValueNameLen uint32
    96  	MaxValueLen     uint32
    97  	lastWriteTime   syscall.Filetime
    98  }
    99  
   100  // Stat retrieves information about the open key k.
   101  func (k Key) Stat() (*KeyInfo, error)