Click here to Skip to main content
15,038,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on this function that will read keys from the HKCU on a remote computer. Microsoft states that you can do this with openremotebasekey but i cant seem to get it to rad some information from the voliatile environment area. below is the function i created and my goal is to get the HOMEPATH vaule from HKEY_CURRENT_USER\Volatile Environment. I keep getting the below error if someone can show me what im doing wrong that would be great.

Error: Object reference not set to an instance of an object.

Public Function ReadHKCUReg(ByVal strComputer As String, ByVal SubKey As String, ByVal KeyName As String) As String
    Dim str As String = DirectCast(Nothing, String)
    Dim CurrentUser As RegistryKey = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, strComputer)
    Dim Regvalue As RegistryKey = CurrentUser.OpenSubKey(SubKey)

    str = Regvalue.GetValue(KeyName)

    Return str

End Function

1 solution

First, we have no idea what the code looks like that is calling this function.

Second, you're not doing any checking after the Dim CurrentUser As RegistryKey... line to see if you actually have an object. Neither do you do it after the next line.

If either one of these lines returns Nothing, the next line will fail with the error you specified.

And finally, the DirectCast(Nothing, String) is useless. Just remove it. When you declare a string variable and don't assign anything to it, it is automatically initialized to Nothing.

There is also no such thing as converting Nothing to String. The end result is Nothing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900