Results 1 to 9 of 9

Thread: How To Get Value From Registry Entry

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    How To Get Value From Registry Entry

    Given the following Registry Info how do I extract the value from a SubItem?

    HKEY_LOCAL_MACHINE\SOFTWARE\RealNetworks\RealPlayer\version

    By looking into the registry the value of version is 6.0.14.806. How do I code to get that value into a variable?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How To Get Value From Registry Entry

    how you want to do?
    apis or scripting

    edit, here is a very simple script example
    Code:
          Dim wsh, str
    on error resume next
          Set wsh = CreateObject("WScript.Shell")
    
             str = wsh.RegRead("HKCR\.pid\") ' change to suit your key
    'msgbox err.number
    '	if err.number = "-2147024894" then str = "filetype not registered"
         msgbox str
    Last edited by westconn1; Nov 8th, 2008 at 05:20 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How To Get Value From Registry Entry

    Heres a module I made just for reading HKEY_LOCAL_MACHINE sinces thats pretty much all I ever need anyway.

    Debug.Print sdaGetRegEntry("SOFTWARE\RealNetworks\RealPlayer\", "version")
    Attached Files Attached Files

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How To Get Value From Registry Entry

    Quote Originally Posted by westconn1
    how you want to do?
    apis or scripting

    edit, here is a very simple script example
    Code:
          Dim wsh, str
    on error resume next
          Set wsh = CreateObject("WScript.Shell")
    
             str = wsh.RegRead("HKCR\.pid\") ' change to suit your key
    'msgbox err.number
    '	if err.number = "-2147024894" then str = "filetype not registered"
         msgbox str
    Nope, returns an empty value. I did subsitute with my key like this

    str = wsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\RealNetworks\RealPlayer\version")

    and also like this:

    str = wsh.RegRead("HKLM\SOFTWARE\RealNetworks\RealPlayer\version")

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How To Get Value From Registry Entry

    You'll may have to explicitly declare the variable types if you're doing it in VB6 rather than a vbs script. i.e.


    vb Code:
    1. Private Sub Form_Load()
    2.    Dim WshShell   As Object
    3.    Dim strValue   As String
    4.    
    5.    Set WshShell = CreateObject("Wscript.Shell")
    6.    MsgBox WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\RealNetworks\RealPlayer\version")
    7. End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How To Get Value From Registry Entry

    i tested code in vb6, did not return for that key as i don't have, but worked ok for others
    try a \ at the end of the string if it is a default value rather than a named value

    "HKLM\SOFTWARE\RealNetworks\RealPlayer\version\"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How To Get Value From Registry Entry

    Below works
    Code:
     Dim WshShell   As Object
     Dim strValue   As String
       
     Set WshShell = CreateObject("Wscript.Shell")
     strValue = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\RealNetworks\RealPlayer\version")
    Below also works
    Code:
     Dim VersionNumber As String
    
     VersionNumber = sdaGetRegEntry("SOFTWARE\RealNetworks\RealPlayer", "version")
    Now here is where neither one works and I don't understand why.

    On this one I get a Type Mismatch
    Code:
     Dim WshShell   As Object
     Dim strValue   As String
       
     Set WshShell = CreateObject("Wscript.Shell")
     strValue = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Bags\1\Desktop\ItemPos1024x768(1)")
    And on this one.....

    Please note that in the function sdaGetRegEntry

    I changed this:
    If Not ERROR_SUCCESS = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKeys, 0&, KEY_READ, lngHandle) Then Exit Function

    to this:

    If Not ERROR_SUCCESS = RegOpenKeyEx(HKEY_CURRENT_USER, strSubKeys, 0&, KEY_READ, lngHandle) Then Exit Function

    ....and it just returns a null value.
    Code:
     Dim Positions As String
    
     Positions = sdaGetRegEntry("Software\Microsoft\Windows\Shell\Bags\1\Desktop", "ItemPos1024x768(1)")

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How To Get Value From Registry Entry

    Dim WshShell As Object
    Dim strValue() As Variant

    Set WshShell = CreateObject("Wscript.Shell")
    strValue = WshShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Bags\1\Desktop\ItemPos1024x768( 1)")
    this works for me, but only as a variant OR variant array
    returns an array of
    0 to 3423 on my computer
    it maybe that it should return a TYPE of some sort
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How To Get Value From Registry Entry

    Windows Scripting Host can't be used to retrieve strings from REG_BINARY entries, only numbers. It can't cope with UDTs either - there's a limit to the size of the returned array that it can handle.

    EDIT: I had put some API code in here that would put REG_BINARY data in a UDT, but looking at the registry entry again, I've realised it contains an arry of UDTs. You'll first need to deduce the structure of the data the ValueName contains. Right-click in regedit and pick "Modify binary data" to copy/paste...
    Last edited by schoolbusdriver; Nov 10th, 2008 at 06:12 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
  •  



Click Here to Expand Forum to Full Width