# WinReg Package
{: .no_toc }

**Author:** forliny. **Version:** 1.0.1.1. **On GitHub:** [forliny/TbWinReg](https://github.com/forliny/TbWinReg)

The **WinReg** contributed package provides a Windows Registry helper class that reads, writes, deletes, and enumerates registry keys and values through the Win32 `advapi32` API. The package exposes a single **RegCls** class wrapping the most common registry operations into a convenient interface. It declares the [**RegKeyspace**](/tB/Packages/Contributed/WinReg/RegKeyspace.md) and [**enumRegSzType**](/tB/Packages/Contributed/WinReg/enumRegSzType.md) enumerations and its own copies of the `advapi32` declarations it uses, and takes the rest, among them `RegOpenKeyEx`, `RegSetValueEx` and the types **BOOL** and **REGTYPES**, from the **WinDevLib** package. A comment in the source recommends the declarations of **WinDevLib** over the package's own.

The root keys, such as `HKEY_CURRENT_USER`, are members of **RegKeyspace**, so a project names them with **WinReg** alone. **WinReg** references the **WinDevLib** package, but a project does not see the names a package's own references declare. A project that names `BOOL` or `REGTYPES`, or calls `RegOpenKeyEx` or `RegCloseKey` and uses the `KEY_*` constants, as some examples here do, references **WinDevLib** as well. One that declares the **Dictionary** that [**EnumValues**](/tB/Packages/Contributed/WinReg/RegCls/EnumValues.md) returns references the Microsoft Scripting Runtime.

> [!NOTE] 1.0.1.1, BETA 1045
> The package does not compile, so a project that references it does not build. **RegCls** declares `RegCreateKeyEx` and `RegEnumKeyEx` with a `ByRef` structure parameter, *lpSecurityAttributes* (`SECURITY_ATTRIBUTES`) and *lpftLastWriteTime* (`FILETIME`), and passes `VarPtr(0&)` for it in [**WriteString**](/tB/Packages/Contributed/WinReg/RegCls/WriteString.md), [**WriteDWORD**](/tB/Packages/Contributed/WinReg/RegCls/WriteDWORD.md), [**CreateKey**](/tB/Packages/Contributed/WinReg/RegCls/CreateKey.md) and [**DeleteKeyEx**](/tB/Packages/Contributed/WinReg/RegCls/DeleteKeyEx.md). A pointer passed for a structure has to be passed `ByVal` (error TB5001). `VarPtr(0&)` is also the address of a temporary **Long**, not a null pointer; `ByVal vbNullPtr` passes a null pointer. The behaviour described on these pages is that of the package with those four calls passed `ByVal`.

> [!WARNING] 1.0.1.1
> Three methods give wrong results. [**ReadString**](/tB/Packages/Contributed/WinReg/RegCls/ReadString.md) returns the text followed by NUL characters. [**GetRegistryValueType**](/tB/Packages/Contributed/WinReg/RegCls/GetRegistryValueType.md) returns **REG_NONE** for every value that holds data. The first overload of [**ConvertValueToString**](/tB/Packages/Contributed/WinReg/RegCls/ConvertValueToString.md) returns an empty string for the types it supports.

## Classes

- [RegCls](/tB/Packages/Contributed/WinReg/RegCls/index.md) -- a Windows Registry helper class that reads, writes, deletes, and enumerates registry keys and values through the Win32 `advapi32` API
  - [ConvertValueToString](/tB/Packages/Contributed/WinReg/RegCls/ConvertValueToString.md) -- converts a registry value (from an open key handle or a byte array) to its String representation
  - [CreateKey](/tB/Packages/Contributed/WinReg/RegCls/CreateKey.md) -- creates a registry key, including any intermediate keys in the path that do not already exist
  - [DeleteKey](/tB/Packages/Contributed/WinReg/RegCls/DeleteKey.md) -- deletes a registry key that has no subkeys
  - [DeleteKeyEx](/tB/Packages/Contributed/WinReg/RegCls/DeleteKeyEx.md) -- recursively deletes a registry key and all of its subkeys and values
  - [DeleteValue](/tB/Packages/Contributed/WinReg/RegCls/DeleteValue.md) -- deletes a named value from a registry key
  - [EnumValues](/tB/Packages/Contributed/WinReg/RegCls/EnumValues.md) -- returns a Dictionary containing the names and string representations of all values under a registry key
  - [GetRegistryValueType](/tB/Packages/Contributed/WinReg/RegCls/GetRegistryValueType.md) -- returns the data type of a named registry value without reading its data
  - [KeyExists](/tB/Packages/Contributed/WinReg/RegCls/KeyExists.md) -- returns True if the specified registry key exists, False otherwise
  - [ReadDWORD](/tB/Packages/Contributed/WinReg/RegCls/ReadDWORD.md) -- reads a REG_DWORD value from the Windows registry and returns it as a Long
  - [ReadString](/tB/Packages/Contributed/WinReg/RegCls/ReadString.md) -- reads a REG_SZ string value from the Windows registry and returns it as a String
  - [ValueExists](/tB/Packages/Contributed/WinReg/RegCls/ValueExists.md) -- returns True if a named value exists under the specified registry key
  - [WriteDWORD](/tB/Packages/Contributed/WinReg/RegCls/WriteDWORD.md) -- writes a 32-bit integer value (REG_DWORD) to a registry key, creating the key if it does not already exist
  - [WriteString](/tB/Packages/Contributed/WinReg/RegCls/WriteString.md) -- writes a string value to a registry key, creating the key if it does not exist

## Enumerations

- [enumRegSzType](/tB/Packages/Contributed/WinReg/enumRegSzType.md) -- the codes of the registry value types
- [RegKeyspace](/tB/Packages/Contributed/WinReg/RegKeyspace.md) -- the predefined root keys of the registry
