how to check if a registry key/value exists?

Hi folks. Like the title says, I need to know how to check if a registry key or value exists. Been through the docs, but now I’m more confused than ever. Any help would be greatly appreciated.

For the record, I’ve been using this:

reg = new RegistryItem("HKEY_CLASSES_ROOT\\Directory\\shell\\OpenCmdHereAsAdmin", false) Catch err as RegistryAccessErrorException

That just seems to stop all operations at that point though, and if the key doesn’t exist, I want to create it (already know how to do that).

ok, missed the obvious, but I’m still a bit confused about something. I have the key and value being created if they don’t already exist, but how do I control what TYPE of value is created? I’m now using this:

Dim reg as RegistryItem Dim reg As New RegistryItem("HKEY_CLASSES_ROOT\\Directory\\shell\\OpenCmdHereAsAdmin", true) reg.Value("Icon") = "imageres.dll,-5324"
This creates the “Icon” value as type REG_SZ. Don’t even know if that’s the correct type.

Hi Bill,
There are a few Registry Types that are available in native Xojo. To be able to choose from all of the types to be added, then the program will probably need to work with declares. If you already have a proper entry in the Registry, then this declare will allow the program to set the type of Registry Type:

Public Function RegSetValueExA(HKEY as Int32, lpValueName as CString, lpReserved as Int32, dwType as Int32, lpData as CString, cbData as int32) as UInt32 #If TargetWin32 Then Soft Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (_ HKEY as Int32, _ lpValueName as CString,_ lpReserved as Int32, _ dwType as Int32, _ lpData as CString, _ cbData as int32) As UInt32 Return RegSetValueEx(HKey, lpValueName, lpReserved,dwType, lpData, cbData) #Endif End Function

An example to set the type to REG_SZ is:

//Create a new key value and add a string to the value Dim s as String = "MyNewString" MyResult = Registry.RegSetValueExA(HKey, "MyNewRegSZ", &h0, &h1, s, s.Len)

The REG_SZ type has a value of &h1. There are many types available and it depends on what you want to do.

What are you wanting to do?

I wrote a book on how to perform some advanced Registry Work with Xojo and the title of the book is called “Program Windows 10 Registry with Xojo”. Its available at the following link. XojoLibrary

In Xojo you can indirectly control the type by ensuring you have the type you want went assigning the Value, it will make a best guess for you. See the bottom of RegistryItem — Xojo documentation for more info.

If you want more control, you’ll need to do what Eugene suggests above, but I’d use RegSetValueExW rather than RegSetValueExA so you don’t run into unicode filename issues down the line.

Thanks for all the help, guys!
Eugene, I’m building an app to set a whole whack of windows 10 tweaks, some of which are done in the registry. The example I gave creates an “Open command prompt here as administrator” context menu entry. There are several entries required to accomplish this, but I’m working on just one to get things figured out before proceeding further.

No worries, sounds good. The link was meant to be https://documentation.xojo.com/api/windows/registryitem.html#registryitem-value :slight_smile:

Thanks, Julian. I’m still very much a rookie, so it took me a bit to get my head around how to use RegistryItem.Value in my app, but I got it. Some of this might well be overkill, but here’s what I’ve got now:

[code]Dim reg As New RegistryItem(“HKEY_CLASSES_ROOT\Directory\shell”, False)

Dim reg2 as Variant
reg2 = reg.Value(“OpenCmdHereAsAdmin”)
if reg2 <> Nil then
contadmin.Value = False
contadminun.Value = True
else
contadmin.Value = True
contadminun.Value = False
end if[/code]
contadmin and contadminun are checkboxes. This code is just to check on app startup what registry tweaks have or have not been done, and set the appropriate checkboxes accordingly. The actual registry edits happen when the user clicks an “Apply” button after making the desired selections. An example of implementing this particular registry edit looks like this:

Dim reg As New RegistryItem("HKEY_CLASSES_ROOT\\Directory\\shell\\OpenCmdHereAsAdmin", True) reg.Value("Icon") = "imageres.dll,-5324" contadmin.Value = false contadminun.Value = true
I don’t know how elegant this is, but it works. :wink: