writing to registry with RegSetValueEx gives access denied


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: writing to registry with RegSetValueEx gives access denied

  1. #1
    Join Date
    Mar 2006
    Posts
    10

    writing to registry with RegSetValueEx gives access denied

    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

  2. #2
    Join Date
    Mar 2006
    Posts
    10
    [edit]

    Nevermind, sorted it
    Last edited by RossOliver; 03-15-2006 at 02:22 PM.

  3. #3
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    498
    Ross,

    Never thought of altering/checking registry on another machine, but see it could be usefull (if used carefully!). Would you please advise how you fixed your original problem?

    Thanks Greg

  4. #4
    Join Date
    Mar 2006
    Posts
    10
    Hey,

    Well, the code above I ended up scrapping because it was useless

    I then ran into a problem with my new code where it would put a 3 behind every number I was trying to write to the key. So if I told it to write 1 it would enter 31. I found out that was because I opened the registry with KEY_READ instead of KEY_READ Or Key_Write.

    This code works fine(I won't include all of it, it's a big mess at the minute. This is just my open/read/write API calls);

    Code:
    ret = RegConnectRegistry("192.168.0.195", HKEY_LOCAL_MACHINE,                       hRemoteReg)
    ret = RegOpenKeyExA(hRemoteReg, "path\to\key", 0&, KEY_READ Or Key_Write, lpHKey)
    ret = RegQueryValueEx(lpHKey, "keyName", ByVal 0&, REG_DWORD, ReturnedLong, 4)
    ret = RegSetValueEx(lpHKey, "keyName", 0&, REG_DWORD, newKeyValue, 4)
    Hope that helps, if you need any other bits let me know,

    -Ross

  5. #5
    Join Date
    Feb 2004
    Location
    Sydney, Australia
    Posts
    498
    Thanks

Similar Threads

  1. writing to registry: No error, but also no effect
    By Johnny Knoxville in forum VB Classic
    Replies: 0
    Last Post: 05-13-2002, 07:14 AM
  2. Access 97 to 2000 Conversion Error
    By Michelle in forum VB Classic
    Replies: 8
    Last Post: 04-19-2002, 06:01 PM
  3. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 11:28 AM
  4. Replies: 1
    Last Post: 06-07-2001, 04:04 PM
  5. Access denied to preserve.web
    By Phil Haack in forum ASP.NET
    Replies: 3
    Last Post: 11-11-2000, 04:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center