Search code examples
vb.netvisual-studioregistryvisual-studio-201332bit-64bit

How to look for a registry key? 32/64 bit


How to look for a registry key? I need to look from both 32 and 64 bit systems with 3 diffrent GUID's. I need to return the one it found the InstallLocation in to a TextBox. I have made the following code. Tho i don't know if it's actually usable. I am a beginner at this. Please help.

Imports Microsoft.Win32
Imports System.Net
Imports System

Public Class Form1

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

        Is64Bit = String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))

        If Not Is64Bit Then
            Try
                Dim rk32_1 As RegistryKey
                rk32_1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812EU}_is1")
                Dim il_rk32_1 As String = rk32_1.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk32_2 As RegistryKey
                rk32_2 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812NA}_is1")
                Dim il_rk32_2 As String = rk32_2.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk32_3 As RegistryKey
                rk32_3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812RU}_is1")
                Dim il_rk32_3 As String = rk32_3.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
        End If

        If Is64Bit Then
            Try
                Dim rk64_1 As RegistryKey
                rk64_1 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812EU}_is1")
                Dim il_rk64_1 As String = rk64_1.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk64_2 As RegistryKey
                rk64_2 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812NA}_is1")
                Dim il_rk64_2 As String = rk64_2.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
            Try
                Dim rk64_3 As RegistryKey
                rk64_3 = Registry.LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1EAC1D02-C6AC-4FA6-9A44-96258C37C812RU}_is1")
                Dim il_rk64_3 As String = rk64_3.GetValue("InstallLocation").ToString

            Catch ex As Exception

            End Try
        End If

    End Sub

End Class

Solution

  • Firstly, keep in mind that if the program is compiled as x86, the (IntPtr.Size * 8) will always return 32 (if this is an issue, I do have a class that can get the OS version regardless of how the exe is compiled)

    I'd recommend using the following :

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        ' Check if the OS is 64 bit
        Dim Is64Bit As Boolean = ((IntPtr.Size * 8) = 64)
    
        Dim UninstallPath As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    
        If Is64Bit Then
            UninstallPath = UninstallPath.Insert(8, "\Wow6432Node")
        End If
    
        Dim il_rk32_1 As String = Nothing
        Dim il_rk32_2 As String = Nothing
        Dim il_rk32_3 As String = Nothing
    
        Try
            Dim rk32_1 As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallPath & "{1EAC1D02-C6AC-4FA6-9A44-96258C37C812EU}_is1")
    
            If rk32_1 IsNot Nothing Then
                Dim Val As Object = rk32_1.GetValue("InstallLocation")
    
                If Val IsNot Nothing Then
                    il_rk32_1 = Val.ToString
                End If
            End If
    
        Catch ex As Exception
    
        End Try
    
        If String.IsNullOrEmpty(il_rk32_1) Then
            il_rk32_1 = "C:\Games\World_of_Tanks\"
        End If
    
        Me.TextBox1.Text = il_rk32_1
    
    
        Try
            Dim rk32_2 As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallPath & "{1EAC1D02-C6AC-4FA6-9A44-96258C37C812NA}_is1")
    
            If rk32_2 IsNot Nothing Then
                Dim Val As Object = rk32_2.GetValue("InstallLocation")
    
                If Val IsNot Nothing Then
                    il_rk32_2 = Val.ToString
                End If
            End If
    
        Catch ex As Exception
    
        End Try
    
        If String.IsNullOrEmpty(il_rk32_2) Then
            il_rk32_2 = "C:\Games\World_of_Tanks\"
        End If
    
        Me.TextBox2.Text = il_rk32_2
    
    
        Try
            Dim rk32_3 As RegistryKey = Registry.LocalMachine.OpenSubKey(UninstallPath & "{1EAC1D02-C6AC-4FA6-9A44-96258C37C812RU}_is1")
    
            If rk32_3 IsNot Nothing Then
                Dim Val As Object = rk32_3.GetValue("InstallLocation")
    
                If Val IsNot Nothing Then
                    il_rk32_3 = Val.ToString
                End If
            End If
    
        Catch ex As Exception
    
        End Try
    
        If String.IsNullOrEmpty(il_rk32_3) Then
            il_rk32_3 = "C:\Games\World_of_Tanks\"
        End If
    
        Me.TextBox3.Text = il_rk32_3
    
    End Sub
    

    Edit:

    I've changed the above code to accomedate the OP's question (in the comments)