How to read a value from a registry key

3

I'm trying to get a value from the registry key but the value returns empty, does anyone have any other way to get the registry key value?

Follow the code in VB.NET

Private Sub ObterNomeDeRede()

Dim ValorRegistro = 
My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows 
NT\CurrentVersion\NetworkList\NewNetWorks", "NetworkList", Nothing)

MsgBox(ValorRegistro, vbInformation)

End Sub
    
asked by anonymous 08.03.2018 / 03:19

2 answers

3

I tried to use the same code and also did not return any value, although the key exists in the Registry of Windows. I tried it this way:

Dim regKey = My.Computer.Registry.LocalMachine.OpenSubKey(
   "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks")
Dim value = regKey.GetValue("NetworkList")

But it gave error in the second line, saying that the variable regKey contained no value (exception Object reference not set to an instance of an object ).

I then ran Sysinternals' Process Monitor , filtering only Registry and found that the program was actually trying to access this key (note the subkey that was added): HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks .

My Windows is 64-bit, so I soon figured the program was running in 32-bit. To be sure, I put two labels on the Form to check for two things, if the operating system was 64-bit and if the application was 64-bit:

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

      Is64BitOsLabel.Text = $"Is 64-bit OS: {Environment.Is64BitOperatingSystem}"
      Is64BitAppLabel.Text = $"Is 64-bit App: {Is64BitApp()}"

   End Sub

   Private Function Is64BitApp() As Boolean

      Return IntPtr.Size = 8

   End Function

(The code for the function Is64BitApp() I found here )

I then confirmed that the OS was 64-bit, but the application was not, although the Target CPU of my project was marked as AnyCPU . It was then that I noticed the Prefer 32-bit option, just below the Target CPU option in Project properties > Compile , which was already checked when I created the project. I unchecked this option and then the application started to run as 64-bit.

I tried again to access the registry key and this time it was found, but then it started to give the Requested registry access is not allowed exception, because I was trying to access HKEY_LOCAL_MACHINE with an ordinary user. I ran Visual Studio as an administrator and it finally worked.

The only thing that did not work was the display of the returned value in a MsgBox , as you did, because since this value is of type REG_MULTI_SZ , the returned type was String() .

EDIT:
Then I discovered that I would not even need to have created the Is64BitApp() function because, in addition to the Environment.Is64BitOperatingSystem property I used in the code, there is also the Environment.Is64BitProcess property:

  

Environment.Is64BitProcess Property
link

After this question the user jnmoura did another, which I also answered, which complements a bit this, so I will leave here as a reference:

  

Access 64-bit key in Windows Registry through 32-bit application

    
08.03.2018 / 09:21
1

I left it this way:

 Private Sub ObterTipoDaRede()
    Dim pRegKey As RegistryKey = Registry.LocalMachine
    pRegKey = pRegKey.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\NewNetworks")
    Dim val As Object = pRegKey.GetValue("NetworkList")
    Me.teste = val
End Sub


Public Sub Preencher_Grid_dgvDadosComputador(Grid As Object)
    With Grid
        .Rows.Add("Nome_Do_Computador ", Me.NomeMaquina)
        .Rows.Add("Nome_Domínio ", Me.DominioMaquina)
        .Rows.Add("Pasta_(Moura_)", Me.PastaMoura)
        .Rows.Add("Firewall_Redes/Domínio", Me.FirewallRedesDominio)
        .Rows.Add("Firewall_Redes/Privadas", Me.FirewallRedesPrivadas)
        .Rows.Add("Firewall_Redes/Publicas", Me.FireWallRedesPublicasConvidadas)
        .Rows.Add("teste", Me.teste)
    End With
End Sub

In the constructor I call the sub

Public Sub New()

    ObterTipoDaRede()

End Sub

And in the form load I call the procedure to populate the datagrid

 Dim Analise As New Analisar()


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Analise.Preencher_Grid_dgvDadosComputador(dgvDadosComputador)

End Sub
    
08.03.2018 / 16:28