Cannot read a registry key that is available in regedit. What gives?

I’m trying to find out if a certain key exists on a given Windows computer:

Dim reg As New RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\GPL Ghostscript")

This always throws a RegistryAccessErrorException, but the key is clearly there, verified in regedit. Is this a permissions thing? I’m not terribly familiar with windows registry voodoo - and it appears this exception can be thrown when a key does not exist or when you don’t have permission to read a given key.

How can I tell the difference? Are there certain sections of the registry that are just “off limits” to non-admin apps?

That will try to CREATE it if not present
See http://documentation.xojo.com/index.php/RegistryItem.Constructor(path_as_String_,_create_as_Boolean)
And if it already does then you get an error

try
Dim reg As New RegistryItem(“HKEY_LOCAL_MACHINE\SOFTWARE\GPL Ghostscript”, FALSE)

Thank you @Norman Palardy - out of curiosity, why is the example in the documentation NOT using the form of the constructor you recommend:

// get a registry key Dim reg As New RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")

I based my code on the example - is it incorrect?

And, as a follow up, this code:

Dim reg As New RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\GPL Ghostscript", false)

Also generates the same RegistryAccessErrorException.

[quote=281533:@Kimball Larsen]Thank you @Norman Palardy - out of curiosity, why is the example in the documentation NOT using the form of the constructor you recommend:

// get a registry key Dim reg As New RegistryItem("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
[/quote]
No idea - I’d suspect its been that way for a very long time and no one has ever been prodded to update it since that key exists.
Its just illustrating the concept - nothing more.
The exception I’m not sure of.
Could be permissions to read certain keys in that part of the registry OR that its missing (see http://documentation.xojo.com/index.php/RegistryAccessErrorException)

Dont see how you can tell to be honest.

I think the documentation is a little misleading as well. From my reading of the description of the Constructor:

The optional parameter Create defaults to True; if you set it to False, the path will not be created if the path is not found. Instead, a RegistryAccessErrorException will occur.

This seems to indicate that if the path already exists in the registry, then the existing key will be used to populate the new RegistryItem object. If the path does not exist, then it will be created, unless you set create to false, in which case the exception will be thrown.

Should I file a feedback to clean up this documentation? It’s really not clear.

Keep in mind that in 32 bit apps SYSWOW6432Node is automatically inserted, so you’re actually trying to access HKEY_LOCAL_MACHINE\SOFTWARE\SYSWOW6432Node\Microsoft\Windows NT\CurrentVersion
and your key probably does not exit there.

At least that’s the way I remember it working.

This seems like a wonderful thing to put in the documentation for RegistryItem. I had no idea.

<https://xojo.com/issue/44887>

Example:

[code]Function ReadRegistry(Folder As String, Key As String) As Variant
Dim myRegItem As RegistryItem
Dim found As Boolean
Dim elements(-1), path As String

found = True
elements = folder.split("")

For i As Integer = 0 To Elements.Ubound
path = path + elements(i) + “”

Try
  myRegItem = New RegistryItem(path, False)
  
Catch err As RegistryAccessErrorException
  found = False
  Exit
End Try

Next

If found And myRegItem <> Nil Then
Return myRegItem.value(key)
Else
Return “”
End If

End Function[/code]