Trouble reading registry on Win7 x64 with A2010 x86

mdlueck

Sr. Application Developer
Local time
Today, 07:38
Joined
Jun 23, 2011
Messages
2,648
I have always had solid read access to the Windows registry running on Windows XP with Access 2007. I am utilizing code from here in a separate VBA Module to obtain access to the Windows registry:

"Change registry settings in VBA"
http://www.programmersheaven.com/mb/vba/371593/371593/change-registry-settings-in-vba/

On Windows 7 x64 / Access 2010 x86, I am seeing a 0 value in lngKeyHandle. Also, m_lngRetVal has a value of 2 after the RegOpenKey LOC.

Code:
' --------------------------------------------------------------
' Query the key path
' --------------------------------------------------------------
  m_lngRetVal = RegOpenKey(lngRootKey, strRegKeyPath, lngKeyHandle)
  
' --------------------------------------------------------------
' If no key handle was found then there is no key.  Leave here.
' --------------------------------------------------------------
  If lngKeyHandle = 0 Then
      regQuery_A_Key = vbNullString
      m_lngRetVal = RegCloseKey(lngKeyHandle)   ' always close the handle
      Exit Function
  End If
I have verified that I have the key successfully in the registry... installed via the .reg file that works on Windows XP / Access 2007 systems.

Suggestions anyone? TIA!
 
Are you sure you're breaking up the full key name correctly? Would this help . .
Code:
Private Function GetRootKeyHandle(FullKeyName As String) As Long
[COLOR="Green"]'  Returns the long integer handle of the given registry root node[/COLOR]
   Select Case Split(FullKeyName, "\")(0)
      Case "HKEY_CLASSES_ROOT"
         GetRootKeyHandle = &H80000000
      Case "HKEY_CURRENT_USER"
         GetRootKeyHandle = &H80000001
      Case "HKEY_LOCAL_MACHINE"
         GetRootKeyHandle = &H80000002
      Case "HKEY_USERS"
         GetRootKeyHandle = &H80000003
      Case Else
         Error 5
   End Select
End Function

Private Function GetSubKeyNameFromFullKey(keyname As String) As String
[COLOR="Green"]'  Drops the first key and returns the subkey path
'  eg: returns "SOFTWARE\Sample\Testing"
'           from   "HKEY_LOCAL_MACHINE\SOFTWARE\Sample\Testing"[/COLOR]
   GetSubKeyNameFromFullKey = Mid(keyname, InStr(keyname, "\") + 1)
End Function
. . . then call the open like . . .
Code:
m_val = RegOpenKey(GetRootKeyHandle(FullKey), GetSubKeyNameFromFullKey(FullKey), KeyHandle)
. . . because error 2 is file not found.

But then these are all 32 bit API calls too :confused:
hth
 
Are you sure you're breaking up the full key name correctly?

I would expect programs which work properly on Windows XP / Access 2007 to remain working properly on Windows 7 x64 64-bit / Access 2010 x86 32-bit.

My use of the functions in this Module are as follows:

Code:
  'Read the values out of the registry
  strDBServer = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "Server")
  strDBDatabase = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "Database")
  strDBUID = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "UID")
  strDBPWD = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "PWD")
But then these are all 32 bit API calls too :confused:

So is there some known limitation on using 32-bit registry API's found in Access 2010 x86 32-bit on x64 64-bit Windows to access the registry?
 
I came across a link stating troubles with running x86 API calls from x64 Windows OS here:

"Can't Run Code on a 64 Bit Machine"
http://www.mrexcel.com/forum/excel-questions/646239-cant-run-code-64-bit-machine.html#post3206098

So I updated the API declarations thusly:

Code:
' --------------------------------------------------------------
' Declarations required to access the Windows registry
' --------------------------------------------------------------
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootKey As Long) As Long
  
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
            (ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
            (ByVal lngRootKey As Long, ByVal lpSubKey As String) As Long
  
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
            (ByVal lngRootKey As Long, ByVal lpValueName As String) As Long
  
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
            (ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
            (ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
             lpType As Long, lpData As Any, lpcbData As Long) As Long
  
  Private Declare [COLOR=Blue][B]PtrSafe[/B][/COLOR] Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
            (ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
             ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
I get exactly the same return codes as before.
 
So what, exactly, are you passing to the function?
 
So what, exactly, are you passing to the function?

?????????????

Trying again....

My use of the functions in this Module are as follows:

Code:
  'Read the values out of the registry
  strDBServer = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "Server")
  strDBDatabase = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "Database")
  strDBUID = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "UID")
  strDBPWD = regQuery_A_Key(HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database", "PWD")

It is appearing that I am banging into a permissions issue with Windows 7... that conclusion from this post:

VBA Code Failing in Win7/Excel 2010, Works in XP/Excel 2007
http://stackoverflow.com/questions/...in7-excel-2010-works-in-xp-excel-2007#7386830

Still clawing my way forward... :banghead:
 
Solved!!! Wowsers....

This is what was going on. Windows 7 x64 + Access 2010 x86 = need to move program settings in the registry due to:

Running 32-bit Applications \ Registry Redirector
http://msdn.microsoft.com/en-us/library/aa384232(v=vs.85).aspx

So the program running in x86 VBA still reads from:
HKEY_LOCAL_MACHINE, "SOFTWARE\ClientNameHere\Fandango\Database"

The actual .reg file I populate the keys into the registry with, however, must be updated to store the data to:
HKEY_LOCAL_MACHINE, "SOFTWARE\Wow6432Node\ClientNameHere\Fandango\Database"

On to stitching up the patient... :cool:
 

Users who are viewing this thread

Back
Top Bottom