I started by trying to read a key I created: HKLM\software\mng\test (a simple string). That always failed in HKLM, but worked fine when created in HKCU. My code is hardly worth looking at:
Code:
Sub test_ws()
Set wsh = CreateObject("Wscript.Shell")
cu = wsh.regread("HKCU\Software\mng\test")
lm = wsh.regread("HKLM\Software\mng\test")
'lm = wsh.regread("HKLM\Software\pdfforge\pdfcreator\program\applicationversion")
'lm = wsh.regread("HKLM\Software\7-zip\Path")
End Sub
I added the extra tests on other keys just now to no avail (I don't have the key you used). Before that I had spent some time trying to do it using the Win-32 API calls, confident that that would work. Its a bit more complicated:
Code:
' By MNG, but with thanks to the API-Guide guys who were at Allapi.net, now apparently no more'
Const HKCU = &H80000001
Const HKLM = &H80000002
Const KEY_READ = &H20019
Const KEY_ALL_ACCESS = &HF003F
Const REG_SZ = 1 ' Unicode nul terminated string'
Const REG_BINARY = 3 ' Free form binary'
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, 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
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize) 'get key info'
If lResult = 0 Then
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, Chr$(0)) 'Create a buffer'
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize) 'retrieve value'
If lResult = 0 Then RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1) 'Remove nulls'
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize) 'retrieve value'
If lResult = 0 Then RegQueryStringValue = strData
End If
End If
End Function
Sub test_api()
RegOpenKeyEx HKLM, "software\\mng", 0, KEY_READ, Key
Value = RegQueryStringValue(Key, "test")
MsgBox Value, , "HK??"
End Sub
NB Too much time spent pairing the quote marks to get comments & strings in green!
I just hand edit it to either read HKLM or HKCU - the former failed as with the scripting object, HKCU always worked.
I did try starting Excel as an adminstrator (no change), I checked the permissions of both my 'mng' key, and the ones up the hierarchy - I apparently have full read (and sometimes write) access. Changing the key access to KEY_ALL_ACCESS didn't work - I didn''t expect it to, as that is setting what one wants to do, not giving oneself permission. So I am stumped!
I can see many others in the past have had an issue reading HKLM from VBA, but have seen no solutions. I ahve seen references to a 32-64 bit split in Registry info, but nothing on how to get at the other half, if that is what is going on.