Functions

Helper functions

wslwinreg.common.winerror_to_errno

wslwinreg.common.winerror_to_errno(winerror)

Convert a Windows error code into the matching errno code.

Parameters

winerror – Integer error code number returned by Windows

Returns

Corresponding errno error code or EINVAL if no match.

wslwinreg.common.convert_to_utf16

wslwinreg.common.convert_to_utf16(input_string)

Convert the input string into utf-16-le.

Parameters

input_string – string encoded in locale.getpreferredencoding()

Returns

Byte array in little endian UTF-16

wslwinreg.common.to_registry_bytes

wslwinreg.common.to_registry_bytes(value, typ)

Convert input data into appropriate Windows registry type.

Exception

ValueError for invalid input or TypeError for bad type.

Parameters
  • value – Value to convert

  • typ – Windows registry type to convert to (Example REG_DWORD)

Returns

A ctypes.c_char array.

wslwinreg.common.from_registry_bytes

wslwinreg.common.from_registry_bytes(input_data, input_size, typ)

Convert raw Windows registry data into an appropriate Python object.

Parameters
  • input_data – Raw binary data

  • input_size – Size in bytes of the input data

  • typ – Windows registry type to convert from (Example REG_DWORD)

Returns

Data converted to appropriate Python object, or None.

wslwinreg.get_HKCU

wslwinreg.get_HKCU()

Open HKEY_CURRENT_USER.

Returns

Read only WinRegKey for HKEY_CURRENT_USER

wslwinreg.get_HKLM_32

wslwinreg.get_HKLM_32()

Open HKEY_LOCAL_MACHINE, 32 bit view.

Returns

Read only WinRegKey for HKEY_LOCAL_MACHINE with KEY_WOW64_32KEY

wslwinreg.get_HKLM_64

wslwinreg.get_HKLM_64()

Open HKEY_LOCAL_MACHINE, 64 bit view.

Returns

Read only WinRegKey for HKEY_LOCAL_MACHINE with KEY_WOW64_64KEY

Null implementation

On operating systems such as macOS and Linux that doesn’t have a Windows operating system underpinning, all functions will raise a NotImplementedError exception.

wslwinreg.nullapi.CloseKey

wslwinreg.nullapi.CloseKey(hkey)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.ConnectRegistry

wslwinreg.nullapi.ConnectRegistry(computer_name, key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.CreateKey

wslwinreg.nullapi.CreateKey(key, sub_key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.CreateKeyEx

wslwinreg.nullapi.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.DeleteKey

wslwinreg.nullapi.DeleteKey(key, sub_key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.DeleteKeyEx

wslwinreg.nullapi.DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.DeleteValue

wslwinreg.nullapi.DeleteValue(key, value)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.EnumKey

wslwinreg.nullapi.EnumKey(key, index)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.EnumValue

wslwinreg.nullapi.EnumValue(key, index)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.ExpandEnvironmentStrings

wslwinreg.nullapi.ExpandEnvironmentStrings(str)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.FlushKey

wslwinreg.nullapi.FlushKey(key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.LoadKey

wslwinreg.nullapi.LoadKey(key, sub_key, file_name)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.OpenKey

wslwinreg.nullapi.OpenKey(key, sub_key, reserved=0, access=KEY_READ)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.OpenKeyEx

wslwinreg.nullapi.OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.QueryInfoKey

wslwinreg.nullapi.QueryInfoKey(key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.QueryValue

wslwinreg.nullapi.QueryValue(key, sub_key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.QueryValueEx

wslwinreg.nullapi.QueryValueEx(key, value_name)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.SaveKey

wslwinreg.nullapi.SaveKey(key, file_name)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.SetValue

wslwinreg.nullapi.SetValue(key, sub_key, type, value)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.SetValueEx

wslwinreg.nullapi.SetValueEx(key, value_name, reserved, type, value)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.DisableReflectionKey

wslwinreg.nullapi.DisableReflectionKey(key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.EnableReflectionKey

wslwinreg.nullapi.EnableReflectionKey(key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.QueryReflectionKey

wslwinreg.nullapi.QueryReflectionKey(key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.nullapi.convert_to_windows_path

wslwinreg.nullapi.convert_to_windows_path(path_name)

Convert pathname to Windows.

This is the null function, it returns the path_name unchanged.

Return

path_name as is.

Parameters

path_name – Absolute Windows pathname

wslwinreg.nullapi.convert_from_windows_path

wslwinreg.nullapi.convert_from_windows_path(path_name)

Convert pathname from Windows.

This is the null function, it returns the path_name unchanged.

Return

path_name as is.

Parameters

path_name – Absolute Windows pathname

wslwinreg.nullapi.get_file_info

wslwinreg.nullapi.get_file_info(path_name, string_name)

Not implemented.

Return

None

Parameters
  • path_name – Name of the windows file.

  • string_name – Name of the data chunk to retrieve

Cygwin / MSYS2 implementation

On Cygwin and MSYS2 platforms, the CDLL exposes the Windows API directly so these python functions mimic the C code from Python for Windows and calls the Windows API to perform the low level work.

wslwinreg.cygwinapi.CloseKey

wslwinreg.cygwinapi.CloseKey(hkey)

Closes a previously opened registry key.

The hkey argument specifies a previously opened key.

Note

If hkey is not closed using this method (or via hkey.Close()), it is closed when the hkey object is destroyed by Python.

Parameters

hkeyPyHKEY object or None.

wslwinreg.cygwinapi.ConnectRegistry

wslwinreg.cygwinapi.ConnectRegistry(computer_name, key)

Establishes a connection to a predefined registry handle.

Establishes a connection to a predefined registry handle on another computer, and returns a handle object.

Exception

WindowsError or FileNotFileError

Parameters
  • computer_name – Is the name of the remote computer, of the form r"\\computername". If None, the local computer is used.

  • key – Is the predefined handle to connect to.

Returns

PyHKEY object

wslwinreg.cygwinapi.CreateKey

wslwinreg.cygwinapi.CreateKey(key, sub_key)

Creates or opens the specified key.

If key is one of the predefined keys, sub_key may be None. In that case, the handle returned is the same key handle passed in to the function.

If the key already exists, this function opens the existing key.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or one of the predefined HKEY_* constants.

  • sub_key – Is a string that names the key this method opens or creates.

Returns

Handle of the opened key.

wslwinreg.cygwinapi.CreateKeyEx

wslwinreg.cygwinapi.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)

Creates or opens the specified key.

If key is one of the predefined keys, sub_key may be None. In that case, the handle returned is the same key handle passed in to the function.

If the key already exists, this function opens the existing key.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or one of the predefined HKEY_* constants

  • sub_key – Is a string that names the key this method opens or creates.

  • reserved – Is a reserved integer, and must be zero. The default is zero.

  • access – Os an integer that specifies an access mask that describes the desired security access for the key. Default is common.KEY_WRITE.

Returns

Handle of the opened key.

wslwinreg.cygwinapi.DeleteKey

wslwinreg.cygwinapi.DeleteKey(key, sub_key)

Deletes the specified key.

If the method succeeds, the entire key, including all of its values, is removed.

Exception

WindowsError

Note

This method can not delete keys with subkeys.

Parameters
  • key – Is an already open key, or one of the predefined HKEY_* constants.

  • sub_key – Is a string that must be a subkey of the key identified by the key parameter. This value must not be None, and the key may not have subkeys.

wslwinreg.cygwinapi.DeleteKeyEx

wslwinreg.cygwinapi.DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)

Deletes the specified key.

If the method succeeds, the entire key, including all of its values, is removed.

Exception

WindowsError

Note

This method can not delete keys with subkeys.

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Os a string that must be a subkey of the key identified by the key parameter. This value must not be None, and the key may not have subkeys.

  • access – Is an integer that specifies an access mask that describes the desired security access for the key. Default is common.KEY_WOW64_64KEY.

  • reserved – Is a reserved integer, and must be zero. The default is zero.

wslwinreg.cygwinapi.DeleteValue

wslwinreg.cygwinapi.DeleteValue(key, value)

Removes a named value from a registry key.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • value – Is a string that identifies the value to remove.

wslwinreg.cygwinapi.EnumKey

wslwinreg.cygwinapi.EnumKey(key, index)

Enumerates subkeys of an open registry key, returning a string.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly until an OSError exception is raised, indicating no more values are available.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • index – Is an integer that identifies the index of the key to retrieve.

wslwinreg.cygwinapi.EnumValue

wslwinreg.cygwinapi.EnumValue(key, index)

Enumerates values of an open registry key, returning a tuple.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly, until an OSError exception is raised, indicating no more values.

Index

Meaning

0

A string that identifies the value.

1

An object that holds the value data, and whose type depends on the underlying registry type

2

An integer that identifies the type of the value data

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • index – Is an integer that identifies the index of the value to retrieve.

Returns

A tuple of 3 items.

wslwinreg.cygwinapi.ExpandEnvironmentStrings

wslwinreg.cygwinapi.ExpandEnvironmentStrings(str)

Expands environment variables.

Expands environment variable placeholders NAME% in strings like REG_EXPAND_SZ.

Exception

WindowsError

Parameters

str – String to expand.

Returns

Expanded string

wslwinreg.cygwinapi.FlushKey

wslwinreg.cygwinapi.FlushKey(key)

Writes all the attributes of a key to the registry.

It is not necessary to call FlushKey() to change a key. Registry changes are flushed to disk by the registry using its lazy flusher. Registry changes are also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey() method returns only when all the data has been written to the registry. An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.

Exception

WindowsError

Note

If you don’t know whether a FlushKey() call is required, it probably isn’t.

Parameters

key – Is an already open key, or any one of the predefined HKEY_* constants.

wslwinreg.cygwinapi.LoadKey

wslwinreg.cygwinapi.LoadKey(key, sub_key, file_name)

Creates a subkey under the specified key.

Creates a subkey under the specified key and stores registration information from a specified file into that subkey.

This file must have been created with the SaveKey() function. Under the file allocation table (FAT) file system, the filename may not have an extension.

A call to LoadKey() fails if the calling process does not have the SE_RESTORE_PRIVILEGE privilege.

If key is a handle returned by ConnectRegistry(), then the path specified in fileName is relative to the remote computer.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is a handle returned by ConnectRegistry() or one of the constants common.HKEY_USERS or common.HKEY_LOCAL_MACHINE.

  • sub_key – Is a string that identifies the sub_key to load

  • file_name – Is the name of the file to load registry data from.

wslwinreg.cygwinapi.OpenKey

wslwinreg.cygwinapi.OpenKey(key, sub_key, reserved=0, access=KEY_READ)

Opens the specified key, returning a handle object.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that identifies the sub_key to open

  • reserved – Is a reserved integer, and must be zero. Default is zero.

  • access – Is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ.

Returns

A new handle to the specified key.

wslwinreg.cygwinapi.OpenKeyEx

wslwinreg.cygwinapi.OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)

Opens the specified key, returning a handle object.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that identifies the sub_key to open

  • reserved – Is a reserved integer, and must be zero. Default is zero.

  • access – Is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ.

Returns

A new handle to the specified key.

wslwinreg.cygwinapi.QueryInfoKey

wslwinreg.cygwinapi.QueryInfoKey(key)

Returns information about a key, as a tuple.

Index

Meaning

0

An integer giving the number of sub keys this key has.

1

An integer giving the number of values this key has.

2

An integer giving when the key was last modified (if available) as 100’s of nanoseconds since Jan 1, 1601.

Exception

WindowsError or FileNotFileError

Parameters

key – Is an already open key, or any one of the predefined HKEY_* constants.

Returns

A tuple of 3 items.

wslwinreg.cygwinapi.QueryValue

wslwinreg.cygwinapi.QueryValue(key, sub_key)

Retrieves the unnamed value for a key, as a string.

Values in the registry have name, type, and data components. This method retrieves the data for a key’s first value that has a NULL name. But the underlying API call doesn’t return the type, so always use QueryValueEx() if possible.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key.

wslwinreg.cygwinapi.QueryValueEx

wslwinreg.cygwinapi.QueryValueEx(key, value_name)

Retrieves the type and data for a specified value name.

Retrieves the type and data for a specified value name associated with an open registry key.

Index

Meaning

0

The value of the registry item.

1

An integer giving the registry type for this value.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • value_name – Is a string indicating the value to query.

Returns

A tuple of 2 items.

wslwinreg.cygwinapi.SaveKey

wslwinreg.cygwinapi.SaveKey(key, file_name)

Saves the specified key, and all its subkeys to the specified file.

If key represents a key on a remote computer, the path described by file_name is relative to the remote computer. The caller of this method must possess the SeBackupPrivilege security privilege.

This function passes NULL for security_attributes to the API.

Exception

WindowsError or FileNotFileError

Note

Privileges are different than permissions.

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • file_name – Is the name of the file to save registry data to. This file cannot already exist. If this filename includes an extension, it cannot be used on file allocation table (FAT) file systems by the LoadKey() method.

wslwinreg.cygwinapi.SetValue

wslwinreg.cygwinapi.SetValue(key, sub_key, type, value)

Associates a value with a specified key.

If the key specified by the sub_key parameter does not exist, the SetValue function creates it.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

The key identified by the key parameter must have been opened with common.KEY_SET_VALUE access.

Exception

TypeError, WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that names the subkey with which the value is associated.

  • type – is an integer that specifies the type of the data. Currently this must be REG_SZ, meaning only strings are supported. Use the SetValueEx() function for support for other data types.

  • value – Is a string that specifies the new value.

wslwinreg.cygwinapi.SetValueEx

wslwinreg.cygwinapi.SetValueEx(key, value_name, reserved, type, value)

Stores data in the value field of an open registry key.

This method can also set additional value and type information for the specified key. The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

To open the key, use the CreateKeyEx() or OpenKeyEx() methods.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • value_name – Is a string that names the subkey with which the value is associated.

  • reserved – can be anything – zero is always passed to the API.

  • type – Is an integer that specifies the type of the data.

  • value – Is a string that specifies the new value.

wslwinreg.cygwinapi.DisableReflectionKey

wslwinreg.cygwinapi.DisableReflectionKey(key)

Disables registry reflection.

Disables registry reflection for 32-bit processes running on a 64-bit operating system.

If the key is not on the reflection list, the function succeeds but has no effect. Disabling reflection for a key does not affect reflection of any subkeys.

Exception

WindowsError

Parameters

key – Is an already open key, or one of the predefined HKEY_* constants.

wslwinreg.cygwinapi.EnableReflectionKey

wslwinreg.cygwinapi.EnableReflectionKey(key)

Restores registry reflection for the specified disabled key.

Restoring reflection for a key does not affect reflection of any subkeys.

Exception

WindowsError

Parameters

key – Is an already open key, or one of the predefined HKEY_* constants.

wslwinreg.cygwinapi.QueryReflectionKey

wslwinreg.cygwinapi.QueryReflectionKey(key)

Determines the reflection state for the specified key.

Parameters

key – Is an already open key, or any one of the predefined HKEY_* constants.

Returns

True if reflection is disabled.

wslwinreg.cygwinapi.convert_to_windows_path

wslwinreg.cygwinapi.convert_to_windows_path(path_name)

Convert a MSYS/Cygwin path to windows if needed.

If the path is already Windows format, it will be returned unchanged.

Return

Pathname converted to Windows.

Parameters

path_name – Windows or Linux pathname

wslwinreg.cygwinapi.convert_from_windows_path

wslwinreg.cygwinapi.convert_from_windows_path(path_name)

Convert an absolute Windows path to Cygwin/MSYS2.

If the path is already Cygwin/MSYS2 format, it will be returned unchanged.

Return

Pathname converted to Linux.

Parameters

path_name – Absolute Windows pathname

wslwinreg.cygwinapi.get_file_info

wslwinreg.cygwinapi.get_file_info(path_name, string_name)

Extract information from a windows exe file version resource.

Given a windows exe file, extract the “StringFileInfo” resource and parse out the data chunk named by string_name.

Full list of resource names: https://docs.microsoft.com/en-us/windows/desktop/menurc/stringfileinfo-block

Examples

file_version = get_file_info("devenv.exe", "FileVersion")
product_version =  get_file_info("devenv.exe", "ProductVersion")

Return

None if no record found or an error, or a valid string

Parameters
  • path_name – Name of the windows file.

  • string_name – Name of the data chunk to retrieve

Windows Subsystem for Linux implementation

On Windows Subsystem for Windows, the calls are sent to a server that will issue the calls directly in the Windows host which performs the actual the low level work.

wslwinreg.wslapi.CloseKey

wslwinreg.wslapi.CloseKey(hkey)

Closes a previously opened registry key.

The hkey argument specifies a previously opened key.

Note

If hkey is not closed using this method (or via hkey.Close()), it is closed when the hkey object is destroyed by Python.

Parameters

hkey – PyHKEY object or None.

wslwinreg.wslapi.ConnectRegistry

wslwinreg.wslapi.ConnectRegistry(computer_name, key)

Establishes a connection to a predefined registry handle.

Establishes a connection to a predefined registry handle on another computer, and returns a handle object.

Exception

WindowsError or FileNotFileError

Parameters
  • computer_name – Is the name of the remote computer, of the form r"\\computername". If None, the local computer is used.

  • key – Is the predefined handle to connect to.

Returns

PyHKEY object

wslwinreg.wslapi.CreateKey

wslwinreg.wslapi.CreateKey(key, sub_key)

Creates or opens the specified key.

If key is one of the predefined keys, sub_key may be None. In that case, the handle returned is the same key handle passed in to the function.

If the key already exists, this function opens the existing key.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or one of the predefined HKEY_* constants.

  • sub_key – Is a string that names the key this method opens or creates.

Returns

Handle of the opened key.

wslwinreg.wslapi.CreateKeyEx

wslwinreg.wslapi.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)

Creates or opens the specified key.

If key is one of the predefined keys, sub_key may be None. In that case, the handle returned is the same key handle passed in to the function.

If the key already exists, this function opens the existing key.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or one of the predefined HKEY_* constants

  • sub_key – Is a string that names the key this method opens or creates.

  • reserved – Is a reserved integer, and must be zero. The default is zero.

  • access – Os an integer that specifies an access mask that describes the desired security access for the key. Default is common.KEY_WRITE.

Returns

Handle of the opened key.

wslwinreg.wslapi.DeleteKey

wslwinreg.wslapi.DeleteKey(key, sub_key)

Not implemented.

Exception

NotImplementedError is always thrown.

wslwinreg.wslapi.DeleteKeyEx

wslwinreg.wslapi.DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)

Deletes the specified key.

If the method succeeds, the entire key, including all of its values, is removed.

Exception

WindowsError

Note

This method can not delete keys with subkeys.

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Os a string that must be a subkey of the key identified by the key parameter. This value must not be None, and the key may not have subkeys.

  • access – Is an integer that specifies an access mask that describes the desired security access for the key. Default is common.KEY_WOW64_64KEY.

  • reserved – Is a reserved integer, and must be zero. The default is zero.

wslwinreg.wslapi.DeleteValue

wslwinreg.wslapi.DeleteValue(key, value)

Removes a named value from a registry key.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • value – Is a string that identifies the value to remove.

wslwinreg.wslapi.EnumKey

wslwinreg.wslapi.EnumKey(key, index)

Enumerates subkeys of an open registry key, returning a string.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly until an OSError exception is raised, indicating no more values are available.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • index – Is an integer that identifies the index of the key to retrieve.

wslwinreg.wslapi.EnumValue

wslwinreg.wslapi.EnumValue(key, index)

Enumerates values of an open registry key, returning a tuple.

The function retrieves the name of one subkey each time it is called. It is typically called repeatedly, until an OSError exception is raised, indicating no more values.

Index

Meaning

0

A string that identifies the value.

1

An object that holds the value data, and whose type depends on the underlying registry type

2

An integer that identifies the type of the value data

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • index – Is an integer that identifies the index of the value to retrieve.

Returns

A tuple of 3 items.

wslwinreg.wslapi.ExpandEnvironmentStrings

wslwinreg.wslapi.ExpandEnvironmentStrings(str)

Expands environment variables.

Expands environment variable placeholders NAME% in strings like REG_EXPAND_SZ.

Exception

WindowsError

Parameters

str – String to expand.

Returns

Expanded string

wslwinreg.wslapi.FlushKey

wslwinreg.wslapi.FlushKey(key)

Writes all the attributes of a key to the registry.

It is not necessary to call FlushKey() to change a key. Registry changes are flushed to disk by the registry using its lazy flusher. Registry changes are also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey() method returns only when all the data has been written to the registry. An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.

Exception

WindowsError

Note

If you don’t know whether a FlushKey() call is required, it probably isn’t.

Parameters

key – Is an already open key, or any one of the predefined HKEY_* constants.

wslwinreg.wslapi.LoadKey

wslwinreg.wslapi.LoadKey(key, sub_key, file_name)

Creates a subkey under the specified key.

Creates a subkey under the specified key and stores registration information from a specified file into that subkey.

This file must have been created with the SaveKey() function. Under the file allocation table (FAT) file system, the filename may not have an extension.

A call to LoadKey() fails if the calling process does not have the SE_RESTORE_PRIVILEGE privilege.

If key is a handle returned by ConnectRegistry(), then the path specified in fileName is relative to the remote computer.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is a handle returned by ConnectRegistry() or one of the constants common.HKEY_USERS or common.HKEY_LOCAL_MACHINE.

  • sub_key – Is a string that identifies the sub_key to load

  • file_name – Is the name of the file to load registry data from.

wslwinreg.wslapi.OpenKey

wslwinreg.wslapi.OpenKey(key, sub_key, reserved=0, access=KEY_READ)

Opens the specified key, returning a handle object.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that identifies the sub_key to open

  • reserved – Is a reserved integer, and must be zero. Default is zero.

  • access – Is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ.

Returns

A new handle to the specified key.

wslwinreg.wslapi.OpenKeyEx

wslwinreg.wslapi.OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)

Opens the specified key, returning a handle object.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that identifies the sub_key to open

  • reserved – Is a reserved integer, and must be zero. Default is zero.

  • access – Is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ.

Returns

A new handle to the specified key.

wslwinreg.wslapi.QueryInfoKey

wslwinreg.wslapi.QueryInfoKey(key)

Returns information about a key, as a tuple.

Index

Meaning

0

An integer giving the number of sub keys this key has.

1

An integer giving the number of values this key has.

2

An integer giving when the key was last modified (if available) as 100’s of nanoseconds since Jan 1, 1601.

Exception

WindowsError or FileNotFileError

Parameters

key – Is an already open key, or any one of the predefined HKEY_* constants.

Returns

A tuple of 3 items.

wslwinreg.wslapi.QueryValue

wslwinreg.wslapi.QueryValue(key, sub_key)

Retrieves the unnamed value for a key, as a string.

Values in the registry have name, type, and data components. This method retrieves the data for a key’s first value that has a NULL name. But the underlying API call doesn’t return the type, so always use QueryValueEx() if possible.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key.

wslwinreg.wslapi.QueryValueEx

wslwinreg.wslapi.QueryValueEx(key, value_name)

Retrieves the type and data for a specified value name.

Retrieves the type and data for a specified value name associated with an open registry key.

Index

Meaning

0

The value of the registry item.

1

An integer giving the registry type for this value.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • value_name – Is a string indicating the value to query.

Returns

A tuple of 2 items.

wslwinreg.wslapi.SaveKey

wslwinreg.wslapi.SaveKey(key, file_name)

Saves the specified key, and all its subkeys to the specified file.

If key represents a key on a remote computer, the path described by file_name is relative to the remote computer. The caller of this method must possess the SeBackupPrivilege security privilege.

This function passes NULL for security_attributes to the API.

Exception

WindowsError or FileNotFileError

Note

Privileges are different than permissions.

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • file_name – Is the name of the file to save registry data to. This file cannot already exist. If this filename includes an extension, it cannot be used on file allocation table (FAT) file systems by the LoadKey() method.

wslwinreg.wslapi.SetValue

wslwinreg.wslapi.SetValue(key, sub_key, type, value)

Associates a value with a specified key.

If the key specified by the sub_key parameter does not exist, the SetValue function creates it.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

The key identified by the key parameter must have been opened with common.KEY_SET_VALUE access.

Exception

TypeError, WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • sub_key – Is a string that names the subkey with which the value is associated.

  • type – is an integer that specifies the type of the data. Currently this must be REG_SZ, meaning only strings are supported. Use the SetValueEx() function for support for other data types.

  • value – Is a string that specifies the new value.

wslwinreg.wslapi.SetValueEx

wslwinreg.wslapi.SetValueEx(key, value_name, reserved, type, value)

Stores data in the value field of an open registry key.

This method can also set additional value and type information for the specified key. The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

To open the key, use the CreateKeyEx() or OpenKeyEx() methods.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

Exception

WindowsError or FileNotFileError

Parameters
  • key – Is an already open key, or any one of the predefined HKEY_* constants.

  • value_name – Is a string that names the subkey with which the value is associated.

  • reserved – can be anything – zero is always passed to the API.

  • type – Is an integer that specifies the type of the data.

  • value – Is a string that specifies the new value.

wslwinreg.wslapi.DisableReflectionKey

wslwinreg.wslapi.DisableReflectionKey(key)

Disables registry reflection.

Disables registry reflection for 32-bit processes running on a 64-bit operating system.

If the key is not on the reflection list, the function succeeds but has no effect. Disabling reflection for a key does not affect reflection of any subkeys.

Exception

WindowsError

Parameters

key – Is an already open key, or one of the predefined HKEY_* constants.

wslwinreg.wslapi.EnableReflectionKey

wslwinreg.wslapi.EnableReflectionKey(key)

Restores registry reflection for the specified disabled key.

Restoring reflection for a key does not affect reflection of any subkeys.

Exception

WindowsError

Parameters

key – Is an already open key, or one of the predefined HKEY_* constants.

wslwinreg.wslapi.QueryReflectionKey

wslwinreg.wslapi.QueryReflectionKey(key)

Determines the reflection state for the specified key.

Parameters

key – Is an already open key, or any one of the predefined HKEY_* constants.

Returns

True if reflection is disabled.

wslwinreg.wslapi.convert_to_windows_path

wslwinreg.wslapi.convert_to_windows_path(path_name)

Convert a WSL path to windows if needed.

If the path is already Windows format, it will be returned unchanged.

Return

Pathname converted to Windows.

Parameters

path_name – Windows or Linux pathname

wslwinreg.wslapi.convert_from_windows_path

wslwinreg.wslapi.convert_from_windows_path(path_name)

Convert an absolute Windows path to WSL.

If the path is already Linux format, it will be returned unchanged.

Return

Pathname converted to Linux.

Parameters

path_name – Absolute Windows pathname

wslwinreg.wslapi.get_file_info

wslwinreg.wslapi.get_file_info(path_name, string_name)

Extract information from a windows exe file version resource.

Given a windows exe file, extract the “StringFileInfo” resource and parse out the data chunk named by string_name.

Full list of resource names: https://docs.microsoft.com/en-us/windows/desktop/menurc/stringfileinfo-block

Examples

file_version = get_file_info("devenv.exe", "FileVersion")
product_version =  get_file_info("devenv.exe", "ProductVersion")

Return

None if no record found or an error, or a valid string

Parameters
  • path_name – Name of the windows file.

  • string_name – Name of the data chunk to retrieve