Click to See Complete Forum and Search --> : Registry Access Priviledges


Kruddler
March 28th, 2001, 12:35 AM
I need to obtain Read access or Enumerate_Subkeys access to the System node under HKEY_LOCAL_MACHINE of the registry. I have no problem doing this on 98' however NT and 2000 seem to have security restriction on this node. I can open this node through Regedit on 2000 but not programmatically. Could someone help me please. I have attempted to open the node with RegOpenKeyEx and RegCreateKey. Help someone is going to beat me!

Peter Anderberg
April 11th, 2001, 06:38 AM
Here are som reg functions for you..

Public Function EnumValue(ByVal lpSubKey As String, ByVal strValue As String) As String
Dim hKey As Long 'RegCreateKeyEx
Dim lpClass As String 'RegCreateKeyEx
Dim dwOptions As Long 'RegCreateKeyEx
Dim samDesired As Long 'RegCreateKeyEx
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES 'RegCreateKeyEx
Dim phkResult As Long 'RegCreateKeyEx
Dim lpdwDisposition As Long 'RegCreateKeyEx

Dim dwIndex As Long 'RegEnumValue
Dim nameBuffer(100) As Byte 'RegEnumValue
Dim lpcbValueName As Long 'RegEnumValue
Dim lpReserved As Long 'RegEnumValue
Dim lpType As Long 'RegEnumValue
Dim valueBuffer(100) As Byte 'RegEnumValue
Dim lpcbData As Long 'RegEnumValue

Dim Name As String
Dim value As String
Dim strRet As String

Dim iReturnValue As Long

On Error GoTo ErrorHandler

hKey = HKEY_LOCAL_MACHINE
lpClass = ""
dwOptions = REG_OPTION_NON_VOLATILE
samDesired = KEY_ALL_ACCESS

dwIndex = 0
lpcbValueName = 100
lpcbData = 100

iReturnValue = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCREATEKEYEX

Do
iReturnValue = RegEnumValue(phkResult, dwIndex, nameBuffer(0), lpcbValueName, lpReserved, lpType, valueBuffer(0), lpcbData)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGENUMVALUE

Dim iName As Long
For iName = 0 To lpcbValueName - 1
Name = Name & Chr(nameBuffer(iName))
Next iName

Dim iValue As Long
For iValue = 0 To lpcbData - 2
value = value & Chr(valueBuffer(iValue))
Next iValue

If LCase(Name) = LCase(strValue) Then
strRet = value
Exit Do
End If

'CLEAN UP
lpcbValueName = 100
lpcbData = 100
Name = ""
value = ""
dwIndex = dwIndex + 1
For iName = 0 To 100
nameBuffer(iName) = 0
Next iName
For iValue = 0 To 100
valueBuffer(iValue) = 0
Next iValue
'END CLEAN UP
Loop Until iReturnValue = ERROR_NO_MORE_ITEMS

iReturnValue = RegCloseKey(phkResult)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCLOSEKEY

EnumValue = strRet
Exit Function
ErrorHandler:
If Err.Number = ERROR_REGENUMVALUE Then
EnumValue = ""
Else
Err.Raise Number:=ERROR_ENUMVALUE, Description:="ERROR_ENUMVALUE"
End If
End Function

Public Function EnumValues(ByVal lpSubKey As String) As String()
Dim hKey As Long 'RegCreateKeyEx
Dim lpClass As String 'RegCreateKeyEx
Dim dwOptions As Long 'RegCreateKeyEx
Dim samDesired As Long 'RegCreateKeyEx
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES 'RegCreateKeyEx
Dim phkResult As Long 'RegCreateKeyEx
Dim lpdwDisposition As Long 'RegCreateKeyEx

Dim dwIndex As Long 'RegEnumValue
Dim nameBuffer(100) As Byte 'RegEnumValue
Dim lpcbValueName As Long 'RegEnumValue
Dim lpReserved As Long 'RegEnumValue
Dim lpType As Long 'RegEnumValue
Dim valueBuffer(100) As Byte 'RegEnumValue
Dim lpcbData As Long 'RegEnumValue

Dim Name As String
Dim value As String
Dim strRet() As String
Dim i As Long
Dim iReturnValue As Long
Dim iName As Long

On Error GoTo ErrorHandler

hKey = HKEY_LOCAL_MACHINE
lpClass = ""
dwOptions = REG_OPTION_NON_VOLATILE
samDesired = KEY_ALL_ACCESS

dwIndex = 0
lpcbValueName = 100
lpcbData = 100

iReturnValue = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCREATEKEYEX

Do
iReturnValue = RegEnumValue(phkResult, dwIndex, nameBuffer(0), lpcbValueName, lpReserved, lpType, valueBuffer(0), lpcbData)
If iReturnValue <> ERROR_SUCCESS And iReturnValue <> ERROR_NO_MORE_ITEMS Then Err.Raise ERROR_REGENUMVALUE
For iName = 0 To lpcbValueName - 1
Name = Name & Chr(nameBuffer(iName))
Next iName

If iReturnValue <> ERROR_NO_MORE_ITEMS Then
i = i + 1
ReDim Preserve strRet(i)
strRet(i - 1) = Name
End If

'CLEAN UP
lpcbValueName = 100
lpcbData = 100
Name = ""
value = ""
dwIndex = dwIndex + 1
For iName = 0 To 100
nameBuffer(iName) = 0
Next iName
'END CLEAN UP
Loop Until iReturnValue = ERROR_NO_MORE_ITEMS

iReturnValue = RegCloseKey(phkResult)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCLOSEKEY

EnumValues = strRet
Exit Function
ErrorHandler:
ReDim strRet(1)
If Err.Number = ERROR_REGENUMVALUE Then
strRet(0) = "EOF"
EnumValues = strRet
Else
Err.Raise Number:=ERROR_ENUMVALUES, Description:="ERROR_ENUMVALUES"
End If
End Function

Public Function SetValue(ByVal lpSubKey As String, ByVal lpValueName As String, ByVal lpData As String) As Long
Dim hKey As Long
Dim lpClass As String
Dim dwOptions As Long
Dim samDesired As Long
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES
Dim phkResult As Long
Dim lpdwDisposition As Long

Dim dwType As Long
Dim cbData As Long

Dim iReturnValue As Long

On Error GoTo ErrorHandler

hKey = HKEY_LOCAL_MACHINE
lpClass = ""
dwOptions = REG_OPTION_NON_VOLATILE
samDesired = KEY_ALL_ACCESS

dwType = REG_SZ
cbData = Len(lpData)

iReturnValue = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCREATEKEY

'Note that if you declare the lpData parameter as String, you must pass it By Value.
iReturnValue = RegSetValueEx(phkResult, lpValueName, 0, dwType, ByVal lpData, cbData)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGSETVALUEEX

iReturnValue = RegCloseKey(phkResult)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCLOSEKEY

SetValue = 0
Exit Function
ErrorHandler:
Err.Raise Number:=ERROR_SETVALUE, Description:="ERROR_SETVALUE"
End Function

Public Function DeleteKey(ByVal lpSubKey As String) As Long
Dim hKey As Long
Dim iReturnValue As Long

On Error GoTo ErrorHandler

hKey = HKEY_LOCAL_MACHINE

iReturnValue = RegDeleteKey(hKey, lpSubKey)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGDELETEKEY

DeleteKey = 0
Exit Function
ErrorHandler:
Err.Raise Number:=ERROR_DELETEKEY, Description:="ERROR_DELETEKEY"
End Function

Public Function CreateKey(ByVal lpSubKey As String) As Long
Dim hKey As Long
Dim lpClass As String
Dim dwOptions As Long
Dim samDesired As Long
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES
Dim phkResult As Long
Dim lpdwDisposition As Long

Dim iReturnValue As Long

On Error GoTo ErrorHandler

hKey = HKEY_LOCAL_MACHINE
lpClass = ""
dwOptions = REG_OPTION_NON_VOLATILE
samDesired = KEY_ALL_ACCESS

iReturnValue = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCREATEKEYEX

iReturnValue = RegCloseKey(phkResult)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCLOSEKEY

CreateKey = 0
Exit Function
ErrorHandler:
Err.Raise Number:=ERROR_CREATEKEY, Description:="ERROR_CREATEKEY"
End Function

Public Function DeleteValue(ByVal lpSubKey As String, ByVal lpValueName As String) As Long
Dim hKey As Long
Dim lpClass As String
Dim dwOptions As Long
Dim samDesired As Long
Dim lpSecurityAttributes As SECURITY_ATTRIBUTES
Dim phkResult As Long
Dim lpdwDisposition As Long

Dim iReturnValue As Long

On Error GoTo ErrorHandler

hKey = HKEY_LOCAL_MACHINE
lpClass = ""
dwOptions = REG_OPTION_NON_VOLATILE
samDesired = KEY_ALL_ACCESS

iReturnValue = RegCreateKeyEx(hKey, lpSubKey, 0, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCREATEKEYEX

iReturnValue = RegDeleteValue(phkResult, lpValueName)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGDELETEVALUE

iReturnValue = RegCloseKey(phkResult)
If iReturnValue <> ERROR_SUCCESS Then Err.Raise ERROR_REGCLOSEKEY

DeleteValue = 0
Exit Function
ErrorHandler:
Err.Raise Number:=ERROR_DELETEVALUE, Description:="ERROR_DELETEVALUE"
End Function