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