Search code examples
vb6registry

VB6: Writing to registry


I have to edit an old legacy VB6 application so that it can edit the registry to write the following:

reg add "HKCU\Software\Microsoft\Print\UnifiedPrintDialog" /v "PreferLegacyPrintDialog" /d 1 /t REG_DWORD /f

How can I emulate the above command in VB6?

I read a few posts using the registry = CreateObject("WScript.shell") methodology but it doesn't seem clear to me and I really don't want to mess around with the registry without knowing what I'm doing. Otherwise, could I just run the command through a ShellExecute or something similar?

Any assistance would be appreciated. Thanks!


Solution

  • You can use the Windows API to accomplish what you need. Here's some general purpose code to read and write to the Registry:

    Option Explicit
    
    Private Sub Read_Click()
       Text1.Text = ReadRegistry(HKEY_CURRENT_USER, "Software\Microsoft\Print\UnifiedPrintDialog", "PreferLegacyPrintDialog", ValDWord, "1")
    End Sub
    
    Private Sub Write_Click()
       WriteRegistry HKEY_CURRENT_USER, "Software\Microsoft\Print\UnifiedPrintDialog", "PreferLegacyPrintDialog", ValDWord, Text1.Text
    End Sub
    

    In a Module place the following code:

    Option Explicit
    
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    
    Public Const REG_SZ = 1
    Public Const REG_DWORD = 4
    
    Public Enum InTypes
       ValNull = 0
       ValString = 1
       ValXString = 2
       ValBinary = 3
       ValDWord = 4
       ValLink = 6
       ValMultiString = 7
       ValResList = 8
    End Enum
    
    Private Const ERROR_SUCCESS = 0&
    
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    
    Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, Optional Default As Variant) As Variant
       If ValType = ValString Then
          ReadRegistry = ReadString(Group, Section, Key)
          If ReadRegistry = "" Then ReadRegistry = Default
       ElseIf ValType = ValDWord Then
          ReadRegistry = ReadDword(Group, Section, Key)
          If ReadRegistry = 0 Then ReadRegistry = Default
       End If
    End Function
    
    Public Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, ByVal Value As Variant)
       If ValType = ValString Then
          WriteString Group, Section, Key, CStr(Value)
       ElseIf ValType = ValDWord Then
          WriteDword Group, Section, Key, CLng(Value)
       End If
    End Sub
    
    Private Function ReadString(hKey As Long, strPath As String, strValue As String) As String
       Dim keyhand As Long
       Dim lResult As Long
       Dim strBuf As String
       Dim lDataBufSize As Long
       Dim intZeroPos As Integer
       Dim lValueType As Long
       Dim r As Long
       
       r = RegOpenKey(hKey, strPath, keyhand)
       lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
       strBuf = String(lDataBufSize, " ")
       lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
        
       If lResult = ERROR_SUCCESS Then
          intZeroPos = InStr(strBuf, Chr$(0))
            
          If intZeroPos > 0 Then
             ReadString = Left$(strBuf, intZeroPos - 1)
          Else
             ReadString = strBuf
          End If
       End If
    End Function
    
    Private Sub WriteString(hKey As Long, strPath As String, strValue As String, strdata As String)
       Dim keyhand As Long
       Dim r As Long
         
       r = RegCreateKey(hKey, strPath, keyhand)
       r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
       r = RegCloseKey(keyhand)
    End Sub
    
    Private Function ReadDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
       Dim lResult As Long
       Dim lValueType As Long
       Dim lBuf As Long
       Dim lDataBufSize As Long
       Dim r As Long
       Dim keyhand As Long
       
       r = RegOpenKey(hKey, strPath, keyhand)
       
       lDataBufSize = 4
           
       lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
       
       If lResult = ERROR_SUCCESS Then
          If lValueType = REG_DWORD Then ReadDword = lBuf
       End If
       
       r = RegCloseKey(keyhand)
    End Function
    
    Private Sub WriteDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
       Dim keyhand As Long
       Dim r As Long
         
       r = RegCreateKey(hKey, strPath, keyhand)
       r = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
       r = RegCloseKey(keyhand)
    End Function