B4J Library Windows Registry - jRegistry

This is a very quick wrap of the Windows registry library SimpleRegistry


I needed to extract a registry value on Windows. I looked at many libraries and code samples on the forum and out on the internet. Unfortunately WIndows Registry and Java dont seem to work well together and the only options using pure Java seem to use one of the following options;

1. Use reflection and setting of "preference" private methods to public - these dont appear to work in Java 11+
2. Use JNI to call Windows DLLs - a bit "clunky" and unreliable
3. Calling the "reg" console app - although not ideal this approach seemed the best

After testing the various libraries and not having any luck I settled on #3 with the SimpleRegistry library. The authors apoproach is simple yet effective.

I realise I could have written my own code to use jShell to call the "reg" console app but it was much quicker to simply compile SimpleRegistry using SLC and make it B4J compatible.

NOTE I have not tested every method and some methods return native types instead of B4J types eg the RegistryKey Values method needs to be cast as a list to iterate.

NOTE You will get access denied if you are trying to set values in HKEY_LOCAL_MACHINE unless you are running as admin.

Really quick example to read a registry key;

B4X:
Dim reg As jRegistry
reg.Initialize
FME_SERVER_HOME = reg.getValue("HKEY_LOCAL_MACHINE\SOFTWARE\Safe Software Inc.\Feature Manipulation Engine","FME_SERVER_HOME").As(RegistryValue).Value

Another example, list all values under a key.

B4X:
Dim reg As jRegistry
reg.Initialize

Dim rk As RegistryKey = reg.getKey("HKEY_LOCAL_MACHINE\SOFTWARE\ESRI\ArcGISPro")
 
For Each v As RegistryValue In rk.Values.As(List)
    Log(v.Name)
    Log(v.Value)
Next

Create new key and set value

B4X:
Dim reg As jRegistry
reg.Initialize
reg.setValue("HKEY_CURRENT_USER\SOFTWARE\Peppe","Version",reg.REG_SZ,"","1.2.3")
 

Attachments

  • jRegistry.zip
    13.3 KB · Views: 107
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
I believe @amorosik question is specific to this particular library
 

amorosik

Expert
Licensed User
I've try to use the example on Github link but

Dim rk As jRegistry
rk.setValue("Location_Peppe",RegistryValue.Type.REG_SZ,"\0", "Hello World!")

are not ready to run
How to write on a registry key?
 

tchart

Well-Known Member
Licensed User
Longtime User
I've try to use the example on Github link but

Dim rk As jRegistry
rk.setValue("Location_Peppe",RegistryValue.Type.REG_SZ,"\0", "Hello World!")

are not ready to run
How to write on a registry key?

Couple of things;

1. You need to initialize jRegistry
2. The setValue methods are expecting a RegistryValue.Type - this is an enum which doesnt work with B4J but...

I have updated the library so that RegistryValue.Type enum is now available on the reg object, so you can do this now;

B4X:
Dim reg As jRegistry
reg.Initialize
reg.setValue("HKEY_CURRENT_USER\SOFTWARE\Peppe","Version",reg.REG_SZ,"","1.2.3")
 

amorosik

Expert
Licensed User
And to create a new key?
For example if you wanted to create the key
"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\11.0\Access\Security\Trusted Locations\Location_101"
I have try with:

Dim reg As jRegistry
reg.Initialize
reg.Key ("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\11.0\Access\Security\Trusted Locations\Location_101")

but is not correct
 

tchart

Well-Known Member
Licensed User
Longtime User
And to create a new key?
For example if you wanted to create the key
"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\11.0\Access\Security\Trusted Locations\Location_101"
I have try with:

Dim reg As jRegistry
reg.Initialize
reg.Key ("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\11.0\Access\Security\Trusted Locations\Location_101")

but is not correct
B4X:
reg.Key = "HKEY_CURRENT_USER\SOFTWARE\Peppe\Peppe2"

The original library method is called setKey. The B4X SLC compiler interprets this as being write only which means it needs to be called like that and not as a method. I should probably rename the method to CreateKey or something better.

1682916775714.png
 

DonManfred

Expert
Licensed User
Longtime User
The B4X SLC compiler interprets
This is how Properties and Methods are being recognized.
setValue and getValue results in a Property Value.
SetValue is a Method
B4X:
/*
     * Property Value (setter)
     */
    public void setValue(String val) {
        //
    }
    /*
     * Property Value (getter)
     */
    public String getValue() {
        return "";
    }

    /*
     * Method SetContent
     */
    public void SetContent(String val) {
        //
    }
    /*
     * Property Content (getter)
     */
    public String getContent() {
        return "";
    }
 
Top