In this post I'll try to explain how to gain access to native windows APIs in order to be able to interact with windows registry. The idea of the method I'm going to present here is to use JNA, which in turn gives access to native APIs of the underlying platform (no matter whether it is a Windows or UNIX). In turn you can use those apis in your Java application. I won't go into much detail as to how to implement a library that you need, but rather will give details as to how to compile and use the already contributed Registry.class. Okay, let's begin.
First you need to download JNA's jar package from here .This is the latest version as of 21.01.2010 - 3.2.4 released on 20.11.2009.Then put it in a folder. For the purpose of this document I'll use "C:\Users\lorddoskias\Documents\jlib\jna" along with it you will need some auxiliary files - everything from this link and also the registry class implementing the java bindings.
Put those files in the same folder as the JAR file. Then you need to edit all of the .java files and delete lines starting with "package jnacontrib.win32" after that you need to compile everything, this is done by issuing the following command:
Then compile advapi32.java and kernel32b.java and finally compile registry.java. With this "installation" procedure's complete.
How to use is - basically look at registry.java source file and everything should be pretty straightforward, but for the sake of clarity i'll give an example with querying a registry hive and enumerating all subkeys of a
particular key:
What this codes does is that it makes an array holding all of the subkeys of HKCU\AppEvets\
Basically when a function requires an argument of type REGISTRY_ROOT_KEY you have to pass either
Registry.REGISTRY_ROOT_KEY.CURRENT_USER, Registry.REGISTRY_ROOT_KEY.CLASSES_ROOT, Registry.REGISTRY_ROOT_KEY.LOCAL_MACHINE, Registry.REGISTRY_ROOT_KEY.USERS respectively for HKCU, HKLM, HKCR, HKU
PS/1
Java, JNA, JNI, Windows Registry, Registry APIS, Java jni windows
First you need to download JNA's jar package from here .This is the latest version as of 21.01.2010 - 3.2.4 released on 20.11.2009.Then put it in a folder. For the purpose of this document I'll use "C:\Users\lorddoskias\Documents\jlib\jna" along with it you will need some auxiliary files - everything from this link and also the registry class implementing the java bindings.
Put those files in the same folder as the JAR file. Then you need to edit all of the .java files and delete lines starting with "package jnacontrib.win32" after that you need to compile everything, this is done by issuing the following command:
javac -cp ""C:\Users\lorddoskias\Documents\jlib\jna\jna.jar;C:\Users\lorddoskias\Documents\jlib\jna"\<everything except advapi32.java and kernel32b.java and registry.java>
Then compile advapi32.java and kernel32b.java and finally compile registry.java. With this "installation" procedure's complete.
How to use is - basically look at registry.java source file and everything should be pretty straightforward, but for the sake of clarity i'll give an example with querying a registry hive and enumerating all subkeys of a
particular key:
public class Reg {
public static void main(String args[]) throws Exception {
String[] machine = Registry.getSubKeys(Registry.REGISTRY_ROOT_KEY.CURRENT_USER,"AppEvents");
System.out.println(machine[1]);
}
}
What this codes does is that it makes an array holding all of the subkeys of HKCU\AppEvets\
Basically when a function requires an argument of type REGISTRY_ROOT_KEY you have to pass either
Registry.REGISTRY_ROOT_KEY.CURRENT_USER, Registry.REGISTRY_ROOT_KEY.CLASSES_ROOT, Registry.REGISTRY_ROOT_KEY.LOCAL_MACHINE, Registry.REGISTRY_ROOT_KEY.USERS respectively for HKCU, HKLM, HKCR, HKU
PS/1
Java, JNA, JNI, Windows Registry, Registry APIS, Java jni windows


0 коментара:
Публикуване на коментар