Solved

error trying to retrieve registry key info in vb.net app

Posted on 2011-03-01
13
751 Views
Last Modified: 2012-05-11
the following code worked fine on my windows xp box

 Dim regKey As RegistryKey
        Dim regConnectionString As String

        regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
        regConnectionString = regKey.GetValue("ConnectionString", "")

        If regConnectionString <> "" Then
            m_sConnekStrg = regConnectionString
        Else
            Throw New Exception("Error reading registry: Employee Connection String.")
        End If

        regKey.Close()


but when I try to execute on my windows 7 box I get an object not set to a reference

when trying to execute  regConnectionString = regKey.GetValue("ConnectionString", "")

I get
0
Comment
Question by:johnnyg123
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35011236
Why aren't you storing connection strings in the web.config file?

http://articles.sitepoint.com/article/web-config-file-demystified
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35011239
Or if it's a windows application it's app.config.
0
 

Author Comment

by:johnnyg123
ID: 35011317
I don't like getting things from registry and normally do use config file but this app was written many moons ago and recently I have inherited support
0
 
LVL 33

Expert Comment

by:it_saige
ID: 35011432
For Windows 7, try using:

regKey = Registry.LocalMachineRegistry.OpenSubKey("Software", True)

Dim regKey As RegistryKey
	Dim regConnectionString As String

	'regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
	regKey = Registry.LocalMachineRegistry.OpenSubKey("Software\Employee", True)
	regConnectionString = regKey.GetValue("ConnectionString", "")

	If regConnectionString <> "" Then
		m_sConnekStrg = regConnectionString
	Else
		Throw New Exception("Error reading registry: Employee Connection String.")
	End If

	regKey.Close()

Open in new window

0
 
LVL 33

Expert Comment

by:it_saige
ID: 35011516
Actually, belay my last.  Are you running a 64bit version of Windows 7?

-saige-
0
 

Author Comment

by:johnnyg123
ID: 35011554
I did forget to mention I am running 64 bit windows 7
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:it_saige
ID: 35011567
In case you are not using a 64-bit version of Windows 7?

The appropriate code would be:
Dim regKey As RegistryKey
	Dim regConnectionString As String

	' Not this...
	' regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
	' Specify that you do not need write access to the key using
	regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
	regConnectionString = regKey.GetValue("ConnectionString", "")

	If regConnectionString <> "" Then
		m_sConnekStrg = regConnectionString
	Else
		Throw New Exception("Error reading registry: Employee Connection String.")
	End If

	regKey.Close()

Open in new window

HTH,

-saige-
0
 
LVL 33

Expert Comment

by:it_saige
ID: 35011905
In the case of 64-bit, the LocalMachine\Software hive is no longer located under,

\\HKEY_LOCAL_MACHINE\Software

This is now allocated as the location of 64-bit applications.  To find settings for 32-bit software, you would go one level further into:

\\HKEY_LOCAL_MACHINE\Software\WOW6432Node

So, here are the solutions.  You can:

1.  Recompile the application as a 64-bit application
-or-
2.  Add code into your method to first, distunguish the operating system and then based upon the operating system go to the correct hive location.

HTH,

-saige-
0
 
LVL 33

Expert Comment

by:it_saige
ID: 35012080
And when I say determine the operating system, I do not mean the OS by name but rather determine if it is an x86 application or x64 application.

Let say you used something like this:
Dim osBit As Integer = System.Runtime.InteropServices.Marshal.SizeOf(New IntPtr)
	Dim regKey As RegistryKey
	Dim regConnectionString As String

	' Not this...
	' regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
	' Specify that you do not need write access to the key using
	Select Case osBit
		' 32-bit Version
		Case 4
			regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
			regConnectionString = regKey.GetValue("ConnectionString", "")
		' 64-bit Version
		Case 8
			regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
			regConnectionString = regKey.GetValue("ConnectionString", "")
		Case Else
			' Add fall-out code in case of unsupported or undefined operating system
	End Select

	If regConnectionString <> "" Then
		m_sConnekStrg = regConnectionString
	Else
		Throw New Exception("Error reading registry: Employee Connection String.")
	End If

	regKey.Close()

Open in new window

HTH,

-saige-
0
 

Author Comment

by:johnnyg123
ID: 35012091
ok....I can try that
Guess I would have thought it would just tell me there is no such key rather than give me an object without reference error
0
 
LVL 33

Expert Comment

by:it_saige
ID: 35012129
Sometimes you can get too far ahead of yourself, especially when you dont have Visual Studio open and your just using notepad... *sigh* :P

This is what I meant:
Dim osBit As Integer = System.Runtime.InteropServices.Marshal.SizeOf(New IntPtr)
	Dim regKey As RegistryKey
	Dim regConnectionString As String

	' Not this...
	' regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee")
	' Specify that you do not need write access to the key using
	Select Case osBit
		' 32-bit Version
		Case 4
			regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Employee", False)
			regConnectionString = regKey.GetValue("ConnectionString", "")
		' 64-bit Version
		Case 8
			regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\WOW6432Node\Employee", False)
			regConnectionString = regKey.GetValue("ConnectionString", "")
		Case Else
			' Add fall-out code in case of unsupported or undefined operating system
	End Select

	If regConnectionString <> "" Then
		m_sConnekStrg = regConnectionString
	Else
		Throw New Exception("Error reading registry: Employee Connection String.")
	End If

	regKey.Close()

Open in new window

HTH,

-saige-
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 35012170
@johhnyg123:
--- Guess I would have thought it would just tell me there is no such key rather than give me an object without reference error

The reason it does not give you this is because of what the OpenSubKey method returns:

http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx

Return Value
Type: Microsoft.Win32.RegistryKey
The subkey requested, or Nothing if the operation failed.

If the operation fails to return a value, then the OpenSubKey method returns nothing, hence, Object without reference error.

-saige-

0
 
LVL 5

Expert Comment

by:MedievalWarrior
ID: 35013644
Hi,

The problem is Win32.Registry namespace doesn't have have the new view flags available until .NET Framework 4.0. If you use 4.0 you can use the code examples below, prior to .NET framework 4.0 you have to use P/Invoke RegOpenKeyEx() see Accessing an Alternate Registry View
' Read 64 bit view of registry from 32 bit application running on 64 bit OS
        Dim regkey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey("SOFTWARE\Medieval")
        Debug.Print(regkey.GetValue("Test").ToString)
        regkey.Close()


        ' Read 32 bit view of registry from 64 bit application running on 64 bit OS
        Dim regkey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\Medieval")
        Debug.Print(regkey.GetValue("Test").ToString)
        regkey.Close()

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now