Skip to content

winreg #

WindowsRegistry

Windows registry wrappers for Vlang

Package vpm.winreg

Documentation

Documentation

Funcionalidades

  • Update and delete value
  • List value information
  • Read value of type only REG_SZ and REG_DWORD
  • Read value of type REG_BINARY, REG_QDWORD, REG_DWORD_BIG_ENDIAN and REG_EXPAND_SZ
  • Write value of type only REG_SZ and REG_DWORD
  • Write value of type REG_BINARY, REG_QDWORD, REG_DWORD_BIG_ENDIAN and REG_EXPAND_SZ
  • create new keys
  • Delete keys

How to use

Getting a string value.

import Ddiidev.winreg

h := winreg.open_key(.hkey_local_machine, r'SOFTWARE\Microsoft\Windows\CurrentVersion', .key_read)!

value := h.query_value[string]('ProgramFilesDir')!

println(value)

It is possible to get a value without needing to know the type of the value in the registry.

import Ddiidev.winreg

h := winreg.open_key(.hkey_local_machine, r'SOFTWARE\Microsoft\Windows\CurrentVersion', .key_read)!

value := h.get_value('ProgramFilesDir')!

println(value)

if value is string {
    println('value is string')
}

Defining and creating new value. (Remembering that you need to be as ADM on Windows)

    value_test := 'my_test'

    h := winreg.open_key(.hkey_local_machine, r'SOFTWARE\Microsoft\Windows\CurrentVersion', .key_write)!

    h.set_value('test', value_test)!

fn open_key #

fn open_key(hkey HKEYS, subkey string, mode AccessMode) !HandleKey

fn DwType.get #

fn DwType.get(c u32) !DwType

fn DwType.has #

fn DwType.has(c u32) bool

type DwValue #

type DwValue = f32 | int | string

fn (DwValue) str #

fn (d DwValue) str() string

enum AccessMode #

enum AccessMode {
	key_read      = 0x20019
	key_write     = 0x20006
	key_all_acess = 0xF003F
}

enum DwType #

enum DwType as u32 {
	reg_none             = 0
	reg_sz               = 1
	reg_expand_sz        = 2
	reg_binary           = 3
	reg_dword            = 4
	reg_dword_big_endian = 5
	reg_multi_sz         = 7
	reg_qword            = 11
}

enum HKEYS #

enum HKEYS as u64 {
	hkey_local_machine = 0x80000002
	hkey_current_user  = 0x80000001
	hkey_classes_root  = 0x80000000
	hkey_users         = 0x80000003
}

HKEYS are the keys of the Windows registry.

enum Reserved #

enum Reserved as u32 {
	rrf_rt_reg_dword = 0x00000010
}

struct HandleKey #

@[noinit]
struct HandleKey {
	hkey HKEYS
	mode AccessMode
pub:
	subkey   string
	hkey_ptr voidptr
}

fn (HandleKey) close #

fn (h HandleKey) close() !

close closes a connection to windows registry.

How to use:

handle_key := winreg.open_key(.hkey_local_machine, r'SOFTWARE\Microsoft\Windows\CurrentVersion', .key_read)!

handle_key.close()!

If any error occurs due to lack of permission, etc... it will return a winerror.ErrorRegistry

fn (HandleKey) delete_value #

fn (h HandleKey) delete_value(reg string) !

delete_value deletes a value from the registry. To delete registry values, the application must have elevated permissions on the OS.

How to use:

handle_key.delete_value('ProgramFilesDir')! // Be careful when testing! ⚠️

If any error occurs due to lack of permission, etc... it will return a winerror.ErrorRegistry

fn (HandleKey) enumerate_values_info #

fn (h HandleKey) enumerate_values_info() ![]InfoValues

enumerate_values_info Enumerates the values information of a registry key. Retrieves information about all the values present in the registry key.

How to use:

 h := winreg.open_key(.hkey_current_user, r'Software\Microsoft\Windows\CurrentVersion')

 values := h.enumerate_values_info()!

    for value in values {

        println('Value Name: ${value.name} | Value Type: ${value.typ}')

    }

If any error occurs due to lack of permission, etc... it will return a winerror.ErrorRegistry

fn (HandleKey) get_value #

fn (h HandleKey) get_value(reg string) !DwValue

get_value takes a value of types REG_SZ and REG_DWORD, and returns it as DwValue. If you don't know the type of the value, this function can help a lot.

How to use:

program_files_dir := handle_key.get_value('ProgramFilesDir')!

if program_files_dir is string {
    println('program_files_dir is string: "$program_files_dir}"')
} else {
    println('program_files_dir is int: "$program_files_dir}"')
}

If any error occurs due to lack of permission, etc... it will return a winerror.ErrorRegistry

fn (HandleKey) query_value #

fn (h HandleKey) query_value[T](reg string) !T

query_value[T] takes a value of types REG_SZ and REG_DWORD, and returns it with the specified generic type. You need to know exactly what type is expected, if the value is a REG_DWORD and T is equal to a string Some invalid data will then be returned.

How to use:

program_files_dir := handle_key.query_value[string]('ProgramFilesDir')!

println('program_files_dir is my string: "$program_files_dir}"')

If any error occurs due to lack of permission, etc... it will return a winerror.ErrorRegistry

fn (HandleKey) set_value #

fn (h HandleKey) set_value(reg string, dw_value DwValue) !

set_value modifies and creates a new value in the registry. Its REG_SZ and REG_DWORD value is given through the value passed in dw_value.

How to use:

handle_key.set_value('test', 123)! // REG_DWORD
handle_key.set_value('test', 123.3)! // REG_SZ
handle_key.set_value('test', '123')! // REG_SZ

If any error occurs due to lack of permission, etc... it will return a winerror.ErrorRegistry

struct InfoValues #

struct InfoValues {
pub mut:
	name string
	typ  DwType
}