Results 1 to 23 of 23

Thread: cant read the registry

  1. #1

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    cant read the registry

    I am using a module by KayJay that I found in the codebank. Which I've attached

    Although I seriously doubt the fault is in there.

    I'm trying to access a certain key in the registry. and save to it works using this:
    VB Code:
    1. Call SaveSettingString(HKEY_CURRENT_USER, "\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "User Agent", cmbUA.Text)

    But I have trouble when reading the key using this:
    VB Code:
    1. MsgBox GetSettingString(HKEY_CURRENT_USER, "\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "User Agent")
    it just return a blank string

    Why is this happening? Why don't I get what has been saved?
    Attached Files Attached Files
    Have I helped you? Please Rate my posts.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Has it been saved? Open RegEdit and look.


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    yeah. it saves fine, it just doesn't read it
    Have I helped you? Please Rate my posts.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Have you tried it with another module? There are loads of them floating around... Maybe it is the module the problem...


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    I did try another one but that one had other problems. I'll go have another hunt for them
    Have I helped you? Please Rate my posts.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I keep posting one that people seem to like Search for my username or something


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: cant read the registry

    Originally posted by Acidic
    I am using a module by KayJay that I found in the codebank. Which I've attached

    Although I seriously doubt the fault is in there.

    I'm trying to access a certain key in the registry. and save to it works using this:
    VB Code:
    1. Call SaveSettingString(HKEY_CURRENT_USER, "\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "User Agent", cmbUA.Text)

    But I have trouble when reading the key using this:
    VB Code:
    1. MsgBox GetSettingString(HKEY_CURRENT_USER, "\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "User Agent")
    it just return a blank string

    Why is this happening? Why don't I get what has been saved?
    Set a breakpoint on the first line of code in the GetSettingString function and when the code gets there press F8 to step through the code line by line so that you can look at the variables and see what is happening.

  8. #8

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    OK. I've tried that, but my VB skills are too bad to see what's going wrong. I've attached the project so you can have a look
    Attached Files Attached Files
    Have I helped you? Please Rate my posts.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Sorry, but I don't like to fool with projects that modify the registry so I haven't run your project.

    Do you know how to set a breakpoint? If so what happens after you set the breakpoint on the first line of code in the GetSettingString function and you press F8?

  10. #10
    Lively Member rotcrules's Avatar
    Join Date
    Mar 2004
    Location
    SD
    Posts
    113
    what version of windows are you using?

    one thing you might want to try is insted of using a your function use the SaveSettings and GetSettings function see if that works first.
    [Vbcode]If YourOpinion = WindowsIsCrap Then
    Kill Wndows
    Open Linux
    ElseIf YourOpinion = WindowsIsGreat Then
    Unload Me
    Else
    Get MSNTV
    End If[/vbcode]

  11. #11

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    ok. I set the preakpoint to the getSettingString function in the module
    I press F8 once and it goes to the first if statement. This bit is there only to determine if one of the optional parameters are filled in. It isn't so it goes past this making GetSettingString="".

    Then into the actual code, lRegResult becomes 2. Then after another F8 it becomes 1010.
    Then comes this line:
    VB Code:
    1. If lRegResult = ERROR_SUCCESS Then
    Obiously it fails that, so it skips the whole block. That's why it doesn't do anything. I'll post the function

    VB Code:
    1. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
    2. Dim hCurKey As Long
    3. Dim lValueType As Long
    4. Dim strBuffer As String
    5. Dim lDataBufferSize As Long
    6. Dim intZeroPos As Integer
    7. Dim lRegResult As Long
    8.  
    9. ' Set up default value
    10. If Not IsEmpty(Default) Then
    11.   GetSettingString = Default
    12. Else
    13.   GetSettingString = ""
    14. End If
    15. 'GetSettingString remains ""
    16.  
    17. ' Open the key and get length of string
    18. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    19. 'After that line it =2
    20. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    21. 'And here it = 1010
    22.  
    23. If lRegResult = ERROR_SUCCESS Then
    24. 'The it fails that IF missing out the whole thing
    25.   If lValueType = REG_SZ Then
    26.     ' initialise string buffer and retrieve string
    27.     strBuffer = String(lDataBufferSize, " ")
    28.     lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
    29.    
    30.     ' format string
    31.     intZeroPos = InStr(strBuffer, Chr$(0))
    32.     If intZeroPos > 0 Then
    33.       GetSettingString = Left$(strBuffer, intZeroPos - 1)
    34.     Else
    35.       GetSettingString = strBuffer
    36.     End If
    37.  
    38.   End If
    39.  
    40. Else
    41.   ' there is a problem
    42. End If
    43.  
    44. lRegResult = RegCloseKey(hCurKey)
    45. End Function
    Have I helped you? Please Rate my posts.

  12. #12

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Originally posted by rotcrules
    what version of windows are you using?

    one thing you might want to try is insted of using a your function use the SaveSettings and GetSettings function see if that works first.
    I'm using Win98.
    Could you give me some quick code to test those two functions. They won't work for my program because I need to edit another string which they can't access. But I could still test them out, just to see if they work.

    edit: never mind. I'm using the code from the link in martins code.
    2nd edit That code worked fine. But as I said that won't work for this project.
    Have I helped you? Please Rate my posts.

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here is what the API Guide says about the return code from both RegOpenKey and RegQueryValueEx.

    VB Code:
    1. If the function succeeds, the return value is ERROR_SUCCESS.
    2.  
    3. If the function fails, the return value is a nonzero error code defined in WINERROR.H. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.

    Here is a FormatMessage example.

    VB Code:
    1. Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
    2. Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    3. Const LANG_NEUTRAL = &H0
    4. Const SUBLANG_DEFAULT = &H1
    5. Const ERROR_BAD_USERNAME = 2202&
    6. Private Declare Function GetLastError Lib "kernel32" () As Long
    7. Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
    8. Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
    9. Private Sub Form_Load()
    10.     'KPD-Team 1999
    11.     'URL: [url]http://www.allapi.net/[/url]
    12.     'E-Mail: [email][email protected][/email]
    13.     Dim Buffer As String
    14.     'Create a string buffer
    15.     Buffer = Space(200)
    16.     'Set the error number
    17.     SetLastError ERROR_BAD_USERNAME
    18.     'Format the message string
    19.     FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
    20.     'Show the message
    21.     MsgBox Buffer
    22. End Sub

  14. #14

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Hmm. I'm not sure how to use that code to get a more generic description of the error.
    Have I helped you? Please Rate my posts.

  15. #15

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    right. I'm off for the night. I'll keep trying to figure this out tomorrow. Thanks for all the help guys. I know helping a noob is really boring and frustrating.
    Have I helped you? Please Rate my posts.

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Try this (which I haven't tested)

    VB Code:
    1. Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    2. Private Declare Function GetLastError Lib "kernel32" () As Long
    3. Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
    4.  
    5. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
    6. Dim hCurKey As Long
    7. Dim lValueType As Long
    8. Dim strBuffer As String
    9. Dim lDataBufferSize As Long
    10. Dim intZeroPos As Integer
    11. Dim lRegResult As Long
    12. [b]    Dim Buffer As String
    13.     'Create a string buffer
    14.     Buffer = Space(200)[/b]
    15.  
    16. ' Set up default value
    17. If Not IsEmpty(Default) Then
    18.   GetSettingString = Default
    19. Else
    20.   GetSettingString = ""
    21. End If
    22. 'GetSettingString remains ""
    23.  
    24. ' Open the key and get length of string
    25. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    26. [b]    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
    27.     'Show the message
    28.     MsgBox Buffer[/b]
    29. 'After that line it =2
    30. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    31. 'And here it = 1010
    32. [b]    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
    33.     'Show the message
    34.     MsgBox Buffer[/b]
    35. If lRegResult = ERROR_SUCCESS Then
    36. 'The it fails that IF missing out the whole thing
    37.   If lValueType = REG_SZ Then
    38.     ' initialise string buffer and retrieve string
    39.     strBuffer = String(lDataBufferSize, " ")
    40.     lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
    41.    
    42.     ' format string
    43.     intZeroPos = InStr(strBuffer, Chr$(0))
    44.     If intZeroPos > 0 Then
    45.       GetSettingString = Left$(strBuffer, intZeroPos - 1)
    46.     Else
    47.       GetSettingString = strBuffer
    48.     End If
    49.  
    50.   End If
    51.  
    52. Else
    53.   ' there is a problem
    54. End If
    55.  
    56. lRegResult = RegCloseKey(hCurKey)
    57. End Function

  17. #17

  18. #18

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    OK. All the code you posted I shoved in a module in a new project. I then called the function the same way i did before. and I get this error:
    "ByRef Argument Type Mismatch"
    and it highlights the button like this:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim originalUA As String
    3. originalUA = GetSettingString([COLOR=orange]HKEY_CURRENT_USER[/COLOR] , "\Software\Microsoft\Windows\CurrentVersion\Internet Settings", "User Agent")
    4. MsgBox originalUA
    5. End Sub

    I then went into the code in the module and changed your private's to public's. Same error though.
    Have I helped you? Please Rate my posts.

  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    The GetSettingString function is defined this way:

    VB Code:
    1. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String

    So it expects that the first parameter it receives will be a Long. Is HKEY_CURRENT_USER defines as a long?

  20. #20

    Thread Starter
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    uuhm. well what should the parameter be?
    Have I helped you? Please Rate my posts.

  21. #21

  22. #22
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Although this thread is abit old, meh!! Was trying to use the code posted above and I found it wouldn't work while I started strPath parameter with a \, removing it solved the problem.
    Don't Rate my posts.

  23. #23
    Hyperactive Member AvisSoft's Avatar
    Join Date
    Sep 2002
    Location
    Chandigarh
    Posts
    459
    Hi!

    I used that registry module too...and found that its not as powerful as the cRegistry module.

    You can get the cRegistry module with examples from the following url: http://www.maxvb.com/viewtopic.php?p=2511

    Thanks!
    Tapan Bhanot,
    CEO, Avis Software.
    Website: www.avissoftware.com

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