TReg32 - Windows Registry

Fuente: source/classes/reg32.prg

TReg32 wraps the Windows Registry API (RegOpenKeyEx, RegQueryValueEx, RegSetValueEx, RegCreateKeyEx, etc.) into an object-oriented interface. It supports reading and writing string, numeric (DWORD), and binary values, enumerating and deleting subkeys, and automatic type detection. Suitable for storing application settings in HKCU or HKLM.

DATA Members

DATATypeDescription
cRegKeyCharacterFull registry key path (e.g. "Software\MyApp")
nKeyNumericRoot key handle (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, etc.)
nHandleNumericOpened key handle from RegOpenKeyEx / RegCreateKeyEx
nErrorNumericLast Windows API error code (0 = success)

Methods

MethodDescription
New( nKey, cKey )Open an existing registry key for reading
Create( nKey, cKey )Create or open a registry key with write access
Get( cSubKey, uDef )Read a value by name. Returns uDef if not found. Auto-detects string, DWORD, or binary
GetBinary( cSubKey )Read a binary (REG_BINARY) value and return it as a character string
Set( cSubKey, uVal, nType )Write a value. nType: 1=REG_SZ (string), 3=REG_BINARY, 4=REG_DWORD (numeric). Auto-detected if omitted
Delete( cSubKey )Delete a value or subkey (if empty)
Close()Close the registry key handle

Example: HKCU Settings

#include "FiveWin.ch"

function Main()

   local oReg := TReg32():New( HKEY_CURRENT_USER, "Software\MyApp" )

   if oReg:nHandle != 0
      // Read values
      local cName := oReg:Get( "UserName", "Default" )
      local nPos  := oReg:Get( "WindowPos", 100 )
      local dLast := oReg:Get( "LastRun", Date() )

      ? "User:", cName
      ? "Position:", nPos

      // Write updated values
      oReg:Set( "LastRun", Date() )
      oReg:Set( "WindowPos", 200, 4 )   // REG_DWORD

      oReg:Close()
   else
      // Key does not exist -- create it
      oReg := TReg32():Create( HKEY_CURRENT_USER, "Software\MyApp" )
      if oReg:nError == 0
         oReg:Set( "UserName", "John" )
         oReg:Set( "WindowPos", 200 )
         oReg:Set( "LastRun", Date() )
         oReg:Close()
      endif
   endif

return nil

Ver También