On this page:
get-resource
write-resource

13 Windows Registry

 (require file/resource) package: base

procedure

(get-resource section 
  entry 
  [value-box 
  file 
  #:type type]) 
  (or/c #f string? bytes? exact-integer? #t)
  section : string?
  entry : string?
  value-box : (or/c #f (box/c (or/c string? bytes? exact-integer?)))
   = #f
  file : (or/c #f path-string?) = #f
  type : (or/c 'string 'string/utf-16 'bytes 'bytes* 'integer)
   = derived-from-value-box
Gets a value from the Windows registry or an ".ini" file. For backward compatibility, the result is #f for platforms other than Windows. The registry is read when file is #f and when section is "HKEY_CLASSES_ROOT", "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", or "HKEY_USERS". When file is #f and section is not one of the special registry strings, then (build-path (find-system-path 'home-dir) "mred.ini") is read.

The resource value is keyed on the combination of section and entry. The result is #f if no value is found for the specified section and entry. If value-box is a box, then the result is #t if a value is found, and the box is filled with the value; when value-box is #f, the result is the found value.

Registry values of any format can be extracted. A combination of the type argument and the type of the resource determines how the resource is initially converted to a Racket value:

That initial conversion produces either a string or a byte string. The requested type might then trigger an additional transformation:

If value-box is a box, then the default type is derived from the initial box content: 'string, 'bytes, or 'integer. Otherwise, the default type is 'string.

Resources from ".ini" files are always strings, and are converted like REG_SZ registry values.

To get the “default” value for a registry entry, use a trailing backslash. For example, the following expression gets a command line for starting a browser:

(get-resource "HKEY_CLASSES_ROOT"
              "htmlfile\\shell\\open\\command\\")

Changed in version 8.0.0.10 of package base: Added 'sting/utf-16 and 'bytes* options for type.

procedure

(write-resource section    
  entry    
  value    
  [file    
  #:type type    
  #:create-key? create-key?])  boolean?
  section : string?
  entry : string?
  value : (or/c string? bytes? exact-integer?)
  file : (or/c path-string? #f) = #f
  type : 
(or/c 'string 'expand-string 'bytes 'dword
      'bytes/string 'bytes/expand-string)
 = 'string
  create-key? : any/c = #f
Write a value to the Windows registry or an ".ini" file. For backward compatibility, the result is #f for platforms other than Windows. The registry is written when file is #f and when section is "HKEY_CLASSES_ROOT", "HKEY_CURRENT_CONFIG", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", or "HKEY_USERS". When file is #f and section is not one of the special registry strings, then (build-path (find-system-path 'home-dir) "mred.ini") is written.

The resource value is keyed on the combination of section and entry. If create-key? is false when writing to the registry, the resource entry must already exist, otherwise the write fails. The result is #f if the write fails or #t if it succeeds.

The type argument determines both the format of the value written to the registry and its conversion of the to bytes:

When writing to an ".ini" file, the format is always a string, independent of type.

Changed in version 8.0.0.10 of package base: Added 'expand-string, 'bytes/string, and 'bytes/expand-string options for type.