Reputation: 530
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
Upvotes: 2
Views: 4896
Reputation: 4715
In order to access application configuration installed from a .reg file, I had to write the following function:
Public Const RegRoot As String = "Software\MyApp\"
Function ReadFromRegister(ByRef FieldToRead As String) As String
ReadFromRegister = ""
Dim MyReg As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
If MyReg Is Nothing Then
End
End If
Dim MySubReg As RegistryKey = MyReg.OpenSubKey(RegRoot)
If MySubReg Is Nothing Then
End
End If
ReadFromRegister = MySubReg.GetValue(FieldToRead)
MySubReg.Close()
MyReg.Close()
End Function
Note that the function OpenBaseKey
is introduced from Framework 4 on.
Upvotes: 0
Reputation: 679
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)
Upvotes: 0
Reputation: 20494
I didn't looked too much at your code 'cause you did the opposite checks. Wow64 refers to 32-Bit applications on 64-Bit machine.
From Wikipedia :
WoW64 (Windows 32-bit on Windows 64-bit)
The WoW64 subsystem also handles other key aspects of running 32-bit applications. It is involved in managing the interaction of 32-bit applications with the Windows components such as the Registry, which has distinct keys for 64-bit and 32-bit applications. For example HKEY_LOCAL_MACHINE\Software\Wow6432Node is the 32-bit equivalent of HKEY_LOCAL_MACHINE\Software (although 32-bit applications are not aware of this redirection). Some Registry keys are mapped from 64-bit to their 32-bit equivalents, while others have their contents mirrored, depending on the edition of Windows.
PS: Just to notice it, the same thing happens for System32
and SysWOW64
windows directories.
UPDATE:
Just an easy way to retrieve whether the OS is x64 (instead of getting a Environment variable PROCESSOR_ARCHITEW6432
which could be faked in a x86 OS):
' Get OS Architecture
' ( By Elektro)
'
' Usage Examples :
' Dim OSArchitecture As Architecture = GetOSArchitecture()
' MsgBox(OSArchitecture.ToString)
'
''' <summary>
''' Determines whether the OS is 32 or 64 Bits.
''' </summary>
''' <returns>
''' The return value could be:
''' '32' for 32-Bit OS (x86)
''' '64' for 64-Bit OS (x64)
''' </returns>
Private Function GetOSArchitecture() As Architecture
Return [Enum].Parse(GetType(Architecture),
Runtime.InteropServices.Marshal.SizeOf(GetType(IntPtr)) * 8)
End Function
''' <summary>
''' Indicates the possible processor architectures.
''' </summary>
Private Enum Architecture As Integer
''' <summary>
''' 32-Bit
''' </summary>
x86 = 32
''' <summary>
''' 64-Bit
''' </summary>
x64 = 64
End Enum
Upvotes: 1