p0lar_bear
p0lar_bear

Reputation: 2275

Keep getting error 2 (Registry path does not exist) with RegQueryValueEx in VBA

I'm trying to have VBA query the registry to see if an ODBC driver is installed, and I get error 2 when trying to read a key's value. I tried a simpler key/value, but no cigar.

Edit for Clarification

I am developing on a 32-bit system, but this needs to work for both 32 and 64-bit systems. What the problem is is that the call to RegOpenKeyEx works, but RegQueryValueEx returns error 2: file does not exist on my 32-bit system. My syntax appears correct, what am I doing wrong?

Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const ERROR_SUCCESS = 0&                ' Successful
Public Const ERROR_FILE_NOT_FOUND = 2&         ' Registry path does not exist
Public Const ERROR_ACCESS_DENIED = 5&          ' Requested permissions not available
Public Const STANDARD_RIGHTS_READ = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE _
                        Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) _
                        And (Not SYNCHRONIZE))
Public Const REG_SZ = 1 ' Unicode nul terminated string

Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As _
    Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    (ByVal hKey As Long, lpValueName As String, _
    ByVal lpReserved As Long, lpType As Long, _
    lpData As Any, lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

Sub TestRegAPI()
  Dim KeyName As String, handle As Long, handle2 As String

  'KeyName = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
  KeyName = "SOFTWARE\7-Zip"
  r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyName, 0, KEY_READ, handle)
  If r Then
    MsgBox "Unable to open the specified Registry key, code " & r
    Else
    'r = RegQueryValueEx(handle, "MySQL ODBC 5.1 Driver", 0, 1&, handle2, length)
    r = RegQueryValueEx(handle, "Path", 0, REG_SZ, handle2, length)
    RegCloseKey handle
  End If
End Sub

Upvotes: 1

Views: 4156

Answers (2)

Christian Blackburn
Christian Blackburn

Reputation: 11

From what I could find on Wikipedia Windows NT4 has WMI as a downloadable add-on and from Windows 2000 on it is a bundled component of Windows. Here my example is loading the installation path to Mozilla Thunderbird:

Const HKEY_LOCAL_MACHINE = &H80000002

Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")

strKey_Path = "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\thunderbird.exe"

 ' here the value name is blank, because I want the default value for the key, otherwise it wouldn't be ""
strValue_Name = ""



objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKey_Path, strValue_Name, strThunderbird_Path

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91376

This works for me on a 64 bit machine. It is from http://blogs.technet.com/b/heyscriptingguy/archive/2005/07/07/how-can-i-get-a-list-of-the-odbc-drivers-that-are-installed-on-a-computer.aspx

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
objRegistry.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes

For i = 0 To UBound(arrValueNames)
    strValueName = arrValueNames(i)
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
    Debug.Print arrValueNames(i) & " -- " & strValue
Next

Upvotes: 1

Related Questions