Get Value from registry in Windows 8

Pinto

Member
Joined
May 19, 2013
Messages
15
Programming Experience
5-10
I have a program that reads and writes values to the registry.
My program works just fine on windows t but not on windows 8.

the function I use is as follows:
 Public Function ReadRegistryValue(ByVal MainKey As RegistryKey, ByVal sKey As String, ByVal sKeyName As String, _
                                ByRef oNameValue As Object) As String
        Dim rkKey As RegistryKey
        Dim Value As New String("")
        Try
            'open the given subkey
            rkKey = MainKey.OpenSubKey(sKey, True)
            'check to see if the subkey exists
            If rkKey Is Nothing Then 'it doesnt exist
                'throw an exception
                rkKey = MainKey.CreateSubKey(sKey, RegistryKeyPermissionCheck.Default)
                'set the value of the subkey
                rkKey.SetValue(sKeyName, oNameValue, RegistryValueKind.String)
                'Throw New Exception("The Registry SubKey provided doesnt exist!")
            End If
            'get the value
            oNameValue = rkKey.GetValue(sKeyName)
            Value = oNameValue.ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: Reading Registry Value", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return Value
    End Function


If the entry does not exist it creates the entry and sets the value. On windows 8 I get the following error
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.

Why does this work differently in windows 8?

Does anyone have a solution?

Thanks in advance
Pinto
 
Last edited by a moderator:
I really don't use the Registry much at all and, as recommended by Microsoft, you shouldn't either, if you can avoid it. If you can't avoid it then you need to take Registry virtualisation into account, which I think is likely the issue here. If the SetValue call is not throwing an exception then the value is being set, but Windows 7 and later will not write to the actual HKEY_LOCAL_MACHINE hive for non-admin users, but rather a virtual copy of it. I don't know all the details because, as I said, I really don't use the Registry. That's a topic that you should research though, to determine how you can effectively use the Registry with the tighter security in Windows 7 and later.
 
We do not use the HKEY_LOCAL_MACHINE we actually USE HKEY_CURRENT_USER.

I guess I have some more learning to do. Unfortunately we have a lot of info that is stored in the reg. It would be a huge undertaking to change that functionality right now. Thanks for the advice though.
 
Imports Microsoft.Win32
' enable or disable task manager
Dim x As RegistryKey = Registry.CurrentUser.CreateSubKey("Software\Microsoft\Windows\CurrentVersion\Policies\System")
x.SetValue("DisableTaskMgr", 1)
x.Close()

this is the simplest method of doing this i think. You can change the key to whatever path you want or any value.
 
Im glad to help, i have researched the registry allot so if you need anything pm me :) :) :) :) 8)

Or better still, start a new thread so that everyone can see it, provide help and benefit from any help provided. It's then possible to PM someone to direct them to that thread. Let's not hide anything away because that defeats the purpose of a public forum.
 
Back
Top