Hey,

I'm having trouble writing to the registry on a remote computer with RegSetValueEx. It returns error code 5, access is denied. I have checked the registry permissions and they are all set correctly.

My declarations;

Code:
Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Declare Function RegCloseKey& Lib "advapi32.dll" (ByVal hKey&)

Declare Function RegOpenKeyExA& Lib "advapi32.dll" (ByVal hKey&, _
                ByVal lpszSubKey$, dwOptions&, _
                ByVal samDesired&, lpHKey&)
                
Declare Function RegQueryValueExA& Lib "advapi32.dll" (ByVal hKey&, _
                ByVal lpszValueName$, ByVal lpdwRes&, _
                lpdwType&, ByVal lpDataBuff$, nSize&)
                
Declare Function RegQueryValueEx& Lib "advapi32.dll" _
                Alias "RegQueryValueExA" _
                (ByVal hKey&, ByVal lpszValueName$, _
                ByVal lpdwRes&, lpdwType&, _
                lpDataBuff&, nSize&)
                
Declare Function RegConnectRegistry Lib "advapi32.dll" _
                Alias "RegConnectRegistryA" _
                (ByVal lpMachineName As String, _
                ByVal hKey As Long, _
                phkResult As Long) As Long
                
Private Declare Function RegSetValueEx Lib "advapi32.dll" _
                Alias "RegSetValueExA" (ByVal hKey As Long, _
                ByVal lpValueName As String, ByVal Reserved As Long, _
                ByVal dwType As Long, lpData As Any, _
                ByVal cbData As Long) As Long
                               
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

Const ERROR_SUCCESS = 0&
Const REG_SZ = 1&                          ' Unicode nul terminated string
Const REG_DWORD = 4&                       ' 32-bit number

Const KEY_QUERY_VALUE = &H1&
Const KEY_SET_VALUE = &H2&
Const KEY_CREATE_SUB_KEY = &H4&
Const KEY_ENUMERATE_SUB_KEYS = &H8&
Const KEY_NOTIFY = &H10&
Const KEY_CREATE_LINK = &H20&
Const READ_CONTROL = &H20000
Const WRITE_DAC = &H40000
Const WRITE_OWNER = &H80000
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_READ = READ_CONTROL
Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Const Key_Write = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Const KEY_EXECUTE = KEY_READ

'Global variable
Public hRemoteReg As Long
Public remoteState As String
Connect to remote computer;

Code:
Function remoteConnection(remoteComputer As String) As Boolean

    Dim lRet As Long

    'Connect to the remote registry
    lRet = RegConnectRegistry(remoteComputer, _
                              HKEY_LOCAL_MACHINE, _
                              hRemoteReg)

    If (lRet = ERROR_SUCCESS) Then
        remoteConnection = True 'Connected to remote computer
        MsgBox "Successfully connected to remote computer.", 64, "Information"
    Else
        remoteConnection = False 'Failed to connect to remote computer
        MsgBox "Cannot continue; Could not connect to remote computer. Check IP address or computer name.", 48, "Warning"
    End If

End Function
Check the key is there, and if it is then write to it. Everything in this function works except writing to the key;

Code:
Function checkKey(SubKey$, value$, regValue&) As Boolean

    Dim sKeyType&       'To return the key type.  This function expects REG_SZ or REG_DWORD
    Dim ret&            'Returned by registry functions, should be 0&
    Dim lpHKey&         'Return handle to opened key
    Dim lpcbData&       'Length of data in returned string
    Dim ReturnedString$ 'Returned string value
    Dim ReturnedLong&   'Returned long value
    Dim keyValue$       'Value of read key
    
    'Open key dir
    ret = RegOpenKeyExA(hRemoteReg, SubKey, 0&, KEY_READ, lpHKey)
    
    If ret <> ERROR_SUCCESS Then
        checkKey = False 'Unable to open key dir, so leave
        MsgBox "Cannot continue; Unable to locate key directory. Check registry permissions on the remote computer.", 48, "Warning"
        Exit Function
    End If
    
    'Let user know registry is open
    MsgBox "Registry opened.", 64, "Information"
    
    'Set up buffer for data to be returned in
    'Adjust next value for larger buffers
    lpcbData = 255
    ReturnedString = Space$(lpcbData)
    
    'Attempt to read key value
    ret& = RegQueryValueExA(lpHKey, value, ByVal 0&, sKeyType, ReturnedString, lpcbData)
    
    If ret <> ERROR_SUCCESS Then
    
        checkKey = False   'Value probably doesn't exist or permissions deny access
        MsgBox "Cannot continue; Unable to read key. Check registry permissions on the remote computer.", 48, "Warning"
        Exit Function
        
    Else
    
        If sKeyType = REG_DWORD Then
            ret = RegQueryValueEx(lpHKey, value, ByVal 0&, sKeyType, ReturnedLong, 4)
            
            If ret = ERROR_SUCCESS Then 'Found integer key
                checkKey = True
                keyValue = CStr(ReturnedLong)
                MsgBox "Key successfully read.", 64, "Information"
                
                Select Case keyValue
                    Case 0
                        remoteState = "enabled"
                        MsgBox "Cannot continue; RDP is already enabled on the remote computer.", 48, "Warning"
                        'Remote access is alread enabled, so we can close the registry key
                        ret = RegCloseKey(lpHKey)
                        MsgBox "Registry closed.", 64, "Information"
                        Exit Function
                    Case 1
                        remoteState = "disabled"
                        'Enable remote access by changing the fDenyTSConnections key to 0

'-------------------------------------------
'-------------------------------------------
'THIS IS THE FUNCTION THAT'S GOING WRONG;
'-------------------------------------------
'-------------------------------------------


                        ret = RegSetValueEx(lpHKey, value, 0&, REG_DWORD, regValue, 4)
                        MsgBox ReturnAPIError(ret)
                        If ret <> ERROR_SUCCESS Then 'Could not set key value
                            checkKey = False
                            MsgBox "Cannot continue; Unable to write RDP key value. Check registry permissions on the remote computer.", 48, "Warning"
                            ret = RegCloseKey(lpHKey) 'Close registry key
                            Exit Function
                        Else
                            checkKey = True
                            MsgBox "RDP key value successfully updated.", 64, "Information"
                        End If
                End Select
                
            End If
        Else 'Found string key
                keyValue = Left$(ReturnedString, lpcbData - 1)
                checkKey = True
                MsgBox "Key successfully read. Key value is " & keyValue & ".", 64, "Information"
        End If
        
    End If
    
End Function
I realise it is allot of code to trawl through, but if anyone has the time I would greatly appretiate it

Thanks,

-Ross