Results 1 to 5 of 5

Thread: registry returns as boolean for some reason

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    registry returns as boolean for some reason

    I have this code in a .bas module, when ever i try to use it the value always comes back as false. If i go to regedit and find they key the value is password. I need the value to come back as password.

    Please can somebody tell me how to fix this as i need it.

    Code:
    Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, _
        ByRef KeyVal As String) As Boolean
        Dim i As Long
        Dim rc As Long
        Dim hKey As Long
        Dim KeyValType As Long
        Dim tmpVal As String
        Dim KeyValSize As Long
        
        rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey)
        If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError
        
        tmpVal = String$(1024, 0)
        KeyValSize = 1024
        
        rc = RegQueryValueEx(hKey, SubKeyRef, 0, KeyValType, tmpVal, KeyValSize)
        If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError
        
    
    
        If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
            tmpVal = Left(tmpVal, KeyValSize - 1)
        Else
            tmpVal = Left(tmpVal, KeyValSize)
        End If
        
    
    
        Select Case KeyValType
            Case REG_DWORD
    
    
            For i = Len(tmpVal) To 1 Step -1
                KeyVal = KeyVal + Format(Hex(Asc(Mid(tmpVal, i, 1))), "00")
            Next
            KeyVal = Format$("&h" + KeyVal)
            Case REG_SZ
            KeyVal = tmpVal
        End Select
    
    GetKeyValue = True
    rc = RegCloseKey(hKey)
    Exit Function
    
    GetKeyError:
    GetKeyValue = False
    rc = RegCloseKey(hKey)
    End Function
    thanks Chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: registry returns as boolean for some reason

    The function is written to return a Boolean so either True or False makes perfect sense.

    If you need it to return as string, then change As Boolean to As String.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: registry returns as boolean for some reason

    it still returns as false
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: registry returns as boolean for some reason

    Step through your code to find which line is causing the error.

    What is the value of the rc variable after the RegOpenKeyEx and RegQueryValueEx functions are called?

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: registry returns as boolean for some reason

    Try this (changes in red)
    Code:
    Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, _
        ByRef KeyVal As String) As String
        Dim i As Long
        Dim rc As Long
        Dim hKey As Long
        Dim KeyValType As Long
        Dim tmpVal As String
        Dim KeyValSize As Long
        
        rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey)
        If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError
        
        tmpVal = String$(1024, 0)
        KeyValSize = 1024
        
        rc = RegQueryValueEx(hKey, SubKeyRef, 0, KeyValType, tmpVal, KeyValSize)
        If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError
        
    
    
        If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
            tmpVal = Left(tmpVal, KeyValSize - 1)
        Else
            tmpVal = Left(tmpVal, KeyValSize)
        End If
        
    
    
        Select Case KeyValType
            Case REG_DWORD
    
    
            For i = Len(tmpVal) To 1 Step -1
                KeyVal = KeyVal + Format(Hex(Asc(Mid(tmpVal, i, 1))), "00")
            Next
            KeyVal = Format$("&h" + KeyVal)
            Case REG_SZ
            KeyVal = tmpVal
        End Select
    
    GetKeyValue = KeyVal
    rc = RegCloseKey(hKey)
    Exit Function
    
    GetKeyError:
    GetKeyValue = "Error in getting Key Value for " & KeyName
    rc = RegCloseKey(hKey)
    End Function
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width