github.com/iDigitalFlame/xmt@v0.5.4/device/winapi/registry/handle.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package registry
    21  
    22  import (
    23  	"syscall"
    24  
    25  	"github.com/iDigitalFlame/xmt/device/winapi"
    26  )
    27  
    28  const (
    29  	// WoW64to32 is a bitmask used in any registry access flags that indicates
    30  	// to the kernel that you want to use the 32bit registry hives instead of
    31  	// the 64bit ones. Has no effect on 32bit systems.
    32  	WoW64to32 uint32 = 0x200
    33  	// WoW64to64 is a bitmask used in any registry access flags that indicates
    34  	// to the kernel that you want to use the 64bit registry hives instead of
    35  	// the 32bit ones. Has no effect on 32bit systems.
    36  	//
    37  	// NOTE(dij): I think this is the /default/ for 64bit systems tho?!
    38  	//            SO you might be able to safely ignore this flag if necessary.
    39  	WoW64to64 uint32 = 0x100
    40  )
    41  
    42  // Delete deletes the subkey path of the key and its values.
    43  //
    44  // This function fails with "invalid argument" if the key has subkeys or
    45  // values. Use 'DeleteTreeKey' instead to delete the full non-empty key.
    46  func Delete(k Key, s string) error {
    47  	return DeleteEx(k, s, 0)
    48  }
    49  
    50  // DeleteTree deletes the subkey path of the key and its values recursively.
    51  func DeleteTree(k Key, s string) error {
    52  	return winapi.RegDeleteTree(uintptr(k), s)
    53  }
    54  
    55  // Open opens a new key with path name relative to the key.
    56  //
    57  // It accepts any open root key, including CurrentUser for example, and returns
    58  // the new key and an any errors that may occur during opening.
    59  //
    60  // The access parameter specifies desired access rights to the key to be opened.
    61  func Open(k Key, s string, a uint32) (Key, error) {
    62  	v, err := winapi.UTF16PtrFromString(s)
    63  	if err != nil {
    64  		return 0, err
    65  	}
    66  	var h syscall.Handle
    67  	if err = syscall.RegOpenKeyEx(syscall.Handle(k), v, 0, a, &h); err != nil {
    68  		return 0, err
    69  	}
    70  	return Key(h), nil
    71  }
    72  
    73  // DeleteEx deletes the subkey path of the key and its values.
    74  //
    75  // This function fails with "invalid argument" if the key has subkeys or
    76  // values. Use 'DeleteTreeKey' instead to delete the full non-empty key.
    77  //
    78  // This function allows for specifying which WOW endpoint to delete from
    79  // leave the flags value as zero to use the current WOW/non-WOW registry point.
    80  //
    81  // NOTE(dij): WOW64_32 is 0x200 and WOW64_64 is 0x100
    82  func DeleteEx(k Key, s string, flags uint32) error {
    83  	return winapi.RegDeleteKeyEx(uintptr(k), s, flags)
    84  }
    85  
    86  // Create creates a key named path under the open key.
    87  //
    88  // CreateKey returns the new key and a boolean flag that reports whether the key
    89  // already existed.
    90  //
    91  // The access parameter specifies the access rights for the key to be created.
    92  func Create(k Key, s string, a uint32) (Key, bool, error) {
    93  	var (
    94  		h   uintptr
    95  		d   uint32
    96  		err = winapi.RegCreateKeyEx(uintptr(k), s, "", 0, a, nil, &h, &d)
    97  	)
    98  	if err != nil {
    99  		return 0, false, err
   100  	}
   101  	return Key(h), d == 2, nil
   102  }