Click to See Complete Forum and Search --> : HKEY_LOCAL_MACHINE and RegOpenKey


GeorgeT
October 16th, 2001, 12:45 PM
Hi,

I need to read value from HKEY_LOCAL_MACHINE.
Same code works for HKEY_CURRENT_USER. Anybody knows whats is going on? Thanks for help.

Function call

DAAPath = GetRegEntry(HKEY_LOCAL_MACHINE, "\Software\Test", "DAA2000")

'---------------------------------------------
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (byval hkey as Long, byval lpSubKey as string, phkResult as Long) as Long
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
Declare Function RegCloseKey Lib "advapi32.dll" (byval hkey as Long) as Long

public Const ERROR_SUCCESS = 0&

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 HKEY_CURRENT_CONFIG = &H80000005
public Const HKEY_DYN_DATA = &H80000006


Function GetRegEntry(RootKey as Long, SubKeyName as string, KeyEntry as string) as string

If RegOpenKey(RootKey, SubKeyName, hkey&) = ERROR_SUCCESS then
Returned$ = Space$(255)
Success& = RegQueryValueEx(hkey&, KeyEntry, 0&, lpType&, byval Returned$, 255)
Returned$ = Trim$(Returned$)
If (Returned$ <> "") then
GetRegEntry = Left$(Returned$, len(Returned$) - 1)
End If
RegCloseKey (hkey&)
End If

End Function

Cimperiali
October 17th, 2001, 07:30 AM
Cakkie answered something like this: you must have permissions to access the machine key. You can test this running regedit and trying to manually add a key to the HKEY_LOCAL_MACHINE. I am afraud you ewill have to relog as local administrator to be enabled...


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater