On this page:
get-resource
write-resource

9 Windows Registry

 (require file/resource)

(get-resource section 
  entry 
  [value-box 
  file 
  #:type type]) 
  (or/c #f string? bytes? exact-integer? #t)
  section : 
(or/c "HKEY_CLASSES_ROOT"
      "HKEY_CURRENT_CONFIG"
      "HKEY_CURRENT_USER"
      "HKEY_LOCAL_MACHINE"
      "HKEY_USERS")
  entry : string?
  value-box : (or/f #f (box/c (or/c string? bytes? exact-integer?)))
   = #f
  file : #f = #f
  type : (or/c 'string 'bytes 'integer) = derived-from-value-box
Gets a value from the Windows registry. Under platforms other than Windows, an exn:fail:unsupported exception is raised.

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.

The type argument determines how a value in the registry is converted to a Racket value. If value-box is a box, then the default type is derived from the initial box content, otherwise the default type is 'string.

Registry values of any format can be extracted. Values using the registry format REG_SZ are treated as strings, and values with the format REG_DWORD are treated as 32-bit signed integers. All other formats are treated as raw bytes. Data from the registry is converted to the requested type type:

The file argument is included for backward compatibility and must be #f.

To get the “default” value for an 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\\")

(write-resource section    
  entry    
  value    
  [file    
  #:type type    
  #:create-key? create-key?])  boolean?
  section : 
(or/c "HKEY_CLASSES_ROOT"
      "HKEY_CURRENT_CONFIG"
      "HKEY_CURRENT_USER"
      "HKEY_LOCAL_MACHINE"
      "HKEY_USERS")
  entry : string?
  value : (or/c string? bytes? exact-integer?)
  file : #f = #f
  type : (or/c 'string 'bytes 'integer) = 'string
  create-key? : any/c = #f
Write a value to the Windows registry. Under platforms other than Windows, an exn:fail:unsupported exception is raised.

The resource value is keyed on the combination of section and entry. If create-key? is false, 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 the format of the value in the registry: 'string writes using the REG_SZ format, 'bytes writes using the REG_BINARY format, and 'dword writes using the REG_DWORD format. Any kind of value can be converted for any kind of type using the inverse of the conversions for get-resource.

The file argument must be #f. A path is allowed for backward compatibility of arguments, but providing a path causes an exn:fail:unsupported exception to be raised.