Skip to content

VBMAN.Regedit - Registry Operation Object

Overview

VBMAN.Regedit provides Windows registry read/write functionality, implemented by calling the reg command line tool.

Core Features

  • Query Registry Items: Support querying keys under specified path
  • Add/Modify Key Values: Support creating or updating registry items
  • Delete Key Values: Support deleting specified registry items
  • Type Safety: Use TypeRegData type to encapsulate return values

Data Structure

TypeRegData

Registry item data structure

vb
Public Type TypeRegData
    HasName As Boolean  ' Whether has name
    RegName As String   ' Key name
    RegType As String   ' Type (REG_SZ, REG_DWORD, etc.)
    RegValue As String  ' Key value
End Type

Properties

PropertyTypeDescription
LastErrorStringLast error message

Methods

FindItem

Query registry items, return array

vb
Public Function FindItem(ByVal Path As String, Optional ByVal Name As String) As TypeRegData()

Parameters:

  • Path - Registry path, e.g., "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
  • Name - Key name (optional, if empty returns all keys)

Returns: TypeRegData array

Example:

vb
Dim items() As TypeRegData
items = VBMAN.Regedit.FindItem("HKCU\Software\Microsoft\Windows\CurrentVersion\Run")

Dim i As Long
For i = LBound(items) To UBound(items)
    If items(i).HasName Then
        Debug.Print items(i).RegName & " = " & items(i).RegValue
    End If
Next i

FindFirst

Query first matching registry value

vb
Public Function FindFirst(ByVal Path As String, Optional ByVal Name As String) As Variant

Returns: Key value (string), returns empty string if not found

Example:

vb
Dim value As Variant
value = VBMAN.Regedit.FindFirst("HKCU\Software\Microsoft\Windows\CurrentVersion\Run", "Notepad")
If value <> vbNullString Then
    Debug.Print "Found value: " & value
End If

FindLast

Query last matching registry value

vb
Public Function FindLast(ByVal Path As String, Optional ByVal Name As String) As Variant

SaveItem

Add or modify registry item

vb
Public Function SaveItem(ByVal Path As String, ByVal Name As String, ByVal Data As Variant, Optional StartArgs As Variant, Optional IsOverWrite As Boolean = True) As Boolean

Parameters:

  • Path - Registry path
  • Name - Key name
  • Data - Key value
  • StartArgs - Startup parameters (optional)
  • IsOverWrite - Whether to overwrite (default True)

Returns: True=success, False=failure

Example:

vb
' Add startup item
Dim success As Boolean
success = VBMAN.Regedit.SaveItem( _
    "HKCU\Software\Microsoft\Windows\CurrentVersion\Run", _
    "MyApp", _
    "C:\MyApp\app.exe" _
)

If success Then
    MsgBox "Add successful"
Else
    MsgBox "Add failed: " & VBMAN.Regedit.LastError
End If

DeleteItem

Delete registry item

vb
Public Function DeleteItem(ByVal Path As String, ByVal Name As String) As Boolean

Example:

vb
' Delete startup item
Dim success As Boolean
success = VBMAN.Regedit.DeleteItem("HKCU\Software\Microsoft\Windows\CurrentVersion\Run", "MyApp")

Comprehensive Examples

Example 1: Manage Startup Items

vb
Private Sub ManageStartup()
    Const RUN_PATH As String = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
    
    ' Add startup item
    VBMAN.Regedit.SaveItem RUN_PATH, "MyApplication", App.Path & "\" & App.EXEName & ".exe"
    
    ' Query all startup items
    Dim items() As TypeRegData
    items = VBMAN.Regedit.FindItem(RUN_PATH)
    
    Dim i As Long
    For i = LBound(items) To UBound(items)
        If items(i).HasName Then
            List1.AddItem items(i).RegName & " = " & items(i).RegValue
        End If
    Next i
    
    ' Delete startup item
    ' VBMAN.Regedit.DeleteItem RUN_PATH, "MyApplication"
End Sub

Example 2: Read System Information

vb
Private Sub ReadSystemInfo()
    ' Read Windows version
    Dim version As Variant
    version = VBMAN.Regedit.FindFirst("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "DisplayVersion")
    Debug.Print "Windows version: " & version
    
    ' Read computer name
    Dim computerName As Variant
    computerName = VBMAN.Regedit.FindFirst("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName")
    Debug.Print "Computer name: " & computerName
End Sub

Best Practices

  1. Error Checking: Check LastError or use return value to determine result after operation
  2. Permission Note: Modifying HKLM requires administrator privileges
  3. Backup Registry: Recommend backing up before large-scale modifications
  4. Use Constants: Define commonly used paths as constants

Notes

  • Registry operations need to be cautious, incorrect modifications may cause system issues
  • Some operations may require administrator privileges
  • Executed through reg command, depends on system command line tools

VB6 and LOGO copyright of Microsoft Corporation