Results 1 to 8 of 8

Thread: [RESOLVED] Get the value of a dword from the registry

  1. #1

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

    Resolved [RESOLVED] Get the value of a dword from the registry

    I am unable to get a value from the registry, every time i run my code it returns a value of 'NoAutoRebootWithLoggedOnUser', even though the value should be 1.

    What have i done wrong?

    Thanks Chris1990,
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             ''CheckStatus()
    
            Dim readValue As String
            readValue = My.Computer.Registry.LocalMachine.GetValue _
        ("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU", "NoAutoRebootWithLoggedOnUser", Nothing)
            MsgBox("The value is " & readValue)
        End Sub
    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
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Get the value of a dword from the registry

    readValue should be an object that is returned not always a string, see this example.

    This part especially:

    Case RegistryValueKind.DWord
    Console.WriteLine("Value = " + Convert.ToString(CType(o,Int32)))

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Get the value of a dword from the registry

    It's a matter of not paying attention to your code.

    My.Computer.LocalMachine returns a RegistryKey object. If you have a look, you'll see the 3-argument version takes a name, a default value, and a RegistryValueOptions.

    If Option Strict were on, it'd have helped you figure this out by giving you a compile error: Nothing isn't a valid value for the third parameter. Since Option Strict is off, the compiler is "helping" you by automatically converting it to 0. You think your code says, "Get the value of 'NoAutoRebootWithLoggedOnUser' from 'HKLM\SOFTWARE...', and return Nothing if the value cannot be retrieved." Instead, your code says, "Get the value of 'HKLM\SOFTWARE\...\AU' and return 'NoAutoRebootWithLoggedOnUser' if the valeu cannot be retrieved, do not use any optional behaviors." Obviously you can't retrieve the value of that key (it probably has no value), so you return the default.

    Values are what live under keys. There's no overload of GetValue() that lets you specify a subkey path and a value name. But you can open subkeys and use the subkey's GetValue() method:
    Code:
    Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU")
        Dim value As Object = key.GetValue("NoAutoRebootWithLoggedOnUsers")
        Console.WriteLine(value)
        Console.ReadLine()
    End Using
    Alternatively, it looks like you're confused with My.Computer.Registry.GetValue(), which *does* have the 3-args version you tried to call:
    Code:
    Dim value As Object = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU", "NoAutoRebootWithLoggedOnUsers", Nothing)
    Console.WriteLine(value)
    Console.ReadLine()
    This is why I never use the "My.Whatever" classes: they usually have a different API than the .NET classes that would normally do the job, and when they return the .NET API objects you can get quite confused as you have to remember which context you're in.

  4. #4

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

    Re: Get the value of a dword from the registry

    thanks for your help.

    @Sitten_Spynne: I am using your 2nd example, but still when i run the code it returns nothing 'no data', even though i know that the value is definitely 1. I've tried running the program with admin rights as well.

    Code:
    Dim Value As Object = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU", "NoAutoRebootWithLoggedOnUsers", "no data")
            MsgBox(Value)
    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.

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Get the value of a dword from the registry

    Works fine on my machine, and I copy/pasted your code. Double-check the registry key exists. Check your project settings. My projects are targeted for x86 but running on x64; maybe yours is different and that has an impact? I'm leaning towards "something's wrong with the key".

  6. #6

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

    Re: Get the value of a dword from the registry

    I think its because the key's a dword, i've just tested a string and it works fine.

    Code:
            Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU")
                Dim value As Object = key.GetValue("test")
                ''Dim value As Object = key.GetValue("NoAutoRebootWithLoggedOnUsers")
                MsgBox(value)
            End Using
    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.

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Get the value of a dword from the registry

    I think that's a red herring, because as I stated in #5 it works fine on my machine. Something else must be in play, or there's a difference between our environments that is causing the behavior.

  8. #8

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

    Re: Get the value of a dword from the registry

    lol, I've just noticed my mistake after 2 days of trying to figure it out.

    You was right, it was the wrong registry key 'NoAutoRebootWithLoggedOnUsers' it should of been 'NoAutoRebootWithLoggedOnUser'. Strange how the s has appeared, when i copied it from the code i used to write the key with.

    i feel so stupid, thanks for your help.
    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.

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